Skip to content

Instantly share code, notes, and snippets.

@kdarty
Last active November 3, 2015 14:14
Show Gist options
  • Save kdarty/659b8e02e353e8da04db to your computer and use it in GitHub Desktop.
Save kdarty/659b8e02e353e8da04db to your computer and use it in GitHub Desktop.
The following SQL Script will retrieve a list of all "Unique" Constraints for a given Database. This is intended for use within SQL Server. Other Constraint Types that can be queried: Foreign Key and Primary Key
-- StackOverflow Discussion:
-- http://stackoverflow.com/questions/2675168/get-the-unique-constraint-columns-list-in-tsql
SELECT TC.CONSTRAINT_NAME,
CC.COLUMN_NAME,
TC.TABLE_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CC
ON TC.CONSTRAINT_NAME = CC.CONSTRAINT_NAME
WHERE TC.CONSTRAINT_TYPE = 'Unique'
ORDER BY TC.Constraint_Name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment