Foreign Key
FOREIGN KEY says this column refers to a PRIMARY KEY (or UNIQUE) in another table. marks.roll REFERENCES students(roll). You cannot insert marks for roll 99 if there is no student 99. You cannot delete student 12 if marks still point to 12, unless ON DELETE CASCADE.
Needs InnoDB. Both columns same type. CREATE TABLE child (…, FOREIGN KEY (roll) REFERENCES students(roll));
ON DELETE CASCADE: delete parent, child rows go too. ON DELETE SET NULL: child roll becomes NULL. Default RESTRICT/NO ACTION: reject the delete.
Trap — MyISAM silently ignoring FK. Trap: CASCADE on a table you did not mean to empty.
On the example next to this theory — Foreign Key — keys and constraints protect data integrity; indexes speed lookups.
students.id ← PK (unique)
▲
│ FK
marks.student_idmarks.roll → students.roll. Cannot orphan a marks row.