Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 constraints

Author  Topic 

sandhyarao49
Starting Member

4 Posts

Posted - 2008-10-05 : 20:15:33


Hi,

I have a small doubt.

How to check the constraints(and constraints logic) defined on a particular table.


Thanks,
Sandhya

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-06 : 00:26:23
sp_help tablename will give you all constraint defined on table along with column details.


or use

select * from sys.objects o
left join sys.key_constraints kc on kc.parent_object_id = o.object_id
left join sys.check_constraints cc on cc.parent_object_id = o.object_id
left join sys.default_constraints dc on dc.parent_object_id = o.object_id
left join sys.foreign_keys fk on fk.parent_object_id = o.object_id
where OBJECT_NAME(o.object_id)=yourtablename
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-06 : 01:25:16
or use information_schema views

select ccu.COLUMN_NAME,ccu.CONSTRAINT_NAME,rc.CONSTRAINT_TYPE from information_schema.constraint_column_usage ccu
INNER JOIN information_schema.TABLE_CONSTRAINTS rc
ON rc.CONSTRAINT_NAME=ccu.CONSTRAINT_NAME
where ccu.table_name='YourTableName'
Go to Top of Page
   

- Advertisement -