gpt4 book ai didi

mysql - 无法在我的数据库中添加外键约束

转载 作者:行者123 更新时间:2023-11-29 23:17:59 26 4
gpt4 key购买 nike

我不知道为什么在创建数据库时出现此错误:

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;

最佳答案

您需要在 Personne.idPersonne 上创建索引。

来自http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html :

MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan.

关于mysql - 无法在我的数据库中添加外键约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27594080/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com