ALTER TABLE My_Child_Table ADD CONSTRAINT FK_User_IDThe last two lines are important. In this example, if the user id in the primary table is modified or deleted, the change will cascade to the child table. This ensures consistency between the two tables.
FOREIGN KEY ( User_ID_In_Child_Table )
REFERENCES My_Primary_Table ( User_ID_In_Primary_Table )
ON DELETE CASCADE
ON UPDATE CASCADE
As always, this works on SQL Server. Check your documentation for mySql, Oracle, etc.
No comments:
Post a Comment