What you're asking for is a foreign key constraint. You'd need to use InnoDB - quote:
For storage engines other than InnoDB, MySQL Server parses the FOREIGN KEY syntax in CREATE TABLE statements, but does not use or store it.
To add a foreign key constraint within the CREATE TABLE statement for PERSONS
:
FOREIGN KEY (surname) REFERENCES names(surnames)
Using an ALTER TABLE statement if the tables already exist:
ALTER TABLE persons
ADD CONSTRAINT FOREIGN KEY (surname) REFERENCES names(surname)
Be aware that if you use the ALTER TABLE
statement, the data in the PERSONS
table can only contain surname values that exist in the NAMES.surname
table - it can not be applied until after the data has been fixed.