How to Drop a Column with a Constraint in SQL Server

This article demonstrates how to drop a column in a SQL Server database, when that column has an existing constraint (Hint: You need to drop the constraint first).

If you’ve ever tried to drop a column, say like this:

ALTER TABLE Tasks
    DROP COLUMN TaskCode;
GO

Only to get an error like this:

Msg 5074, Level 16, State 1, Line 1
The object 'taskcode_unique' is dependent on column 'TaskCode'.
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE DROP COLUMN TaskCode failed because one or more objects access this column.

The reason is that the column has an existing constraint, and you can’t drop the table when it has a constraint.

Therefore, you need to drop the constraint before dropping the table.

Drop the Constraint First

Here’s how to drop the constraint:

ALTER TABLE Tasks
    DROP CONSTRAINT taskcode_unique;
GO

Result:

Commands completed successfully.

Then Drop the Table

Now that you’ve dropped the constraint, you can go ahead and drop the table:

ALTER TABLE Tasks
    DROP COLUMN TaskCode;
GO

Result:

Commands completed successfully.