我想创建复合外键,但它给我的 sql 错误 1005
i want to create composite foreign key but it gives my sql error 1005
Table complaint_record
带复合主键
CREATE TABLE `complaint_record` (
`complaint_id` int(11) NOT NULL AUTO_INCREMENT,
`cat_id` int(10) unsigned NOT NULL,
`store_id` int(10) unsigned NOT NULL,
`user_id` int(10) unsigned DEFAULT NULL,
`complaint_data` varchar(300) NOT NULL,
`status` varchar(45) DEFAULT NULL,
`RCA` varchar(100) DEFAULT NULL,
`priority` int(11) DEFAULT NULL,
`assigned_to` varchar(45) DEFAULT NULL,
`date_submission` datetime DEFAULT NULL,
PRIMARY KEY (`complaint_id`,`store_id`,`cat_id`),
KEY `cat_id` (`cat_id`),
KEY `user_id` (`user_id`),
KEY `store_id` (`user_id`),
KEY `store_id_idx` (`store_id`),
CONSTRAINT `cat_id` FOREIGN KEY (`cat_id`) REFERENCES `complaint_categories` (`cat_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `store_id` FOREIGN KEY (`store_id`) REFERENCES `store_record` (`store_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
)
Complaint_comment
table里面我要创建复合外键
CREATE TABLE `complaint_comment` (
`user_id` int(10) unsigned NOT NULL,
`complaint_id` int(11) unsigned NOT NULL,
`store_id` int(10) unsigned NOT NULL,
`cat_id` int(10) unsigned NOT NULL,
`commented_on` datetime NOT NULL,
`comment` longtext NOT NULL,
KEY `user_id_comment` (`user_id`),
CONSTRAINT `user_id_comment` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
)
我正在创建外键
ALTER TABLE `kabliwala_db`.`complaint_comment` ADD CONSTRAINT `complaint_key` FOREIGN KEY `complaint_key` (`complaint_id`, `store_id`, `cat_id`)
REFERENCES `complaint_record` (`complaint_id`, `store_id`, `cat_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
在 complaint_comment table complaint_id 中是无符号和 int 但在 complaint_record table complaint_id 中只是 int
即两个字段的类型不同。所以不能创建不同类型的外键。
Table complaint_record
带复合主键
CREATE TABLE `complaint_record` (
`complaint_id` int(11) NOT NULL AUTO_INCREMENT,
`cat_id` int(10) unsigned NOT NULL,
`store_id` int(10) unsigned NOT NULL,
`user_id` int(10) unsigned DEFAULT NULL,
`complaint_data` varchar(300) NOT NULL,
`status` varchar(45) DEFAULT NULL,
`RCA` varchar(100) DEFAULT NULL,
`priority` int(11) DEFAULT NULL,
`assigned_to` varchar(45) DEFAULT NULL,
`date_submission` datetime DEFAULT NULL,
PRIMARY KEY (`complaint_id`,`store_id`,`cat_id`),
KEY `cat_id` (`cat_id`),
KEY `user_id` (`user_id`),
KEY `store_id` (`user_id`),
KEY `store_id_idx` (`store_id`),
CONSTRAINT `cat_id` FOREIGN KEY (`cat_id`) REFERENCES `complaint_categories` (`cat_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `store_id` FOREIGN KEY (`store_id`) REFERENCES `store_record` (`store_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
)
Complaint_comment
table里面我要创建复合外键
CREATE TABLE `complaint_comment` (
`user_id` int(10) unsigned NOT NULL,
`complaint_id` int(11) unsigned NOT NULL,
`store_id` int(10) unsigned NOT NULL,
`cat_id` int(10) unsigned NOT NULL,
`commented_on` datetime NOT NULL,
`comment` longtext NOT NULL,
KEY `user_id_comment` (`user_id`),
CONSTRAINT `user_id_comment` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
)
我正在创建外键
ALTER TABLE `kabliwala_db`.`complaint_comment` ADD CONSTRAINT `complaint_key` FOREIGN KEY `complaint_key` (`complaint_id`, `store_id`, `cat_id`)
REFERENCES `complaint_record` (`complaint_id`, `store_id`, `cat_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
在 complaint_comment table complaint_id 中是无符号和 int 但在 complaint_record table complaint_id 中只是 int
即两个字段的类型不同。所以不能创建不同类型的外键。