Here’s my script:
create table Country
(
CountryId int primary key,
Name varchar(255)
);
create table Person
(
PersonId int primary key,
Name varchar(255),
FOREIGN KEY (CountryId) references Country(CountryId)
);
I’m transitioning from MS SQL and trying to get a grasp on MySQL and starting off with my typical hello world of Person->Country relationship to get a feel for foreign keys.
I’m getting this error on PHPMyAdmin:
SQL query:
CREATE TABLE Person(
PersonId INT PRIMARY KEY , Name VARCHAR( 255 ) , FOREIGN KEY (
CountryId ) REFERENCES Country( CountryId ) );MySQL said:
1072 – Key column ‘CountryId’ doesn’t exist in table
What newbie mistake am I making here?
That’s because you had not created the column in Person that would be used in the foreign key, thus,
Key column 'CountryId' doesn't exist in table. Here’s how you’d do it: