17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The CREATE INDEX Statement<br />

The CREATE INDEX statement does exactly what it sounds like — it creates an index on the specified<br />

table or view based on the stated columns.<br />

The syntax to create an index is somewhat drawn out and introduces several items that we haven’t<br />

really talked about up to this point:<br />

CREATE [UNIQUE] [CLUSTERED|NONCLUSTERED]<br />

INDEX ON ( [ASC|DESC] [,...n])<br />

INCLUDE ( [, ...n])<br />

[WITH<br />

[PAD_INDEX = { ON | OFF }]<br />

[[,] FILLFACTOR = ]<br />

[[,] IGNORE_DUP_KEY = { ON | OFF }]<br />

[[,] DROP_EXISTING = { ON | OFF }]<br />

[[,] STATISTICS_NORECOMPUTE = { ON | OFF }]<br />

[[,] SORT_IN_TEMPDB = { ON | OFF }]<br />

[[,] ONLINE = { ON | OFF }<br />

[[,] ALLOW_ROW_LOCKS = { ON | OFF }<br />

[[,] ALLOW_PAGE_LOCKS = { ON | OFF }<br />

[[,] MAXDOP = <br />

]<br />

[ON { | | DEFAULT }]<br />

There is legacy syntax available for many of these options, and so you may see that syntax put into use<br />

to support prior versions of <strong>SQL</strong> <strong>Server</strong>. That syntax is, however, considered deprecated and will be<br />

removed at some point. I highly recommend that you stay with the newer syntax where possible.<br />

Loosely speaking, this statement follows the same CREATE syntax<br />

that we’ve seen plenty of already (and will see even more of). The primary hitch in things is that we<br />

have a few intervening parameters that we haven’t seen elsewhere.<br />

Just as we’ll see with views in our next chapter, we do have to add an extra clause onto our CREATE<br />

statement to deal with the fact that an index isn’t really a stand-alone kind of object. It has to go together<br />

with a table or view, and we need to state the table that our column(s) are ON.<br />

After the ON () clause, everything is optional. You can mix<br />

and match these options. Many of them are seldom used, but some (such as FILLFACTOR) can have a<br />

significant impact on system performance and behavior, so let’s look at them one by one.<br />

ASC/DESC<br />

Chapter 9: <strong>SQL</strong> <strong>Server</strong> Storage and Index Structures<br />

There is a similar but sufficiently different syntax for creating XML indexes. That<br />

will be handled separately at the end of this section.<br />

These two allow you to choose between an ascending and a descending sort order for your index. The<br />

default is ASC, which is, as you might guess, ascending order.<br />

277

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!