How to delete rows in tables that contain foreign keys to other tables

前端 未结 5 1388
借酒劲吻你
借酒劲吻你 2020-12-05 00:07

Suppose there is a main table containing a primary key and there is another table which contains a foreign key to this main table. So if we delete the row of main table it w

5条回答
  •  悲哀的现实
    2020-12-05 00:50

    First, as a one-time data-scrubbing exercise, delete the orphaned rows e.g.

    DELETE 
      FROM ReferencingTable 
     WHERE NOT EXISTS (
                       SELECT * 
                         FROM MainTable AS T1
                        WHERE T1.pk_col_1 = ReferencingTable.pk_col_1
                      );
    

    Second, as a one-time schema-alteration exercise, add the ON DELETE CASCADE referential action to the foreign key on the referencing table e.g.

    ALTER TABLE ReferencingTable DROP 
       CONSTRAINT fk__ReferencingTable__MainTable;
    
    ALTER TABLE ReferencingTable ADD 
       CONSTRAINT fk__ReferencingTable__MainTable 
          FOREIGN KEY (pk_col_1)
          REFERENCES MainTable (pk_col_1)
          ON DELETE CASCADE;
    

    Then, forevermore, rows in the referencing tables will automatically be deleted when their referenced row is deleted.

提交回复
热议问题