Shankar Modi
Shankar Modi

Reputation: 101

How to get all constraint on table?

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

Upvotes: 9

Views: 23916

Answers (1)

knkarthick24
knkarthick24

Reputation: 3216

USE INFORMATION_SCHEMA.TABLE_CONSTRAINTS

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

Upvotes: 26

Related Questions