简体   繁体   中英

1215 - Cannot add foreign key constraint

I actualy try to create a database but i have an error with the foreign key. Can you hepl me please ?

DROP TABLE IF EXISTS `City`;
CREATE TABLE  `City` (`id` int(11) NOT NULL AUTO_INCREMENT,
`idCountry` int(11) DEFAULT NULL,
`Name` char(35) CHARACTER SET latin1 NOT NULL DEFAULT '',
`CountryCode` char(3) CHARACTER SET latin1 NOT NULL DEFAULT '',
`District` char(20) CHARACTER SET latin1 NOT NULL DEFAULT '',
`Population` int(11) NOT NULL DEFAULT '0',
 PRIMARY KEY (`id`) USING BTREE,
 KEY `fk_constraint_city_country` (`idCountry`),
 CONSTRAINT `fk_constraint_city_country` FOREIGN KEY (`idCountry`) 
 REFERENCES `Country` (`id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=4080 DEFAULT CHARSET=utf8;

I have this error: MySQL said: Documentation 1215 - Cannot add foreign key constraint

DROP TABLE IF EXISTS `Country`;
CREATE TABLE  `Country` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Code` char(3) CHARACTER SET latin1 DEFAULT NULL,
`Name` char(52) CHARACTER SET latin1 NOT NULL DEFAULT '',
`Continent` enum('Asia','Europe','North   
 America','Africa','Oceania','Antarctica','South America') CHARACTER SET    
 latin1 NOT NULL DEFAULT 'Asia',
`Region` char(26) CHARACTER SET latin1 NOT NULL DEFAULT '',
`SurfaceArea` float(10,2) NOT NULL DEFAULT '0.00',
`IndepYear` smallint(6) DEFAULT NULL,
`Population` int(11) NOT NULL DEFAULT '0',
`LifeExpectancy` float(3,1) DEFAULT NULL,
`GNP` float(10,2) DEFAULT NULL,
`GNPOld` float(10,2) DEFAULT NULL,
`LocalName` char(45) CHARACTER SET latin1 NOT NULL DEFAULT '',
`GovernmentForm` char(45) CHARACTER SET latin1 NOT NULL DEFAULT '',
`HeadOfState` char(60) CHARACTER SET latin1 DEFAULT NULL,
`Capital` int(11) DEFAULT NULL,
`Code2` char(2) CHARACTER SET latin1 NOT NULL DEFAULT '',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=240 DEFAULT CHARSET=utf8;

and there is the country langage:

DROP TABLE IF EXISTS `CountryLanguage`;
CREATE TABLE  `CountryLanguage` (
`idCountry` int(11) NOT NULL DEFAULT '0',
`idLanguage` int(11) NOT NULL DEFAULT '0',
`IsOfficial` enum('T','F') CHARACTER SET latin1 NOT NULL DEFAULT 'F',
`Percentage` float(4,1) NOT NULL DEFAULT '0.0',
 PRIMARY KEY (`idCountry`,`idLanguage`) USING BTREE,
 KEY `fk_constraint_Language` (`idLanguage`),
 CONSTRAINT `fk_constraint_Country` FOREIGN KEY (`idCountry`) REFERENCES    
 `Country` (`id`),
 CONSTRAINT `fk_constraint_Language` FOREIGN KEY (`idLanguage`)    
 REFERENCES `Language` (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM