我想创建复合外键但它给我的SQL错误1005

时间:2015-02-13 10:03:34

标签: mysql composite

包含复合主键的表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;

1 个答案:

答案 0 :(得分:-1)

在complaint_comment表中的

,complaint_id是unsigned和int,但在complaint_record表中,complaint_id只是int

即两个字段的类型都不同。所以不能创建不同类型的外键。