Published on

July 28, 2016

How to Drop a Clustered Index on a Primary Key Column in SQL Server

When working with SQL Server, you may come across a situation where you need to drop a clustered index that is created on a primary key column. However, dropping a clustered index on a primary key column is not as simple as running a DROP INDEX script. In fact, it will throw an error if you try to do so.

The error message you will receive is: “An explicit DROP INDEX is not allowed on index ‘Table1.PK_Table1_Col1’. It is being used for PRIMARY KEY constraint enforcement.”

So, how do we drop a clustered index on a primary key column? The answer is actually quite simple. We need to drop the primary key constraint on the same column before we can drop the clustered index.

Let’s walk through an example to demonstrate this process. First, we will create a table with a primary key and a clustered index on the same column:


CREATE TABLE Table1(
    Col1 INT NOT NULL,
    Col2 VARCHAR(100),
    CONSTRAINT PK_Table1_Col1 PRIMARY KEY CLUSTERED (Col1 ASC)
);

Next, we can check if the table has a primary key and a clustered index on the same column using the following script:


-- Check the Name of Primary Key
SELECT name 
FROM sys.key_constraints  
WHERE type = 'PK'
    AND OBJECT_NAME(parent_object_id) = N'Table1';

-- Check the Clustered Index 
SELECT OBJECT_NAME(object_id), name
FROM sys.indexes 
WHERE OBJECTPROPERTY(object_id, 'IsUserTable') = 1
    AND type_desc='CLUSTERED'
    AND OBJECT_NAME(object_id) = N'Table1';

Now, if we try to drop the clustered index using the DROP INDEX script, we will receive an error:


-- Drop Clustered Index
DROP INDEX PK_Table1_Col1 ON Table1;

The error message will be the same as before: “An explicit DROP INDEX is not allowed on index ‘Table1.PK_Table1_Col1’. It is being used for PRIMARY KEY constraint enforcement.”

To successfully drop the clustered index, we need to drop the primary key constraint on the same column using the following script:


-- Drop Constraint
ALTER TABLE Table1
DROP CONSTRAINT PK_Table1_Col1;

After executing the above script, the primary key constraint will be dropped, and we can verify that the table no longer has a primary key or a clustered index using the following script:


-- Check the Name of Primary Key
SELECT name 
FROM sys.key_constraints  
WHERE type = 'PK'
    AND OBJECT_NAME(parent_object_id) = N'Table1';

-- Check the Clustered Index 
SELECT OBJECT_NAME(object_id), name
FROM sys.indexes 
WHERE OBJECTPROPERTY(object_id, 'IsUserTable') = 1
    AND type_desc='CLUSTERED'
    AND OBJECT_NAME(object_id) = N'Table1';

As you can see, both queries will return no results, indicating that the primary key and clustered index have been successfully dropped.

It is important to note that having a table without a clustered index or a primary key is not recommended, as they are critical elements for database integrity and performance. However, there may be certain scenarios where dropping the clustered index on a primary key column is necessary.

If you have any further questions or would like to learn more about this topic, please leave a comment below, and I will address them in a future blog post.

Thank you for reading!

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.