mysql 的唯一索引一般用於不重復的字段,一般會把表中的id設為唯一索引,創建唯一索引的目的不是為了提高查詢速度,而是為了避免數據重復,注意:唯一索引可以有多個,但是列值必須唯一,創建唯一索引使用關鍵字unique。
創建唯一索引
1,創建表的時候創建索引:
create table temp2(id int(10) not null auto_increment,title varchar(10) not null,cnt int(10) not null,primary key(`id`),index(`title`),unique key cnt(`cnt`)) engine=myisam;
2,添加索引
create unique index title on article(`title`);
3,
alter table article add index content(`content`);
主鍵索引是特殊的唯一索引