mysql 字段唯一_MySQL如何保证多字段唯一

mysql中有些表有时需要做一些字段的唯一约束,当然你也可以在insert前判断有无的方式来防止重复,如果不想额外增加代码来更灵活的实现一些字段的唯一约束,mysql提供了两种方式:

1.unique key

alter table xx

add unique key no_account(no,collection_account)

2.unique index

alter table xxadd unique no_account_index(no,collection_account);

`id` int(12) unsigned NOT NULL AUTO_INCREMENT,

`comment_id` int(12) NOT NULL,

`user_id` int(12) NOT NULL,

KEY `FK_t_praise_comment` (`comment_id`),

KEY `FK_t_praise_user` (`user_id`),

UNIQUE KEY `UK_praise` (`comment_id`,`user_id`)

)

mysql中有些表有时需要做一些字段的唯一约束,当然你也可以在insert前判断有无的方式来防止重复,如果不想额外增加代码来更灵活的实现一些字段的唯一约束,mysql提供了两种方式: 1.unique key alter table xx add unique key no_account(no,collection_account) 2.unique index alter table xxadd unique no_account_index(no,collection_account); `id` int(12) unsigned NOT NULL AUTO_INCREMENT, `comment_id` int(12) NOT NULL, `user_id` int(12) NOT NULL, KEY `FK_t_praise_comment` (`comment_id`), KEY `FK_t_praise_user` (`user_id`), UNIQUE KEY `UK_praise` (`comment_id`,`user_id`) )
经验分享 程序员 微信小程序 职场和发展