SQL Server Index系列之二

Agenda

Index Structures

•Primary Key and Clustered Index

•Constraints and Indexes

•Syntax for creating and managing indexes

•Special Indexes

•Index Access Methods

•DMVs for Index tuning

 

 

Primary Key and Clustered Index

 

 

存在uniqueifier列的证据,见下例,注意两种情况下keycnt的值:

--Clustered_Unique if object_id('Clustered_Unique','U') is not null begin drop table Clustered_Unique; end create table Clustered_Unique ( Col1 int not null, Col2 char(4) not null ) go create unique clustered index Cl_unq_col1 on Clustered_Unique(Col1); SELECT first, indid, keycnt, name FROM sysindexes WHERE id = object_id ('Clustered_Unique'); --Clustered_Dupes if object_id('Clustered_Dupes', 'U') is not null begin drop table Clustered_Dupes; end create table Clustered_Dupes ( Col1 int not null, Col2 char(4) not null ) go create clustered index Cl_dupes_col1 on Clustered_Dupes(Col1); SELECT first, indid, keycnt, name FROM sysindexes WHERE id = object_id ('Clustered_Dupes');

 

Constraints and Indexes

1.When declare a PRIMARY KEY or UNIQUE constraint, a unique index is created.
2.Biggest difference between indexes created using the CREATE INDEX and indexes support constraints is in how to drop the index.

查看建立约束而产生的索引,若试图直接删除这些索引,将返回错误

if object_id('ConstraintsIndexes','U') is not null begin drop table ConstraintsIndexes; end create table ConstraintsIndexes ( Col1 int not null, Col2 char(2) ); go select * from sys.indexes where object_id = object_id('ConstraintsIndexes'); --Add Primary Key constraint alter table ConstraintsIndexes add constraint PK_ConstraintsIndexes_Col1 primary key (Col1); select * from sys.indexes where object_id = object_id('ConstraintsIndexes'); --Add Unique constraint alter table ConstraintsIndexes add constraint UNQ_ConstraintsIndexes_Col2 unique (Col2); select * from sys.indexes where object_id = object_id('ConstraintsIndexes'); --Drop Unique constraint drop index UNQ_ConstraintsIndexes_Col2 on ConstraintsIndexes;

你可能感兴趣的:(sql,server,constraints,object,table,null,methods)