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

前端 未结 5 1390
借酒劲吻你
借酒劲吻你 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:30

    You can alter a foreign key constraint with delete cascade option as shown below. This will delete chind table rows related to master table rows when deleted.

    ALTER TABLE MasterTable
    ADD CONSTRAINT fk_xyz 
    FOREIGN KEY (xyz) 
    REFERENCES ChildTable (xyz) ON DELETE CASCADE 
    

提交回复
热议问题