Create Index Syntax

To add an index in SQL Server use the CREATE INDEX statements. When adding indexes remember there can only be one clustered index per table. The main options when creating an index are clutered or nonclustered or unique vs non unique. Using SQL 2005+ you can also specify columns to include at the leaf level of the index.

Create a single nonclustered index

[cc lang=”sql”]
CREATE UNIQUE NONCLUSTERED INDEX IX_NC_PresidentNumber — specify index name
ON dbo.Presidents (PresidentNumber) — specify table and column name
[/cc]

Create a multi-column (composite) nonclustered index

[cc lang=”sql”]
CREATE UNIQUE NONCLUSTERED INDEX IX_NC_PresidentNumber_PresidentName — specify index name
ON dbo.Presidents (PresidentNumber,PresidentName) — specify table and column names
[/cc]

Create a multi-column (composite) clustered index

[cc lang=”sql”]
CREATE UNIQUE CLUSTERED INDEX IX_C_PresidentNumber — specify index name
ON dbo.Presidents (PresidentNumber,PresidentName) — specify table and column names
[/cc]

Create a non clustered index with included columns

[cc lang=”sql”]
CREATE NONCLUSTERED INDEX IX_NC_PresidentNumber — specify index name
ON dbo.Presidents (PresidentNumber) — specify table and column name
INCLUDE (President,YearsInOffice,RatingPoints) — specify included columns
[/cc]

Create index with fill factor

[cc lang=”sql”]
CREATE NONCLUSTERED INDEX IX_NC_PresidentNumber — specify index name
ON dbo.Presidents (PresidentNumber) — specify table and column name
WITH (FILLFACTOR = 80) — specify the fill factor
[/cc]

SQL Server 2008 options

SQL Server 2008 provides additional index options. Most notably it provides the ability to filter an index. This can help decrease the size of the index for very large or partitioned table to only include the records that are normally queried.
[cc lang=”sql”]
CREATE NONCLUSTERED INDEX IX_NC_PresidentNumber
ON dbo.Presidents (PresidentNumber)
INCLUDE (President,YearsInOffice,RatingPoints)
WHERE ElectoralVotes IS NOT NULL
[/cc]
Another 2008 option is the ability to compress the contents of the index based on the row or the page.
[cc lang=”sql”]
CREATE NONCLUSTERED INDEX IX_NC_PresidentNumber
ON dbo.Presidents (PresidentNumber)
WITH ( DATA_COMPRESSION = ROW ) ;
[/cc]
Compress based on page:
[cc lang=”sql”]
CREATE NONCLUSTERED INDEX IX_NC_PresidentNumber
ON dbo.Presidents (PresidentNumber)
WITH ( DATA_COMPRESSION = PAGE ) ;
[/cc]

2 comments
Jeevan P 08 Apr 2014 at 11:24 am

Thank you Derek Dieter

Naveed 29 Aug 2015 at 7:52 am

شبنم می‌گه:شهروندان لیبی و حامیان معمر قذافی توسط شورشیان قتل عام شدند.A grisly scene was deoisvcred in Tripoli today, when reporters came across a former Gadhafi camp in central Tripoli where pro-regime fighters were found massacred, including a number that were bound before their execution.چندین نفر از کشته شدگان را در زمین بیمارستان پیدا کردند که برای سازمان ملل شوکه آور بود. اکنون غرب شیاد مکررا اعلام می کند که قذافی و شورشیان هر دو مرتب جنایت علیه بشریت شده اند ولی از جنایت ناتو/آمریکا سخنی بمیان نمی آورد.بنظر می رسد که غرب در نظر دارد از شورشیان تروریست حداکثر استفاده را بکند و بعد از غلبه کامل بر لیبی آنان را مانند طالبان با چسباندن مارک تروریست به قتل رساند. این کار توسط آمریکا ، انگلیس و فرانسه علیه مردم مسلمان مرتب در تاریخ تکرارشده است اما مسلمانان جاهل هنوز دوزاریشان نیافته است و همچنان با غرب علیه منافع خود همکاری دارند.باند مرتجع لاریجانی/توکلی مانند سران نوکرصفت عرب ازعاملین سازمان سیا/ ام آی 6 و موساد – شورشیان حمایت کردند.با وجود بمباران شدید لیبی توسط ناتو/ آمریکا، غرب هنوز نتوانسته است پایتخت را بزیر کنترل کامل خود در آورد.UN spokesman Rupert Colville promised an investigation into the killings, and urged rebel leaders to “take active steps to ensure that no crimes, or acts of revenge, are committed.”به سخنگوی سازمان ملل باید گفت: خفه شوید – خجالت بکشید . شما بیش از آنچه که فکر می کنید در دنیا افشا شده اید. مردم از بازی شما خسته شده اند. گورتان را گم کنید. شما در جنایت علیه بشریت با غرب شریک اید.Yet the growing concern about the behavior of the rebels hardly begins or ends with this single incident. Amnesty International is also reporting that the rebel faction has been conducting mass arrests of black people across the nation, terming all of them “foreign mercenaries” but with growing evidence that a large number were simply migrant workers.

Featured Articles

 Site Author

  • Thanks for visiting!
css.php