List all CONSTRAINT of database or table in Sql Server - Free Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server,WCF,Entity framework snippets and tutorial

List all CONSTRAINT of database or table in Sql Server

Introduction: In this article today I am going to explain how we can list all CONSTRAINT of database or table in Sql Server

Description:
When we are working on database sometimes we need to check or get the CONSTRAINT on database or table. Using below given query we can get the CONSTRAINT of table or database quickly.

Query to list all fields of sys.objects:
SELECT * FROM sys.objects WHERE type_desc LIKE '%CONSTRAINT'

Using above query we get all fields of sys.objects.

To get only Constraint name, tabe name and Constraint type I use the refined query mention below:
SELECT OBJECT_NAME(object_id) AS ConstraintName,
SCHEMA_NAME(schema_id) AS SchemaName,
OBJECT_NAME(parent_object_id) AS TableName,
type_desc AS ConstraintType
FROM sys.objects
WHERE type_desc LIKE '%CONSTRAINT'

List all CONSTRAINT of database


To get the Constraint detail of a particular table use the below given query:
SELECT OBJECT_NAME(object_id) AS ConstraintName,
SCHEMA_NAME(schema_id) AS SchemaName,
type_desc AS ConstraintType
FROM sys.objects
WHERE type_desc LIKE '%CONSTRAINT' AND OBJECT_NAME(parent_object_id)='Student_Register'
List all CONSTRAINT of database
Replace the Student_Register with your database table name.


What do you think about this article?

If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.

Share this

Share on FacebookTweet on TwitterPlus on Google+


EmoticonEmoticon