Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL错误1005:外键约束格式不正确_Mysql_Sql_Database - Fatal编程技术网

MySQL错误1005:外键约束格式不正确

MySQL错误1005:外键约束格式不正确,mysql,sql,database,Mysql,Sql,Database,我使用MySQL处理一个数据库,我有一个主日历表,它与另一个日历表共享一些列(年和月),我成功地创建了一些主键,但当我创建外键时,我出现了以下错误: ERROR 1005 (HY000) at line 33: Can't create table `testDB`.`astreinte_mensu` (errno: 150 "Foreign key constraint is incorrectly formed") 详细错误输出: Cannot find an index in the r

我使用MySQL处理一个数据库,我有一个主日历表,它与另一个日历表共享一些列(年和月),我成功地创建了一些主键,但当我创建外键时,我出现了以下错误:

ERROR 1005 (HY000) at line 33: Can't create table `testDB`.`astreinte_mensu`
(errno: 150 "Foreign key constraint is incorrectly formed")
详细错误输出:

Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
日期表:

CREATE TABLE date(
   dateID int NOT NULL,
   annee int NOT NULL,
   mois int NOT NULL,
   semaine int NOT NULL,
   jour int NOT NULL,
   CONSTRAINT PK_date PRIMARY KEY (dateID,annee,mois,semaine,jour)
) Engine=innoDB;
阿斯特里恩特•门苏表:

CREATE TABLE astreinte_mensu(
   annee int,
   mois int,
   Personne1Nuit int,
   Personne2Nuit int,
   Personne1Jour int,
   Personne2Jour int,
   FOREIGN KEY (annee) REFERENCES date(annee),
   FOREIGN KEY (mois) REFERENCES date(mois),
   FOREIGN KEY (Personne1Nuit) REFERENCES contact(PersonID),
   FOREIGN KEY (Personne2Nuit) REFERENCES contact(PersonID),
   FOREIGN KEY (Personne1Jour) REFERENCES contact(PersonID),
   FOREIGN KEY (Personne2Jour) REFERENCES contact(PersonID)
) Engine=innoDB;
我已经检查过日期表是在另一个表之前创建的 并且这些列来自相同的数据类型

有人知道可能出了什么问题吗


提前感谢

MySQL中的外键必须引用另一个表中具有索引(理想情况下是唯一索引)的列。这通常发生在另一列是主键时,但只要有索引,它也不能是主键。在当前代码中,您从五列中创建了主键:

CONSTRAINT PK_date PRIMARY KEY (dateID, annee, mois, semaine, jour)
但所有这一切意味着,这五种价值观的每一种组合都必须是唯一的。这并不意味着任何一列都有索引

我在您的代码中看到的直接问题是,您指的是
astreinte\u mensu
表中的
annee
mois
。我们可以尝试向这些列添加唯一的约束。以下是一个可能有效的代码版本:

CREATE TABLE date (
    dateID int NOT NULL,
    annee int NOT NULL,
    mois int NOT NULL,
    semaine int NOT NULL,
    jour int NOT NULL,
    CONSTRAINT PK_date PRIMARY KEY (dateID, annee, mois, semaine, jour),
    UNIQUE KEY idx_annee (annee),
    UNIQUE KEY idx_mois (mois)
) Engine=innoDB;

CREATE TABLE astreinte_mensu (
    annee int,
    mois int,
    Personne1Nuit int,
    Personne2Nuit int,
    Personne1Jour int,
    Personne2Jour int,
    FOREIGN KEY (annee) REFERENCES date(annee),
    FOREIGN KEY (mois) REFERENCES date(mois),
    FOREIGN KEY (Personne1Nuit) REFERENCES contact(PersonID),
    FOREIGN KEY (Personne2Nuit) REFERENCES contact(PersonID),
    FOREIGN KEY (Personne1Jour) REFERENCES contact(PersonID),
    FOREIGN KEY (Personne2Jour) REFERENCES contact(PersonID)
 ) Engine=innoDB;

我假设
contact
表有一列
PersonID
,该列是唯一的。否则,这将是另一个错误原因。

外键必须引用主键或具有唯一约束的列。你的代码没有这样做,它在做其他事情。@TimBiegeleisen我的代码就是这样做的!“annee”和“mois”列得到了主键约束(隐式地也是唯一的)…不,它们没有。您在所有五列上添加了一个集体唯一约束。与单个唯一约束不同。除此错误外,表名不能作为日期数据的日期bcoztype@Sivabalan奇怪的我已经使用了一个日期表,没有任何问题。也许是因为它是小写的?非常感谢!!!这对我来说已经很清楚了。是的,PersonID有唯一的限制,所以现在一切都很好:)让我知道这是否有效。如果没有,我可以改变它。@TimBiegeleisen。虽然您的第一条语句描述了一个最佳实践,但MySQL实际上并没有强制执行它。任何索引都足以作为外键引用。请参阅:。注意:我决不允许对外键关系使用非主键。我只是指出MySQL没有强制执行这一点(尽管其他数据库也有)。@GordonLinoff我不知道这一点:-)也许我在考虑其他数据库。让我更新一下。