How do I use cascade delete with SQL Server?

后端 未结 8 1219
抹茶落季
抹茶落季 2020-11-22 01:12

I have 2 tables: T1 and T2, they are existing tables with data. We have a one to many relationship between T1 and T2. How do I alter the table definitions to perform casca

8条回答
  •  耶瑟儿~
    2020-11-22 01:50

    Use something like

    ALTER TABLE T2
    ADD CONSTRAINT fk_employee
    FOREIGN KEY (employeeID)
    REFERENCES T1 (employeeID)
    ON DELETE CASCADE;
    

    Fill in the correct column names and you should be set. As mark_s correctly stated, if you have already a foreign key constraint in place, you maybe need to delete the old one first and then create the new one.

提交回复
热议问题