gpt4 book ai didi

mysql - 为什么我不能以这种方式添加外键约束?

转载 作者:行者123 更新时间:2023-11-29 13:45:33 25 4
gpt4 key购买 nike

表格:

    CREATE TABLE `relation` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(40) NOT NULL,
`gender` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_relation` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

CREATE TABLE `invite` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`date_sent` date NOT NULL,
`user_id` int(10) unsigned NOT NULL,
`relation_id` int(10) unsigned NOT NULL,
`email` varchar(255) NOT NULL,
`code` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_user` (`user_id`),
CONSTRAINT `fk_user` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8

执行的SQL语句是:

ALTER TABLE `invite` 
ADD CONSTRAINT `fk_relation`
FOREIGN KEY (`relation_id`)
REFERENCES `relation` (`id`)
ON DELETE CASCADE ON UPDATE RESTRICT

Mysql 错误:

SQLSTATE[HY000]:一般错误:1005 无法创建表“dbtest.#sql-d00_39”(错误号:121)。

relation.idinvite.relation_id 列的类型相同 int(10) unsigned

更新添加此键时,餐 table 邀请为空。表关系有 3 行。

最佳答案

试试这个:

ALTER TABLE invite
ADD CONSTRAINT fk_relation
FOREIGN KEY (relation_id)
REFERENCES relation(id)

根据doc语法正确SQL FOREIGN KEY Constraint

关于mysql - 为什么我不能以这种方式添加外键约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17472309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com