mysql复合唯一索引_Mysql,添加复合唯一索引

CREATE TABLE `t_user` (

`id` varchar(11) NOT NULL,

`username` varchar(40) NOT NULL,

`password` varchar(40) NOT NULL,

`sex` varchar(4) NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

alter table t_user add unique index save_unique_index(username,password); -- 添加多字段唯一索引

CREATE TABLE `t_user` (

`id` varchar(11) NOT NULL,

`username` varchar(40) NOT NULL,

`password` varchar(40) NOT NULL,

`sex` varchar(4) NOT NULL,

PRIMARY KEY (`id`),

UNIQUE KEY `save_unique_index` (`username`,`password`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

1Tom123男

2Tom1234男

3Jerry234女

insert into t_user(id,username,password,sex) values('4','Tom','123','男'); -- Duplicate entry 'Tom-123' for key 'save_unique_index'

insert into t_user(id,username,password,sex) values('4','Tom','12345','男'); -- 受影响的行: 1

1Tom123男

2Tom1234男

3Jerry234女

4Tom12345男

你可能感兴趣的:(mysql复合唯一索引)