How to get all constraint on table?

Shankar Modi picture Shankar Modi · Jan 2, 2015 · Viewed 7.5k times · Source

I want to store tables metadata into other table.So how to get all constraint and its type like whether it primary , unique or foreign key in MS SQL server.

table_name constraint_name constraint_type

user pk_user_id PRIMARY_KEY

user_role fk_role FOREIGN_KEY

user unique_email UNIQUE_KEY

Answer

knkarthick24 picture knkarthick24 · Jan 2, 2015

USE INFORMATION_SCHEMA.TABLE_CONSTRAINTS

SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE TABLE_NAME='tablename'