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;

建表后添加1

create unique index UK_student_name on student (name);

建表后添加2

建表后添加约束

alter table student add constraint uk_student_name unique (name);

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM