RDS for MySQL supports foreign key constraints. However, the following error message appears when you create a foreign key constraint.
Cannot add foreign key constraint
The field to be joined is not a primary key in the table to be joined.
The following takes a tstudent
table and a tscore
table as an example to describe how to resolve this issue.
show create table tstudent;
If a similar output is displayed, confirm that the tstudent
table does not have a primary key.
tscore
table has a normal structure:show create table tscore;
The following command output is returned.
tstudent
table:alter table tstudent add primary key(sno);
alter table tscore add constraint fk_tscore_sno foreign key(sno) references tstudent(sno);