| 
                
Please start any new threads on our new 
    site at https://forums.sqlteam.com.  We've got lots of great SQL Server
    experts to answer whatever question you can come up with. 
    
        | 
                
                    | 
                            
                                | Author | Topic |  
                                    | chennaraajStarting Member
 
 
                                        17 Posts | 
                                            
                                            |  Posted - 2014-07-30 : 05:27:54 
 |  
                                            | Hi All,  Need to delete records from dependent tables based on foreign key relationship.ex.Table1 foreign key  with Table2Table2 foreign key  with Table3Table3 foreign key  with Table4ect.....from the above tables i need to delete the records from Table2,3,4 based on Table1 IDrk |  |  
                                    | gbrittonMaster Smack Fu Yak Hacker
 
 
                                    2780 Posts | 
                                        
                                          |  Posted - 2014-07-30 : 08:15:19 
 |  
                                          | (at least your tables don't have circular references!)If you think of your tables in tiers or levels, level 0 would be a table with no FKs.  Level 1 would have one or more FKs refering to Level 0 tables, etc. (I know that the real world is not always that simple!)  Find Level "n" -- the highest tier in the dependency tree.  No tables point to it via FKs.  You start deleting from that table, then move to Level n-1, n-2 ... Level 0.NOte that SQL can do this for you if you have cascading set up.  See here: http://technet.microsoft.com/en-us/library/ms186973(v=sql.105).aspx |  
                                          |  |  |  
                                    | MichaelJSQLConstraint Violating Yak Guru
 
 
                                    252 Posts | 
                                        
                                          |  Posted - 2014-07-30 : 08:19:32 
 |  
                                          | CREATE TABLE T1(ID1 INT Primary Key)CREATE TABLE T2(ID2 INT  Primary Key,ID1 INT)CREATE TABLE T3(ID3 INT  Primary Key,ID2 INT)CREATE TABLE T4(ID4 INT  Primary Key,ID3 INT)ALTER TABLE t4ADD CONSTRAINT FK_T4 FOREIGN KEY (ID3)     REFERENCES T3 (ID3)    -- ON DELETE CASCADE   ALTER TABLE t3ADD CONSTRAINT FK_T3 FOREIGN KEY (ID2)     REFERENCES T2 (ID2)    -- ON DELETE CASCADE   ALTER TABLE t2ADD CONSTRAINT FK_T2 FOREIGN KEY (ID1)     REFERENCES T1 (ID1)    -- ON DELETE CASCADEINSERT INTO T1VALUES(1)INSERT INTO T2VALUES(2,1)INSERT INTO T3VALUES(3,2)INSERT INTO T4VALUES(4,3)SELECT * FROM T1SELECT * FROM T2SELECT * FROM T3SELECT * FROM T4DELETE FROM t4 WHERE ID3 = 3DELETE FROM t3 WHERE ID2 = 2DELETE FROM t2 WHERE ID1 = 1DELETE FROM t1 WHERE ID1 =1SELECT * FROM T1SELECT * FROM T2SELECT * FROM T3SELECT * FROM T4 ALTER TABLE T4 DROP CONSTRAINT FK_T4ALTER TABLE T3 DROP CONSTRAINT FK_T3ALTER TABLE T2 DROP CONSTRAINT FK_T2ALTER TABLE t4ADD CONSTRAINT FK_T4 FOREIGN KEY (ID3)     REFERENCES T3 (ID3)    ON DELETE CASCADE   ALTER TABLE t3ADD CONSTRAINT FK_T3 FOREIGN KEY (ID2)     REFERENCES T2 (ID2)    ON DELETE CASCADE   ALTER TABLE t2ADD CONSTRAINT FK_T2 FOREIGN KEY (ID1)     REFERENCES T1 (ID1)    ON DELETE CASCADEDELETE FROM T1 WHERE ID1 = 1SELECT * FROM T1SELECT * FROM T2SELECT * FROM T3SELECT * FROM T4 |  
                                          |  |  |  
                                    | MichaelJSQLConstraint Violating Yak Guru
 
 
                                    252 Posts | 
                                        
                                          |  Posted - 2014-07-30 : 08:21:29 
 |  
                                          | Oh - copied over my blurb with my code: You can either delete in the appropriate order from lowest child to parent or alter  your constraints to CACADE Delete; Both option I coded here, but it looks like gbritton is pretty much saying the same thing. |  
                                          |  |  |  
                                |  |  |  |  |  |