sql - delete from table with foreign keys to other tables -


i trying delete table(mytable) has foreign key reference linking 4 other tables.

i needed delete data mytable referenced table1 , table2, not table3 , table4. have deleted data in table1 , table2

i tried this:

delete mytable id not in(select mytableid table1) delete mytable id not in(select mytableid table2) 

but doesn't work because if did inadvertently delete data table2 references.

is there way delete table fks aren't being referenced tables?

(rewritten answer sql server syntax after basic research , finding the delete statement in sql server.)

use multiple table syntax of delete statement.

delete   mytable   mytable   left join table1 on mytable.id = table1.mytableid   left join table2 on mytable.id = table2.mytableid   left join table3 on mytable.id = table3.mytableid   left join table4 on mytable.id = table4.mytableid   (table1.mytableid not null or table2.mytableid not null)   , table3.mytableid null   , table4.mytableid null 

the delete operate on table before from clause. can select rows using other tables in from clause not affected. example joins mytable tables mention , checks each row either table1 or table2 refer row and table3 , table4 not refer row.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -