Skip to content

Commit 335e5d8

Browse files
committed
Edit pass
1 parent 4f4305c commit 335e5d8

File tree

1 file changed

+146
-180
lines changed

1 file changed

+146
-180
lines changed
Lines changed: 146 additions & 180 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
---
22
title: "Enable Indexes and Constraints"
3-
description: Enable Indexes and Constraints
3+
description: Learn how to enable indexes and constraints in the SQL Server Database Engine.
44
author: MikeRayMSFT
55
ms.author: mikeray
6-
ms.date: "02/17/2017"
6+
ms.reviewer: randolphwest
7+
ms.date: 07/16/2025
78
ms.service: sql
89
ms.subservice: table-view-index
910
ms.topic: how-to
@@ -18,183 +19,148 @@ helpviewer_keywords:
1819
- "clustered indexes, enabling disabled indexes"
1920
monikerRange: "=azuresqldb-current || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current || =fabric"
2021
---
21-
# Enable Indexes and Constraints
22+
# Enable indexes and constraints
23+
2224
[!INCLUDE [SQL Server Azure SQL Database Azure SQL Managed Instance FabricSQLDB](../../includes/applies-to-version/sql-asdb-asdbmi-fabricsqldb.md)]
2325

24-
This topic describes how to enable a disabled index in [!INCLUDE[ssnoversion](../../includes/ssnoversion-md.md)] by using [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)] or [!INCLUDE[tsql](../../includes/tsql-md.md)]. After an index is disabled, it remains in a disabled state until it is rebuilt or dropped
25-
26-
**In This Topic**
27-
28-
- **Before you begin:**
29-
30-
[Limitations and Restrictions](#Restrictions)
31-
32-
[Security](#Security)
33-
34-
- **To enable a disabled index, using:**
35-
36-
[SQL Server Management Studio](#SSMSProcedure)
37-
38-
[Transact-SQL](#TsqlProcedure)
39-
40-
## <a name="BeforeYouBegin"></a> Before You Begin
41-
42-
### <a name="Restrictions"></a> Limitations and Restrictions
43-
44-
- After rebuilding the index, any constraints that were disabled because of disabling the index must be manually enabled. PRIMARY KEY and UNIQUE constraints are enabled by rebuilding the associated index. This index must be rebuilt (enabled) before you can enable FOREIGN KEY constraints that reference the PRIMARY KEY or UNIQUE constraint. FOREIGN KEY constraints are enabled by using the ALTER TABLE CHECK CONSTRAINT statement.
45-
46-
- Rebuilding a disabled clustered index cannot be performed when the ONLINE option is set to ON.
47-
48-
- When the clustered index is disabled or enabled and the nonclustered index is disabled, the clustered index action has the following results on the disabled nonclustered index.
49-
50-
|Clustered Index Action|Disabled Nonclustered Index ...|
51-
|----------------------------|-----------------------------------|
52-
|ALTER INDEX REBUILD.|Remains disabled.|
53-
|ALTER INDEX ALL REBUILD.|Is rebuilt and enabled.|
54-
|DROP INDEX.|Is enabled.|
55-
|CREATE INDEX WITH DROP_EXISTING.|Remains disabled.|
56-
57-
Creating a new clustered index, behaves the same as ALTER INDEX ALL REBUILD.
58-
59-
- Allowed actions on nonclustered indexes associated with a clustered index depend on the state, whether disabled or enabled, of both index types. The following table summarizes the allowed actions on nonclustered indexes.
60-
61-
|Nonclustered Index Action|When both the clustered and nonclustered indexes are disabled.|When the clustered index is enabled and the nonclustered index is in either state.|
62-
|-------------------------------|--------------------------------------------------------------------|----------------------------------------------------------------------------------------|
63-
|ALTER INDEX REBUILD.|The action fails.|The action succeeds.|
64-
|DROP INDEX.|The action succeeds.|The action succeeds.|
65-
|CREATE INDEX WITH DROP_EXISTING.|The action fails.|The action succeeds.|
66-
67-
- When rebuilding disabled compressed nonclustered indexes, data_compression will default to 'none', meaning that indexes will be uncompressed. This is due to compression settings metadata is lost when nonclustered indexes are disabled. To work around this you must specify explicit data compression in rebuild statement.
68-
69-
### <a name="Security"></a> Security
70-
71-
#### <a name="Permissions"></a> Permissions
72-
Requires ALTER permission on the table or view. If using DBCC DBREINDEX, user must either own the table; or be a member of the **sysadmin** fixed server role; or the **db_ddladmin** and **db_owner** fixed database roles.
73-
74-
## <a name="SSMSProcedure"></a> Using SQL Server Management Studio
75-
76-
#### To enable a disabled index
77-
78-
1. In Object Explorer, click the plus sign to expand the database that contains the table on which you want to enable an index.
79-
80-
2. Click the plus sign to expand the **Tables** folder.
81-
82-
3. Click the plus sign to expand the table on which you want to enable an index.
83-
84-
4. Click the plus sign to expand the **Indexes** folder.
85-
86-
5. Right-click the index you want to enable and select **Rebuild**.
87-
88-
6. In the **Rebuild Indexes** dialog box, verify that the correct index is in the **Indexes to rebuild** grid and click **OK**.
89-
90-
#### To enable all indexes on a table
91-
92-
1. In Object Explorer, click the plus sign to expand the database that contains the table on which you want to enable the indexes.
93-
94-
2. Click the plus sign to expand the **Tables** folder.
95-
96-
3. Click the plus sign to expand the table on which you want to enable the indexes.
97-
98-
4. Right-click the **Indexes** folder and select **Rebuild All**.
99-
100-
5. In the **Rebuild Indexes** dialog box, verify that the correct indexes are in the **Indexes to rebuild** grid and click **OK**. To remove an index from the **Indexes to rebuild** grid, select the index and then press the Delete key.
101-
102-
The following information is available in the **Rebuild Indexes** dialog box:
103-
104-
## <a name="TsqlProcedure"></a> Using Transact-SQL
105-
106-
#### To enable a disabled index using ALTER INDEX
107-
108-
1. In **Object Explorer**, connect to an instance of [!INCLUDE[ssDE](../../includes/ssde-md.md)].
109-
110-
2. On the Standard bar, click **New Query**.
111-
112-
3. Copy and paste the following example into the query window and click **Execute**.
113-
114-
```
115-
USE AdventureWorks2022;
116-
GO
117-
-- Enables the IX_Employee_OrganizationLevel_OrganizationNode index
118-
-- on the HumanResources.Employee table.
119-
120-
ALTER INDEX IX_Employee_OrganizationLevel_OrganizationNode ON HumanResources.Employee
121-
REBUILD;
122-
GO
123-
```
124-
125-
#### To enable a disabled index using CREATE INDEX
126-
127-
1. In **Object Explorer**, connect to an instance of [!INCLUDE[ssDE](../../includes/ssde-md.md)].
128-
129-
2. On the Standard bar, click **New Query**.
130-
131-
3. Copy and paste the following example into the query window and click **Execute**.
132-
133-
```
134-
USE AdventureWorks2022;
135-
GO
136-
-- re-creates the IX_Employee_OrganizationLevel_OrganizationNode index
137-
-- on the HumanResources.Employee table
138-
-- using the OrganizationLevel and OrganizationNode columns
139-
-- and then deletes the existing IX_Employee_OrganizationLevel_OrganizationNode index
140-
CREATE INDEX IX_Employee_OrganizationLevel_OrganizationNode ON HumanResources.Employee
141-
(OrganizationLevel, OrganizationNode)
142-
WITH (DROP_EXISTING = ON);
143-
GO
144-
```
145-
146-
#### To enable a disabled index using DBCC DBREINDEX
147-
148-
1. In **Object Explorer**, connect to an instance of [!INCLUDE[ssDE](../../includes/ssde-md.md)].
149-
150-
2. On the Standard bar, click **New Query**.
151-
152-
3. Copy and paste the following example into the query window and click **Execute**.
153-
154-
```
155-
USE AdventureWorks2022;
156-
GO
157-
-- enables the IX_Employee_OrganizationLevel_OrganizationNode index
158-
-- on the HumanResources.Employee table
159-
DBCC DBREINDEX ("HumanResources.Employee", IX_Employee_OrganizationLevel_OrganizationNode);
160-
GO
161-
```
162-
163-
#### To enable all indexes on a table using ALTER INDEX
164-
165-
1. In **Object Explorer**, connect to an instance of [!INCLUDE[ssDE](../../includes/ssde-md.md)].
166-
167-
2. On the Standard bar, click **New Query**.
168-
169-
3. Copy and paste the following example into the query window and click **Execute**.
170-
171-
```
172-
USE AdventureWorks2022;
173-
GO
174-
-- enables all indexes
175-
-- on the HumanResources.Employee table
176-
ALTER INDEX ALL ON HumanResources.Employee
177-
REBUILD;
178-
GO
179-
```
180-
181-
#### To enable all indexes on a table using DBCC DBREINDEX
182-
183-
1. In **Object Explorer**, connect to an instance of [!INCLUDE[ssDE](../../includes/ssde-md.md)].
184-
185-
2. On the Standard bar, click **New Query**.
186-
187-
3. Copy and paste the following example into the query window and click **Execute**.
188-
189-
```
190-
USE AdventureWorks2022;
191-
GO
192-
-- enables all indexes
193-
-- on the HumanResources.Employee table
194-
DBCC DBREINDEX ("HumanResources.Employee", " ");
195-
GO
196-
```
197-
198-
For more information, see [ALTER INDEX &#40;Transact-SQL&#41;](../../t-sql/statements/alter-index-transact-sql.md), [CREATE INDEX &#40;Transact-SQL&#41;](../../t-sql/statements/create-index-transact-sql.md), and [DBCC DBREINDEX &#40;Transact-SQL&#41;](../../t-sql/database-console-commands/dbcc-dbreindex-transact-sql.md).
199-
200-
26+
This article describes how to enable a disabled index in [!INCLUDE [ssnoversion](../../includes/ssnoversion-md.md)] by using [!INCLUDE [ssManStudioFull](../../includes/ssmanstudiofull-md.md)] or [!INCLUDE [tsql](../../includes/tsql-md.md)]. After an index is disabled, it remains in a disabled state until it rebuilds or is dropped.
27+
28+
## Limitations
29+
30+
After the index rebuilds, any constraints that were disabled because of disabling the index must be manually enabled. `PRIMARY KEY` and `UNIQUE` constraints are enabled by rebuilding the associated index. This index must be rebuilt (enabled) before you can enable `FOREIGN KEY` constraints that reference the `PRIMARY KEY` or `UNIQUE` constraint. `FOREIGN KEY` constraints are enabled by using the `ALTER TABLE CHECK CONSTRAINT` statement.
31+
32+
Rebuilding a disabled clustered index can't be performed when the `ONLINE` option is set to `ON`.
33+
34+
When the clustered index is disabled or enabled and the nonclustered index is disabled, the clustered index action has the following results on the disabled nonclustered index.
35+
36+
| Clustered index action | Disabled nonclustered index status |
37+
| --- | --- |
38+
| `ALTER INDEX REBUILD` | Remains disabled |
39+
| `ALTER INDEX ALL REBUILD` | Rebuilt and enabled |
40+
| `DROP INDEX` | Rebuilt and enabled |
41+
| `CREATE INDEX WITH DROP_EXISTING` | Remains disabled |
42+
43+
Creating a new clustered index behaves the same as `ALTER INDEX ALL REBUILD`.
44+
45+
Allowed actions on nonclustered indexes associated with a clustered index depend on the state, whether disabled or enabled, of both index types. The following table summarizes the allowed actions on nonclustered indexes.
46+
47+
| Nonclustered index action | When both the clustered and nonclustered indexes are disabled | When the clustered index is enabled and the nonclustered index is in either state |
48+
| --- | --- | --- |
49+
| `ALTER INDEX REBUILD` | The action fails | The action succeeds |
50+
| `DROP INDEX` | The action succeeds | The action succeeds |
51+
| `CREATE INDEX WITH DROP_EXISTING` | The action fails | The action succeeds |
52+
53+
When rebuilding disabled compressed nonclustered indexes, `data_compression` defaults to `none`, meaning that indexes are uncompressed. This is due to compression settings metadata is lost when nonclustered indexes are disabled. To work around this issue, you must specify explicit data compression in rebuild statement.
54+
55+
## Permissions
56+
57+
Requires `ALTER` permission on the table or view. If using `DBCC DBREINDEX`, you must either own the table or be a member of the **sysadmin** fixed server role, or a member of the **db_ddladmin** or **db_owner** fixed database roles.
58+
59+
## Use SQL Server Management Studio
60+
61+
### Enable a disabled index
62+
63+
1. In Object Explorer, select the plus sign to expand the database that contains the table on which you want to enable an index.
64+
65+
1. Select the plus sign to expand the **Tables** folder.
66+
67+
1. Select the plus sign to expand the table on which you want to enable an index.
68+
69+
1. Select the plus sign to expand the **Indexes** folder.
70+
71+
1. Right-click the index you want to enable and select **Rebuild**.
72+
73+
1. In the **Rebuild Indexes** dialog box, verify that the correct index is in the **Indexes to rebuild** grid and select **OK**.
74+
75+
### Enable all indexes on a table
76+
77+
1. In Object Explorer, select the plus sign to expand the database that contains the table on which you want to enable the indexes.
78+
79+
1. Select the plus sign to expand the **Tables** folder.
80+
81+
1. Select the plus sign to expand the table on which you want to enable the indexes.
82+
83+
1. Right-click the **Indexes** folder and select **Rebuild All**.
84+
85+
1. In the **Rebuild Indexes** dialog box, verify that the correct indexes are in the **Indexes to rebuild** grid and select **OK**. To remove an index from the **Indexes to rebuild** grid, select the index and then press the Delete key.
86+
87+
The following information is available in the **Rebuild Indexes** dialog box:
88+
89+
## Use Transact-SQL
90+
91+
[!INCLUDE [article-uses-adventureworks](../../includes/article-uses-adventureworks.md)]
92+
93+
### Enable a disabled index using ALTER INDEX
94+
95+
Execute the following Transact-SQL script. This example enables the `IX_Employee_OrganizationLevel_OrganizationNode` index on the `HumanResources.Employee` table.
96+
97+
```sql
98+
USE AdventureWorks2022;
99+
GO
100+
101+
ALTER INDEX IX_Employee_OrganizationLevel_OrganizationNode
102+
ON HumanResources.Employee REBUILD;
103+
GO
104+
```
105+
106+
### Enable a disabled index using CREATE INDEX
107+
108+
Execute the following Transact-SQL script. This example recreates the `IX_Employee_OrganizationLevel_OrganizationNode` index on the `HumanResources.Employee` table, using the `OrganizationLevel` and `OrganizationNode` columns, and then deletes the existing `IX_Employee_OrganizationLevel_OrganizationNode` index.
109+
110+
```sql
111+
USE AdventureWorks2022;
112+
GO
113+
114+
CREATE INDEX IX_Employee_OrganizationLevel_OrganizationNode
115+
ON HumanResources.Employee(OrganizationLevel, OrganizationNode) WITH (DROP_EXISTING = ON);
116+
GO
117+
```
118+
119+
### Enable a disabled index using DBCC DBREINDEX
120+
121+
> [!NOTE]
122+
> [!INCLUDE [ssnotedepfutureavoid-md](../../includes/ssnotedepfutureavoid-md.md)]
123+
124+
Execute the following Transact-SQL script. This example enables the `IX_Employee_OrganizationLevel_OrganizationNode` index on the `HumanResources.Employee` table.
125+
126+
```sql
127+
USE AdventureWorks2022;
128+
GO
129+
130+
DBCC DBREINDEX ("HumanResources.Employee", IX_Employee_OrganizationLevel_OrganizationNode);
131+
GO
132+
```
133+
134+
### Enable all indexes on a table using ALTER INDEX
135+
136+
Execute the following Transact-SQL script. This example enables all indexes on the `HumanResources.Employee` table.
137+
138+
```sql
139+
USE AdventureWorks2022;
140+
GO
141+
142+
ALTER INDEX ALL
143+
ON HumanResources.Employee REBUILD;
144+
GO
145+
```
146+
147+
### Enable all indexes on a table using DBCC DBREINDEX
148+
149+
> [!NOTE]
150+
> [!INCLUDE [ssnotedepfutureavoid-md](../../includes/ssnotedepfutureavoid-md.md)]
151+
152+
Execute the following Transact-SQL script. This example enables all indexes on the `HumanResources.Employee` table.
153+
154+
```sql
155+
USE AdventureWorks2022;
156+
GO
157+
158+
DBCC DBREINDEX ("HumanResources.Employee", " ");
159+
GO
160+
```
161+
162+
## Related content
163+
164+
- [ALTER INDEX (Transact-SQL)](../../t-sql/statements/alter-index-transact-sql.md)
165+
- [CREATE INDEX (Transact-SQL)](../../t-sql/statements/create-index-transact-sql.md)
166+
- [DBCC DBREINDEX (Transact-SQL)](../../t-sql/database-console-commands/dbcc-dbreindex-transact-sql.md)

0 commit comments

Comments
 (0)