Does anyone know why I keep getting this error with this query. I'm stumped:
Error Code: 1215. Cannot add foreign key constraint
My Code:
CREATE TABLE countries (
id INT NOT NULL AUTO_INCREMENT,
cname VARCHAR(45) NOT NULL,
PRIMARY KEY (id)
) COMMENT='Country List';
CREATE TABLE members (
id INT NOT NULL AUTO_INCREMENT,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Salt VARCHAR(45),
PRIMARY KEY (id),
FOREIGN KEY (Salt) REFERENCES countries(cname)
) COMMENT='stuff';
A foreign key must reference a primary or unique key. For example, you could make
countries.cname
unique:Alternatively, you can drop the
salt
column and make the reference via theid
: