Sample Header Ad - 728x90

Query to delete all rows which are not referenced in any other tables

2 votes
1 answer
1948 views
Let's say i have a table called **images**. **Images** table is referenced in many other tables (**movies**, **books**, **tv_series**) through a foreign key. My question is if it is possible to write a query which will delete all orphaned rows without specifying exact tables (eg JOIN movies ON ...). This is important because if new table which references **images** will be introduced it will make the query outdated. If the query will be executed without changes it could delete images which are used in new table.
Asked by Daniel G (23 rep)
Feb 3, 2019, 04:16 PM
Last activity: Feb 20, 2021, 11:06 AM