You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can create clustered indexes on tables by using [!INCLUDE [ssManStudioFull](../../includes/ssmanstudiofull-md.md)] or [!INCLUDE [tsql](../../includes/tsql-md.md)]. With few exceptions, every table should have a clustered index. Besides improving query performance, a clustered index can be rebuilt or reorganized on demand to control table fragmentation. A clustered index can also be created on a view. (Clustered indexes are defined in the article [Clustered and nonclustered indexes](clustered-and-nonclustered-indexes-described.md).)
Clustered indexes are implemented in the following ways:
31
31
32
-
-**PRIMARY KEY and UNIQUE constraints**
32
+
-`PRIMARY KEY` and `UNIQUE` constraints**
33
33
34
34
When you create a `PRIMARY KEY` constraint, a unique clustered index on the column or columns is automatically created if a clustered index on the table doesn't already exist and you don't specify a unique nonclustered index. The primary key column can't allow `NULL` values.
35
35
36
36
When you create a `UNIQUE` constraint, a unique nonclustered index is created to enforce a `UNIQUE` constraint by default. You can specify a unique clustered index if a clustered index on the table doesn't already exist.
37
37
38
-
An index created as part of the constraint is automatically given the same name as the constraint name. For more information, see [Primary and Foreign Key Constraints](../tables/primary-and-foreign-key-constraints.md) and [Unique constraints and check constraints](../tables/unique-constraints-and-check-constraints.md).
38
+
An index created as part of the constraint is automatically given the same name as the constraint name. For more information, see [Primary and foreign key constraints](../tables/primary-and-foreign-key-constraints.md) and [Unique constraints and check constraints](../tables/unique-constraints-and-check-constraints.md).
39
39
40
40
-**Index independent of a constraint**
41
41
@@ -47,7 +47,7 @@ Clustered indexes are implemented in the following ways:
47
47
48
48
- If a clustered index is created on a heap with several existing nonclustered indexes, all the nonclustered indexes must be rebuilt so that they contain the clustering key value instead of the row identifier (RID). Similarly, if a clustered index is dropped on a table that has several nonclustered indexes, the nonclustered indexes are all rebuilt as part of the `DROP` operation. This process might take significant time on large tables.
49
49
50
-
The preferred way to build indexes on large tables is to start with the clustered index and then build any nonclustered indexes. Consider setting the `ONLINE` option to ON when you create indexes on existing tables. When set to ON, long-term table locks aren't held. This enables queries or updates to the underlying table to continue. For more information, see [Perform Index Operations Online](perform-index-operations-online.md).
50
+
The preferred way to build indexes on large tables is to start with the clustered index and then build any nonclustered indexes. Consider setting the `ONLINE` option to `ON` when you create indexes on existing tables. When set to `ON`, long-term table locks aren't held. This enables queries or updates to the underlying table to continue. For more information, see [Perform index operations online](perform-index-operations-online.md).
51
51
52
52
- The index key of a clustered index can't contain **varchar** columns that have existing data in the `ROW_OVERFLOW_DATA` allocation unit. If a clustered index is created on a **varchar** column and the existing data is in the `IN_ROW_DATA` allocation unit, subsequent insert or update actions on the column that would push the data off-row fail. To obtain information about tables that might contain row-overflow data, use the [sys.dm_db_index_physical_stats (Transact-SQL)](../system-dynamic-management-views/sys-dm-db-index-physical-stats-transact-sql.md) dynamic management function.
53
53
@@ -89,7 +89,7 @@ Requires `ALTER` permission on the table or view. User must be a member of the *
89
89
90
90
1. Select the new index in the **Selected Primary/Unique Key or Index** text box.
91
91
92
-
1. In the grid, select **Create as Clustered**, and choose **Yes** from the drop-down list to the right of the property.
92
+
1. In the grid, select **Create as Clustered**, and choose **Yes** from the dropdown list to the right of the property.
93
93
94
94
1. Select **Close**.
95
95
@@ -106,24 +106,24 @@ Requires `ALTER` permission on the table or view. User must be a member of the *
106
106
```sql
107
107
USE AdventureWorks2022;
108
108
GO
109
-
109
+
110
110
-- Create a new table with three columns.
111
111
CREATETABLEdbo.TestTable (
112
112
TestCol1 INTNOT NULL,
113
113
TestCol2 NCHAR(10) NULL,
114
114
TestCol3 NVARCHAR(50) NULL
115
115
);
116
116
GO
117
-
117
+
118
118
-- Create a clustered index called IX_TestTable_TestCol1
119
119
-- on the dbo.TestTable table using the TestCol1 column.
120
120
CREATE CLUSTERED INDEX IX_TestTable_TestCol1 ONdbo.TestTable (TestCol1);
121
121
GO
122
122
```
123
123
124
-
For more information, see [CREATE INDEX (Transact-SQL)](../../t-sql/statements/create-index-transact-sql.md).
124
+
For more information, see [CREATE INDEX](../../t-sql/statements/create-index-transact-sql.md).
You can create nonclustered indexes in [!INCLUDE [ssnoversion](../../includes/ssnoversion-md.md)] by using [!INCLUDE [ssManStudioFull](../../includes/ssmanstudiofull-md.md)] or [!INCLUDE [tsql](../../includes/tsql-md.md)]. A nonclustered index is an index structure separate from the data stored in a table that reorders one or more selected columns. Nonclustered indexes can often help you find data more quickly than searching the underlying table; queries can sometimes be answered entirely by the data in the nonclustered index, or the nonclustered index can point the [!INCLUDE [ssDE](../../includes/ssde-md.md)] to the rows in the underlying table. Generally, nonclustered indexes are created to improve the performance of frequently used queries not covered by the clustered index or to locate rows in a table without a clustered index (called a heap). You can create multiple nonclustered indexes on a table or indexed view.
Nonclustered indexes are implemented in the following ways:
32
35
33
-
-**UNIQUE constraints**
36
+
-`UNIQUE` constraints**
34
37
35
-
When you create a UNIQUE constraint, a unique nonclustered index is created to enforce a UNIQUE constraint by default. You can specify a unique clustered index if a clustered index on the table does not already exist. For more information, see [Unique Constraints and Check Constraints](../../relational-databases/tables/unique-constraints-and-check-constraints.md).
38
+
When you create a `UNIQUE` constraint, a unique nonclustered index is created to enforce a `UNIQUE` constraint by default. You can specify a unique clustered index if a clustered index on the table doesn't already exist. For more information, see [Unique constraints and check constraints](../tables/unique-constraints-and-check-constraints.md).
36
39
37
40
-**Index independent of a constraint**
38
41
39
-
By default, a nonclustered index is created if clustered is not specified. The maximum number of nonclustered indexes that can be created per table is 999. This includes any indexes created by PRIMARY KEY or UNIQUE constraints, but does not include XML indexes.
42
+
By default, a nonclustered index is created if clustered isn't specified. The maximum number of nonclustered indexes that can be created per table is 999. This includes any indexes created by `PRIMARY KEY` or `UNIQUE` constraints, but doesn't include XML indexes.
40
43
41
44
-**Nonclustered index on an indexed view**
42
45
43
-
After a unique clustered index has been created on a view, nonclustered indexes can be created. For more information, see [Create indexed views](../../relational-databases/views/create-indexed-views.md).
46
+
After a unique clustered index has been created on a view, nonclustered indexes can be created. For more information, see [Create indexed views](../views/create-indexed-views.md).
47
+
48
+
<aid="Security"></a>
49
+
50
+
### Security
51
+
52
+
<aid="Permissions"></a>
44
53
45
-
###<aname="Security"></a> Security
54
+
#### Permissions
46
55
47
-
#### <aname="Permissions"></a> Permissions
56
+
Requires `ALTER` permission on the table or view. User must be a member of the **sysadmin** fixed server role or the **db_ddladmin** and **db_owner** fixed database roles.
48
57
49
-
Requires ALTER permission on the table or view. User must be a member of the **sysadmin** fixed server role or the **db_ddladmin** and **db_owner** fixed database roles.
58
+
<aid="SSMSProcedure"></a>
50
59
51
-
## <aname="SSMSProcedure"></a> Using SQL Server Management Studio
60
+
<aid="using-sql-server-management-studio"></a>
52
61
53
-
### To create a nonclustered index by using the Table Designer
### To create a nonclustered index on a table using Transact-SQL
116
+
### Create a nonclustered index on a table using Transact-SQL
96
117
97
118
1. In **Object Explorer**, connect to an instance of [!INCLUDE [ssDE](../../includes/ssde-md.md)] with [!INCLUDE [sssampledbobject-md](../../includes/sssampledbobject-md.md)] installed. You can download [!INCLUDE [sssampledbobject-md](../../includes/sssampledbobject-md.md)] from [sample databases](../../samples/adventureworks-install-configure.md?view=sql-server-ver15&tabs=ssms&preserve-view=true).
98
119
99
120
1. On the Standard bar, select **New Query**.
100
121
101
122
1. Copy and paste the following example into the query window and select **Execute**.
102
123
103
-
```sql
104
-
USE AdventureWorks2022;
105
-
GO
106
-
-- Find an existing index named IX_ProductVendor_VendorID and delete it if found.
0 commit comments