MySQL给字段唯一索引的三种方法

时间:2018-06-02 18:27:29   收藏:0   阅读:199

建表时添加

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;

建表后添加1

create unique index UK_student_name on student (name);

建表后添加2

建表后添加约束

alter table student add constraint uk_student_name unique (name);

 

原文:https://www.cnblogs.com/bigtreei/p/9126000.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!