In SQL Server, a constraint defines rules that data in a database must comply with. For example, you could have a UNIQUE
constraint applied to a column to ensure that any value inserted into that column is unique (i.e. no other row shares the same value).
If later on you need to remove that constraint, here’s how to do it using Transact-SQL.
The ALTER TABLE Statement
You can drop a constraint using the ALTER TABLE
statement.
Here’s an example where we drop a constraint called taskcode_unique
:
ALTER TABLE Tasks DROP CONSTRAINT taskcode_unique; GO
This is quite self-explanatory. It alters the table called Tasks
, by dropping (removing) the constraint called taskcode_unique
.
This can be quite handy if you ever need to drop a table that happens to have a constraint, as you’ll need to drop the constraint before dropping the table.