Table complaint_record
with composite primary key
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 in it I want to create composite foreign key
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
)
I am creating foreign key
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;
in complaint_comment table complaint_id is unsigned and int but in complaint_record table complaint_id is only int
i.e type of both the fields are different. so can't create an foreign key with different type.