SIGN UP MEMBER LOGIN:    
ARTICLE

Index in SQL Server 2008

Posted by Shirsendu Nandi Articles | SQL Server 2012 May 27, 2011
In this article I will describe how to make a Cluster Index, Unique Index and Normal Index in SQL Server 2008 Express editions.
Reader Level:

First see why index is useful. See the following query:

SELECT CustomerID,ContactName,CompanyName,City
FROM Customers
WHERE Country ='India'

The above query retrieves Customer information where country is India from the Customers table of the Master database.
Now there is no Index on this table, so the database engine performs a table scan and reads every row to check if Country is "USA". Suppose there are 100 rows of records. The database engine scans 13 rows and returns the matches.

So the performance will degrade.

Indexing means to fetch the data of a row using a proper pointer.

Now to create the "Cluster Index" in a particular table in SQL Server 2008:

Step 1:

Each table can have one and only one Clustered Index because the index is built on unique key columns. It stores the data rows in the table based on it's key values. The table having a clustered index is also called a clustered table.

Right-click your desired table and after that click design.

Index1.gif

Step 2:

Now right-click on the "CustId" of the Customers Table then click "Indexes/Click".

Index2.gif

Now a window will open.

Index3.gif

See the properties here. It's unique, correct? The index name is "pk_Customer". Created as cluster is also "Yes"
After that click Add.

You can write the query also. The query will be:

CREATE CLUSTERED INDEX PK_Customers on Customer(CustId)

Step 4:

Now it is time for creating the Unique Index.

Now right-click on the "Amount" column (for my case, I want to make this column a Unique Index) of the Customers Table and click "Indexes/Click".

Index4.gif

Step 5:

Again the same window will open.

Index5.gif

See the drop down list. There you can choose "Unique key" or "Index" type. And give the index name. After that click the "Add" button.

You can write the query also

CREATE UNIQUE INDEX UNQ_Amount ON Customer(Amount)

Now Suppose I want to Change the Index Name.So the query will be

EXEC sp_rename 'Customer. UNQ_Amount ', 'Amont'

So here the previous index name "UNQ_Amount" will change to "Amount".

Login to add your contents and source code to this article
share this article :
post comment
 

hi Shirsendu but i want to write a program in asp.net (c#) to create index on a particular column.so that i can create index at runtime.please help me

Posted by mahesh pardeshi Sep 11, 2011

thxxx yar.

Posted by Shirsendu Nandi Jun 01, 2011

nice article

Posted by bala reddy May 30, 2011

Thanks a lot bro...nice article

Posted by surender bhyan May 27, 2011
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor