我不知道为什么我在创建数据库时出现此错误:
Executing SQL script in server
ERROR: Error 1215: Cannot add foreign key constraint
CREATE TABLE IF NOT EXISTS `CollegeDB`.`Eleve` (
`idPersonne` VARCHAR(15) NOT NULL,
`dateNaissance` DATE NULL,
`lieuNaissance` VARCHAR(45) NULL,
PRIMARY KEY (`idPersonne`),
INDEX `fk_Eleve_Personne1_idx` (`idPersonne` ASC),
CONSTRAINT `fk_Eleve_Personne1`
FOREIGN KEY (`idPersonne`)
REFERENCES `CollegeDB`.`Personne` (`idPersonne`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 10 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
我有表Personne和另外两个继承它的表:Eleve和Enseignant 在我的EER图表工作台模型中总共有10个表,任何想法? 非常感谢
表人物:
CREATE TABLE IF NOT EXISTS `CollegeDB`.`Personne` (
`idPersonne` VARCHAR(15) NOT NULL,
`nom` VARCHAR(45) NULL,
`prenom` VARCHAR(45) NULL,
`sexe` VARCHAR(45) NULL,
`statut` VARCHAR(45) NULL,
`rue` VARCHAR(45) NULL,
`image` VARCHAR(45) NULL,
`cp` INT NULL,
PRIMARY KEY (`idPersonne`),
INDEX `cp_idx` (`cp` ASC),
CONSTRAINT `cp`
FOREIGN KEY (`cp`)
REFERENCES `CollegeDB`.`Commune` (`cp`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = big5
PACK_KEYS = Default;
答案 0 :(得分:0)
您需要在Personne.idPersonne上创建索引。
来自http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html:
MySQL要求外键和引用键上的索引 外键检查可以很快,不需要进行表扫描。