Friday 8 November 2013

Create a clustered index

Using SQL Server Management Studio

To create a clustered index by using Object Explorer

  1. In Object Explorer, expand the table on which you want to create a clustered index. 
  2. Right-click the Indexes folder, point to New Index, and select Clustered Index…. 
  3. In the New Index dialog box, on the General page, enter the name of the new index in the Index namebox. 
  4. Under Index key columns, click Add…. 
  5. In the Select Columns from table_name dialog box, select the check box of the table column to be added to the clustered index. 
  6. Click OK. 
  7. In the New Index dialog box, click OK. 
To create a clustered index by using the Table Designer
  1. In Object Explorer, expand the database on which you want to create a table with a clustered index.
  2. Right-click the Tables folder and click New Table…. 
  3. Create a new table as you normally would. For more information, see Create Tables (Database Engine). 
  4. Right-click the new table created above and click Design. 
  5. On the Table Designer menu, click Indexes/Keys. 
  6. In the Indexes/Keys dialog box, click Add. 
  7. Select the new index in the Selected Primary/Unique Key or Index text box.
  8. In the grid, select Create as Clustered, and choose Yes from the drop-down list to the right of the property. 
  9. Click Close. 
  10. On the File menu, click Save table_name. 

Using Transact-SQL


To create a clustered index
  1. In Object Explorer, connect to an instance of Database Engine.
  2. On the Standard bar, click New Query.
  3. Copy and paste the following example into the query window and click Execute.
  1. USE AdventureWorks2012;
    GO
    -- Create a new table with three columns.
    CREATE TABLE dbo.TestTable
        (TestCol1 int NOT NULL,
         TestCol2 nchar(10) NULL,
         TestCol3 nvarchar(50) NULL);
    GO
    -- Create a clustered index called IX_TestTable_TestCol1
    -- on the dbo.TestTable table using the TestCol1 column.
    CREATE CLUSTERED INDEX IX_TestTable_TestCol1 
        ON dbo.TestTable (TestCol1); 
    GO
    

Source: http://technet.microsoft.com/en-us/library/ms186342.aspx

No comments:

Post a Comment