Rey*_*rPM 2 mysql database-design constraint mysql-5.5
看这张表:
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 ;
Run Code Online (Sandbox Code Playgroud)
我只需要设置 message_id、owner_user_id、to_user_id 列的一种可能组合,这意味着组合(例如)1,1,1 只能存在一次,因此不允许重复。我怎么做?在它们之间创建索引?
提前干杯和感谢
一个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) ;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2311 次 |
最近记录: |