唯一索引
2024年06月12日
一、认识
唯一索引确保索引中的值是唯一的,不允许有重复值。
二、创建索引
2.1 create unique index [indexName] on [tableName] ([columnName1][asc|desc], [columnName2][asc|desc], ……)
create unique index [indexName] on [tableName] ([columnName1][asc|desc], [columnName2][asc|desc], ……);
2.2 alter table [tableName] add constraint [uniqueConstraintName] unique ([columnName1][asc|desc], [columnName2][asc|desc], ……);
alter table [tableName] add constraint [uniqueConstraintName] unique ([columnName1][asc|desc], [columnName2][asc|desc], ……);
2.3 create table [tableName] (……, constraint [indexName] unique ([columnName1][asc|desc], [columnName2][asc|desc], ……) )
create table [tableName] (
column1 dateType,
column2 dateType,
constraint [indexName] unique ([columnName1][asc|desc], [columnName2][asc|desc], ……)
)
三、删除索引
3.1 drop index [indexName] on [tableName]
drop index [indexName] on [tableName];
3.2 alter table [tableName] drop index [indexName]
alter table [tableName] drop index [indexName];
四、查看索引
4.1 show index from [tableName]\G
show index from [tableName]\G;