【发布时间】:2015-02-13 10:03:34
【问题描述】:
具有复合主键的表 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 里面的表我要创建复合外键
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;
【问题讨论】:
-
请在问题中添加更多信息。你在哪里得到错误,你尝试了什么等等。
-
您是否尝试过理解该错误代码的含义?此外,您可能需要花更多的精力来格式化您的 SQL。
-
请在您使用的 sql server 中添加标签。
-
欢迎来到 StackOverflow:如果您发布代码、XML 或数据示例,请在文本编辑器中突出显示这些行并单击“代码示例”按钮 (
{ }) 在编辑器工具栏上很好地格式化和语法突出显示它! -
我想我已经为你找到了答案:stackoverflow.com/questions/17812616/…(不太难)