MySQL InnoDB 创建 FK 错误
我正在尝试创建一个自引用 FK:
DROP TABLE IF EXISTS `Company`;
CREATE TABLE `Company` (
`company_id` INTEGER(32) UNSIGNED AUTO_INCREMENT,
`parent_company_id` INTEGER(32),
PRIMARY KEY (`company_id`)
) ENGINE=InnoDB;
ALTER TABLE `Company`
ADD FOREIGN KEY `parent_company_id` REFERENCES `Company`(`company_id`);
我收到以下错误:
1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本对应的手册,了解在第 1 行 'REFERENCES
Company
(company_id
)' 附近使用的正确语法
I am trying to create a self-referential FK:
DROP TABLE IF EXISTS `Company`;
CREATE TABLE `Company` (
`company_id` INTEGER(32) UNSIGNED AUTO_INCREMENT,
`parent_company_id` INTEGER(32),
PRIMARY KEY (`company_id`)
) ENGINE=InnoDB;
ALTER TABLE `Company`
ADD FOREIGN KEY `parent_company_id` REFERENCES `Company`(`company_id`);
I am getting the following error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'REFERENCES
Company
(company_id
)' at line 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
知道了。
的定义
您必须更改
parent_company_id
INTEGER(32)以匹配company_id(自动增量除外),然后使用此语句
所以基本上记住在您用作 FK 的列上放置 unsigned ,以便它与 的定义匹配引用的键
Got it.
You gotta change the definition of
parent_company_id
INTEGER(32)To match company_id except for the autoincrement and then use this statement
So basically remember to put unsigned on the column you are using as FK so it matches the definition of the referenced key