Altering existing unique constraint
As in previous answer to change foreign key constraint
use steps:
Step 1: Drop old constraint:
ALTER TABLE `Message` DROP INDEX `user_id`;
Step 2: Add new:
ALTER TABLE `Message` ADD UNIQUE INDEX (
`user_id`,
`user_to`,
`top_num`,
`msg_type`);
Use SHOW CREATE TABLE to know name of constraint:
mysql> SHOW CREATE TABLE `Message` ;
| Message | CREATE TABLE `Message` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`user_to` int(11) NOT NULL,
`top_num` int(11) NOT NULL,
`priority` smallint(6) NOT NULL,
`error` varchar(120) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `user_id` (`user_id`,`user_to`,`top_num`)
-- ^^^^^^^^^ name
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
If you checks:
mysql> SHOW INDEX FROM `Message`;
Key_name
is user_id
that is first argument in UNIQUE (user_id ....
Suppose if you write:
ALTER TABLE `Message` ADD UNIQUE INDEX (
`user_to`,
`user_id`,
`top_num`,
`msg_type`);
Then you have to drop using user_to
as:
ALTER TABLE `Message` DROP INDEX `user_to`;
This is because you are adding unique index. Please first drop unique index and then add unique constraint.
-- you have to drop each index one by one.
ALTER TABLE Message DROP UNIQUE INDEX user_id;
and now add unique constraint.
ALTER TABLE Message ADD CONSTRAINT uc_message UNIQUE (`user_id`, `user_to`, `top_num`, `msg_type`);