PHP Foreign keyCASCADE 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
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