简体   繁体   English

无法在表中添加两个外键

[英]Unable to add two foreign keys to a table

I have two tables as follow: 1st Table: 我有两个表,如下所示:第一个表:

CREATE TABLE User ( 
            User_ID VARCHAR(8)NOT NULL PRIMARY KEY,
            User_Name VARCHAR (25) NOT NULL,
            User_Gender CHAR (1) NOT NULL,
            User_Position VARCHAR (10) NOT NULL,
          );

2nd table: 第二表:

CREATE TABLE Training (
            Training_Code VARCHAR(8) NOT NULL Primary Key,
            Training_Title VARCHAR(30) NOT NULL,
            );

I am trying to create a table which has two foreign keys to join both of the previous tables: 我正在尝试创建一个表,该表具有两个外键以连接前面的两个表:

CREATE TABLE Request (
            User_ID VARCHAR(8) NOT NULL, 
            Training_Code VARCHAR(8) NOT NULL, 
            Request_Status INT(1) NOT NULL
            );

When I am trying to set the foreign keys in the new table, the User_ID can be done successfully but the Training_Code cannot be set to foreign key due to the error: 当我尝试在新表中设置外键时,可以成功完成User_ID,但由于错误而无法将Training_Code设置为外键:

ERROR 1215 (HY000): Cannot add foreign key constraint

As I searched for this problem, the reason for it, is that data type is not the same, or name is not the same.. but in my situation both are correct so could you tell me what is wrong here ? 当我搜索此问题时,其原因是数据类型不相同或名称不相同..但在我的情况下两者都是正确的,所以您能告诉我这里有什么问题吗?

You need an index for this column in table Request too: 您还需要在表Request中为此列创建索引:

Issue first 首先发行

CREATE INDEX idx_training_code ON Request (Training_Code);

Then you should be successful creating the foreign key constraint with 然后,您应该成功创建具有以下内容的外键约束

ALTER TABLE Request
ADD CONSTRAINT FOREIGN KEY idx_training_code (Training_Code) 
    REFERENCES Training(Training_Code);

It worked for me. 它为我工作。 But I've got to say that it worked without create index too, as the documentation of Using FOREIGN KEY Constraints states: 但是我不得不说,它也无需创建索引也可以工作,因为使用FOREIGN KEY Constraints的文档指出:

MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. MySQL需要在外键和引用键上建立索引,以便外键检查可以快速进行,而无需进行表扫描。 In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. 在引用表中,必须有一个索引,其中外键列以相同的顺序列为第一列。 Such an index is created on the referencing table automatically if it does not exist. 如果这样的索引不存在,则会在引用表上自动创建。 This index might be silently dropped later, if you create another index that can be used to enforce the foreign key constraint. 如果您创建另一个可用于强制外键约束的索引,则以后可能会静默删除该索引。 index_name, if given, is used as described previously. 如果给定,则使用index_name(如前所述)。

Emphasis by me. 我强调。 I don't know what's the issue in your case. 我不知道您的问题是什么。

Demo 演示

Explanation of the issue 问题说明

The behavior mentioned in the question can be reproduced if the table Training is using the MyISAM storage engine. 如果表Training使用MyISAM存储引擎,则可以重现问题中提到的行为。 Then creating a foreign key referencing the table Training will produce the mentioned error. 然后,创建一个引用表Training的外键将产生上述错误。

If there's data in the table, then simple dropping of the table would not be the best solution. 如果表中有数据,那么简单地删除表将不是最佳解决方案。 You can change the storage engine to InnoDB with 您可以使用以下命令将存储引擎更改为InnoDB

ALTER TABLE Training Engine=InnoDB;

Now you can successfully add the foreign key constraint. 现在您可以成功添加外键约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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