gpt4 book ai didi

mysql - MySQL 外键错误

转载 作者:行者123 更新时间:2023-11-29 22:35:34 25 4
gpt4 key购买 nike

我有两个表并尝试创建外键约束,但出现错误:

[SQL] ALTER TABLE `defectstdreference`
ADD CONSTRAINT `Relationship72` FOREIGN KEY (`improve_notice_doc_id`, `defect_id`, `client_id`) REFERENCES `improvementnoticedefect` (`doc_id`, `defect_id`, `client_id`);
[Err] 1215 - Cannot add foreign key constraint

这是我的表格:

CREATE TABLE `improvementnoticedefect` (
`defect_id` int(11) NOT NULL,
`doc_id` bigint(20) NOT NULL,
`client_id` bigint(20) NOT NULL,
`description` varchar(20000) NOT NULL,
PRIMARY KEY (`defect_id`,`doc_id`,`client_id`),
KEY `Relationship68` (`doc_id`,`client_id`),
CONSTRAINT `Relationship68` FOREIGN KEY (`doc_id`, `client_id`) REFERENCES `improvementnotice` (`doc_id`, `client_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

第二:

CREATE TABLE `defectstdreference` (
`reference_id` bigint(20) NOT NULL AUTO_INCREMENT,
`std_doc_id` bigint(20) NOT NULL,
`improve_notice_doc_id` bigint(20) NOT NULL,
`defect_id` int(11) NOT NULL,
`paragraph` varchar(4000) NOT NULL,
`client_id` bigint(20) NOT NULL,
PRIMARY KEY (`reference_id`),
KEY `Relationship70` (`std_doc_id`),
CONSTRAINT `Relationship70` FOREIGN KEY (`std_doc_id`) REFERENCES `std` (`doc_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

以及添加外键的查询:

ALTER TABLE `defectstdreference`
ADD CONSTRAINT `Relationship72` FOREIGN KEY (`improve_notice_doc_id`, `defect_id`, `client_id`) REFERENCES `improvementnoticedefect` (`doc_id`, `defect_id`, `client_id`);

实际上这个查询是由 SymmetricDS 通过 mysql jdbc 驱动程序进行的。

感谢任何帮助

最佳答案

我怀疑这可能是因为外键引用子句中的字段的顺序与主键不同。示例:

mysql> create table pk (id1 integer, id2 integer, id3 integer, primary key(id1, id2, id3));
Query OK, 0 rows affected (0.10 sec)

mysql> create table fk (id1 integer, id2 integer, id3 integer, foreign key(id1, id2, id3) references pk(id2, id1, id3));
ERROR 1005 (HY000): Can't create table 'test.fk' (errno: 150)
mysql> create table fk (id1 integer, id2 integer, id3 integer, foreign key(id1, id2, id3) references pk(id1, id2, id3));
Query OK, 0 rows affected (0.09 sec)

关于mysql - MySQL 外键错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29565413/

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