Search
Database

Clustered Indexes in SQL Server 2008

Published 2 min readBy Programming4us Editorial Team

An index can be defined using one or more columns in the table, the index key, with the following restrictions:

  • You can define an index with up to 16 columns

  • The maximum size of the index key is 900 bytes

The column(s) defined for the clustered index are referred to as the clustering key. A clustered index is special because it causes SQL Server to order the data in the table according to the clustering key. Because a table cannot be sorted more than one way, you can define only one clustered index on a table.

Note


The general syntax for creating a relational index is as follows:

CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX index_name
ON

You may recall the table creation scripts that we used in Article, “Designing Tables,” had a keyword of “clustered” in the specification of a primary key. Although a primary key is a constraint, SQL Server physically implements a primary key as an index. Because the default option for a primary key is clustered, SQL Server creates a clustered index for a primary key. Likewise, a unique constraint is physically implemented as a unique index. Since a primary key is also unique, by default, SQL Server physically implements each primary key as a unique, clustered index.

Note


In the practice that follows, you will create clustered indexes on the OrderHeader and OrderDetail tables previously created in article "Designing Tables". Since we have already created primary keys on the OrderHeader and OrderDetail tables using the default options, you will first drop and re-create the primary keys as nonclustered in order to create the single, allowed clustered index on the table.

Create a Clustered Index

1.

Execute the following code against the SQL2008SBS database:

-Must drop foreign key first, because it depends on the primary keyALTER TABLE Orders.
OrderDetail
DROP CONSTRAINT fk_orderheadertoorderdeatils
GO

-Change the existing primary keys to nonclustered
ALTER TABLE Orders.OrderHeader
DROP CONSTRAINT pk_orderheader
GO
ALTER TABLE Orders.OrderHeader
ADD CONSTRAINT pk_orderheader PRIMARY KEY NONCLUSTERED (OrderID)
GO
ALTER TABLE Orders.OrderDetail
DROP CONSTRAINT pk_orderdetail
GO
ALTER TABLE Orders.OrderDetail
ADD CONSTRAINT pk_orderdetail PRIMARY KEY NONCLUSTERED (OrderDetailID)
GO

-Recreate the foreign key (fix the misspelling from article "Designing Tables")
ALTER TABLE Orders.OrderDetail
ADD CONSTRAINT fk_orderheadertoorderdetails FOREIGN KEY (OrderID)
REFERENCES Orders.OrderHeader (OrderID)
GO
-Create new clustered indexes on the ShipDate/FinalShipDate columns
CREATE CLUSTERED INDEX icx_finalshipdate ON Orders.OrderHeader (FinalShipDate)
GO
CREATE CLUSTERED INDEX icx_shipdate ON Orders.OrderDetail (ShipDate)
GO


2.

In the Object Explorer, expand the Indexes node for the OrderHeader and OrderDetail tables. Observe that you now have a clustered index along with a unique, nonclustered index created against the table.