[英]Can't create table with foreign key - MySQL Error 150
我在理解为什么以下代码段出现MySQL代码错误150时遇到了一些麻烦:
-- Exercise Categories
CREATE TABLE Exercise_cat
(
ec_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(25),
PRIMARY KEY (ec_id)
);
-- This inserts fine
-- Exercise Descriptions
CREATE TABLE Exercise_desc
(
e_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(25),
ec_id INT NOT NULL,
cal_per_hour INT NOT NULL,
PRIMARY KEY (e_id),
FOREIGN KEY (ec_id) REFERENCES Excercise_cat(ec_id)
);
-- ERROR (foreign key constraint not formed)
我没有离开[1] [2]中的列名。
我有分号,就像MySQL希望我那样。
如何修复我的外键,以便创建表?
你的意思是Exercise_cat
不是Excercise_cat
CREATE TABLE Exercise_desc
(
e_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(25),
ec_id INT NOT NULL,
cal_per_hour INT NOT NULL,
PRIMARY KEY (e_id),
FOREIGN KEY (ec_id) REFERENCES Exercise_cat(ec_id)
);
您引用的Excercise_cat
表不存在 。 但是Exercise_cat存在。
因此,以下行应更正。
FOREIGN KEY (ec_id) REFERENCES Excercise_cat(ec_id)
^
|
+-- Remove this extra 'c'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.