October 2022
Intermediate to advanced
380 pages
9h 35m
English
If the foreign key references a table before you have created that table, then you get an error.
| | CREATE TABLE Child ( |
| | child_id INT PRIMARY KEY, |
| | parent_id INT NOT NULL, |
| | FOREIGN KEY (parent_id) REFERENCES Parent(parent_id) |
| | ); |
| | |
| | CREATE TABLE Parent ( |
| | parent_id INT PRIMARY KEY |
| | ); |
The following error is returned as the first CREATE TABLE statement fails, before the second statement is run.
ERROR 1824 (HY000): Failed to open the referenced table ’Parent’
The order in which you create the tables is important. You must create the Parent table before you define a foreign key to reference it.
Read now
Unlock full access