Pages

Monday, April 6, 2009

Unique Constraint in SQL Server -- Part 3

In this article I would like to show you on how to find the Unique constraints defined on a table.



In my previous articles I explained about Unique Constraint creation at the Table Level and also at the Column Level.


How to query Unique Key Constraints Definition?

The below script will give you Constraint definition along with the constraint name.


SELECT TC.TABLE_NAME,CC.CONSTRAINT_NAME,CC.COLUMN_NAME
FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CC,
INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
WHERE TC.CONSTRAINT_TYPE = 'UNIQUE'
AND TC.CONSTRAINT_NAME = CC.CONSTRAINT_NAME
AND TC.TABLE_NAME = CC.TABLE_NAME




How to Drop Unique Constraints?

As always, to drop any object you need to find out the object name.

In this case ,I want to drop UC_ACCOUNT01 constraint defined on a ACCOUNT table.

ALTER TABLE ACCOUNT DROP CONSTRAINT UC_ACCOUNT01;

That’s it. It’s very simple.

No comments: