MySQL DatabaseLesson 56Free preview
MySQL Foreign Key
This lesson is part of MySQL · All levels
Summary
FOREIGN KEY maintains the reference integrity by linking the columns in the child table to the primary key or unique key in the parent table. With this relationship, nonexistent referral data cannot be stored, such as orders for customers that have already been deleted. You can also set automatic actions by ON DELETE and ON UPDATE such as CASCADE or SET.
Key points
- Foreign key refers to the primary key or unique key in other tables
- The FK storage table is called the child table, the table referred to is called the parent table
- ON DELETE CASCADE deletes the child line when the parent is deleted
- ON DELETE SET NULL empties the child's FK column when the parent is deleted
- ON DELETE RESTRICT/NO ACTION refuses to remove the parent as long as it is still referred
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.