I have the following SQL script. After it runs the foreign key relationship is never enforced.
CREATE TABLE Country (
name varchar(40) NOT NULL,
abbreviation varchar(4) NOT NULL,
PRIMARY KEY (name)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE StateProvince (
countryName varchar(40) NOT NULL,
name varchar(100) NOT NULL,
abbreviation varchar(3) NOT NULL,
PRIMARY KEY (countryName,name)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
alter table StateProvince
add index FK_StateProvince_Country (countryName),
add constraint FK_StateProvince_Country
foreign key (countryName)
references Country (name);
Is this because of the composite primary key?
According to the MySQL docs on foreign keys, The MyISAM table engine does not support foreign keys. Instead it silently ignores them. Use InnoDB instead. Try this: