Microsoft SQL Server uses T-SQL (Transact SQL) to access database objects and manipulate data in the Database. If we create one Table and forgot to put a foreign key constraint including CASCADE options on a field that is a foreign key of other Table, we can use the following CODE.
ALTER TABLE table_name
WITH NOCHECK ADD CONSTRAINT fk_constraint_name
[ON DELETE CASCADE]
[ON UPDATE CASCADE]
For example, we have existing Tables as below:
student(id, stname, gender)
is a child table of student
but we forgot to put a foreign key constraint on field [id] in studentphone
. To add a foreign key constraint on field [id] with CASCADE options in Table studentphone
please see the code below.
ALTER TABLE studentphone
WITH NOCHECK ADD CONSTRAINT fk_student_phone
ON DELETE CASCADE
ON UPDATE CASCADE