Foreign Key Constraints of Cascade strict and SET Null on delete and Update

Download database file for practice

Example of Cascade

 

PHP Foreign key
PHP Foreign key
CASCADE FOREIGN KEY
CASCADE FOREIGN KEY

 

 

This Example shows Cascade on delete and update operations.

Database name: t4tutorials

Table 1 : students (student_id, student_namne, T_id)

Table 2 : students (Teacher_id, Teacher_name)

student_id is primary key of table “students” and “T_id” is foreign key linked with primary key “Teacher_id” of “students” table. Teacher_id is primary key of table “students”

Suppose, If we delete a row from table “teachers” by using primary key, it will delete all related data and row from table “students” with the help of foreign key.

 


Example of Restrict

Restrict Foreign key
Restrict Foreign key

This Example shows restict on delete and update operations.

Database name: t4tutorials

Table 1 : students (student_id, student_namne, T_id)

Table 2 : students (Teacher_id, Teacher_name)

student_id is primary key of table “students” and “T_id” is foreign key linked with primary key “Teacher_id” of “students” table. Teacher_id is primary key of table “students”

Suppose, If we delete a row from table “teachers” by using primary key, it will not delete any data from table “students” because are restricting it with “restrict” feature.


Example of Set Null

This Example shows “set Null” on delete and update operations.

Database name: t4tutorials

Table 1 : students (student_id, student_namne, T_id)

Table 2 : students (Teacher_id, Teacher_name)

student_id is primary key of table “students” and “T_id” is foreign key linked with primary key “Teacher_id” of “students” table. Teacher_id is primary key of table “students”

Suppose, If we delete a row from table “teachers” by using primary key, it will not delete any data from table “students” because are restricting it with “restrict” feature.

When row is deleted from the “teachers” table , it will delete the related data from “students” table” and in “teachers” table, the foreign key attribute “T_id” value will be delete and Null will stay here as a value.

SET Null Foreign Key
SET Null Foreign Key