I had to restore table content yesterday since a application could not find the data needed. On examining what had happened we discovered a quite scary scenario. The development team was meaning to do a delete of specific posts in the table and executed the below query.
delete from table_3 where table2_id in (
select table2_id from table2
where table1_id in (500, 557, 256, 158, 598, 601)
);
This resulted in a delete of all the posts in table3.
There was an error in the query though the primary key in table2 is named id
not table2_id
.
The correct query should look like this.
delete from table_3 where table2_id in (
select id from table2
where table1_id in (500, 557, 256, 158, 598, 601)
);
To conclude.
When trying to execute the faulty subquery separately it correctly throws an error that there is no column named table2_id
in table2, but when executing the entire query no error is thrown and all the posts in table3 are deleted.
Shouldn't it throw an error when executing the entire query if there is something wrong with the subquery?