见这张表:
CREATE TABLE IF NOT EXISTS `default_messages_users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`message_id` int(11) NOT NULL,
`owner_user_id` int(11) NOT NULL,
`to_user_id` mediumint(8) NOT NULL
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;我只需要设置列message_id、owner_user_id、to_user_id的一个可能组合,这意味着组合(例如) 1,1,1只能存在一次,因此不允许重复。我是怎么做到的?在他们之间建立索引?
提前干杯和感谢
发布于 2012-08-23 13:36:45
一个UNIQUE约束(或者一个惟一的索引,mysql对约束和索引这两个概念的区别不大)可以根据需要由多个列(2、3、.)组成。示例中的约束由3列构成:
ALTER TABLE default_messages_users
ADD CONSTRAINT Unique__message__owner_user__to_user --- you choose a sensible name
--- for the constraint
UNIQUE message_id_owner_user_id_to_user_id_UQ --- and the index
(message_id, owner_user_id, to_user_id) ;https://dba.stackexchange.com/questions/23001
复制相似问题