MYSQL创建唯一索引

来源:互联网 发布:虎牙直播点歌软件 编辑:程序博客网 时间:2024/05/01 13:08

MYSQL创建唯一索引

创建表时直接设置:

DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`stu_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`stu_id`),
UNIQUE KEY `UK_student_name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;

创建唯一索引:

create unique index UK_student_name on student (name);

建表后添加约束:

alter table student add constraint uk_student_name unique (name);

0 0