错误1215,无法添加外键约束

时间:2022-09-22 18:43:34

I know there are numerous questions regarding this error on here but I've searched through many and none seem to explain it in my case! I've created a table using the following code:

我知道这里有很多关于这个错误的问题,但是我已经找遍了很多,没有一个能解释我的情况!我使用以下代码创建了一个表:

CREATE TABLE Venue (
     venueID VARCHAR(20), 
     venueEmail VARCHAR(30) NOT NULL, 
     address VARCHAR(100), 
     phoneNo VARCHAR(20), 
     managerNo VARCHAR(20), 
     capacity INT(4), 
     PRIMARY KEY (venueEmail)
)ENGINE=InnoDB;

And am trying to create a table with a foreign key that refers to the first table using this code:

我正在尝试用外键创建一个表,该表使用此代码引用第一个表:

CREATE TABLE Concert (
     referenceNo VARCHAR(6), 
     venueEmail VARCHAR(30), 
     eventDate VARCHAR(10),
     startTime VARCHAR(5),
     ticketsSold INT(4),
     PRIMARY KEY (referenceNo), 
     FOREIGN KEY (venueEmail) REFERENCES Venue ON UPDATE CASCADE ON DELETE CASCADE
)ENGINE=InnoDB;

But it's giving me the 1215 error message!

但是它给了我1215错误信息!

1 个解决方案

#1


4  

Syntax wise this isn't correct.

语法方面,这是不正确的。

The issue's resolved here:

这个问题的解决:

CREATE TABLE Venue (
     venueID VARCHAR(20), 
     venueEmail VARCHAR(30) NOT NULL, 
     address VARCHAR(100), 
     phoneNo VARCHAR(20), 
     managerNo VARCHAR(20), 
     capacity INT(4), 
     PRIMARY KEY (venueEmail)
)ENGINE=InnoDB;

CREATE TABLE Concert (
     referenceNo VARCaHAR(6), 
     venueEmail VARCHAR(30), 
     eventDate VARCHAR(10),
     startTime VARCHAR(5),
     ticketsSold INT(4),
     PRIMARY KEY (referenceNo), 
     FOREIGN KEY (venueEmail) REFERENCES Venue(`venueEmail`) ON UPDATE CASCADE ON DELETE CASCADE
)ENGINE=InnoDB;

Note:

注意:

The column being referenced should be stated like table_name(column_name).

被引用的列应该声明为table_name(column_name)。

You missed the column_name part.

您错过了column_name部分。


REFERENCE

参考


More:

更多:

@Bill Karwin added the following useful info in the comment section:

@Bill Karwin在评论部分添加了以下有用的信息:

FWIW this is a MySQL idiosyncrasy. In standard SQL, if you omit the referenced column name, it defaults to the same name as the foreign key column. But InnoDB doesn't support this shortcut syntax—you must specify the column in both cases.

这是一个MySQL特性。在标准SQL中,如果省略了引用的列名,则默认为与外键列相同的名称。但是InnoDB不支持这个快捷语法——在这两种情况下都必须指定列。

#1


4  

Syntax wise this isn't correct.

语法方面,这是不正确的。

The issue's resolved here:

这个问题的解决:

CREATE TABLE Venue (
     venueID VARCHAR(20), 
     venueEmail VARCHAR(30) NOT NULL, 
     address VARCHAR(100), 
     phoneNo VARCHAR(20), 
     managerNo VARCHAR(20), 
     capacity INT(4), 
     PRIMARY KEY (venueEmail)
)ENGINE=InnoDB;

CREATE TABLE Concert (
     referenceNo VARCaHAR(6), 
     venueEmail VARCHAR(30), 
     eventDate VARCHAR(10),
     startTime VARCHAR(5),
     ticketsSold INT(4),
     PRIMARY KEY (referenceNo), 
     FOREIGN KEY (venueEmail) REFERENCES Venue(`venueEmail`) ON UPDATE CASCADE ON DELETE CASCADE
)ENGINE=InnoDB;

Note:

注意:

The column being referenced should be stated like table_name(column_name).

被引用的列应该声明为table_name(column_name)。

You missed the column_name part.

您错过了column_name部分。


REFERENCE

参考


More:

更多:

@Bill Karwin added the following useful info in the comment section:

@Bill Karwin在评论部分添加了以下有用的信息:

FWIW this is a MySQL idiosyncrasy. In standard SQL, if you omit the referenced column name, it defaults to the same name as the foreign key column. But InnoDB doesn't support this shortcut syntax—you must specify the column in both cases.

这是一个MySQL特性。在标准SQL中,如果省略了引用的列名,则默认为与外键列相同的名称。但是InnoDB不支持这个快捷语法——在这两种情况下都必须指定列。