October 2022
Intermediate to advanced
380 pages
9h 35m
English
Standard SQL and most implementations support syntax to define a foreign key for a single column on the same line as the column. However, MySQL doesn’t support inline foreign key syntax. If you try to define a foreign key this way, you get no error, but the foreign key is not added to the table.[36]
| | CREATE TABLE Parent ( |
| | parent_id VARCHAR(10) PRIMARY KEY |
| | ); |
| | |
| | CREATE TABLE Child ( |
| | child_id INT PRIMARY KEY, |
| | parent_id VARCHAR(10) NOT NULL REFERENCES Parent(parent_id) |
| | ); |
If you subsequently view the table definition by running SHOW CREATE TABLE Child, you see the foreign key constraint is missing, as if you had not defined it at all.
Read now
Unlock full access