每行的索引值都是唯一的,不会重复的
(如果表创建了唯一约束,那么系统将自动创建唯一索引)
聚集索引相当于使用字典的拼音查找,因为聚集索引存储记录是物理上连续存在的。
非聚集索引就相当于字典的部首查找,非聚集索引是逻辑上的连续,物理上不连续
聚集索引在一个表中只能有一个,而非聚集索引一个表可以存放多个
语法:
Create [unique][clustered][nonclustered] Index index_name On table_name(列名)
unique 唯一索引
clustered 聚集索引
noclustered 非聚集索引
--创建唯一聚集索引 create unique clustered ' 表示创建唯一聚集索引 index uo_clu_stuno ‘----索引名称 on student(s_stuno) '--- 数据表名称(建立索引的列名) ---创建唯一非聚集索引 create unique nonclustered '--- 表示创建唯一非聚集索引 index uo_clu_stuno ' ----索引名称 on student(s_stuno) ---创建非聚集索引 create nonclustered index nonclu_index on student(s_stuno) ---创建唯一索引 create unique index Nonclu_index on student(s_stuno) 创建复合索引: --创建非聚集复合索引 create nonclustered index Index_StuNo_SName on Student(S_StuNo,S_Name) --创建非聚集复合索引,未指定默认为非聚集索引 create index Index_StuNo_SName on Student(S_StuNo,S_Name) with(drop_existing=on)
在实际应用当中有些列是适合创建索引的,同样也有一些列是不适合创建索引的
当表中的某列被频繁的用于查询,而且对该列进行排序可以创建成索引
EXEC index_stuno_sname
drop index table_name.index_stuno_sname
文章来源: Sql Server 索引的运用