-1

I have created a unique constraint in an SQL Server Database using the following statement:

ALTER TABLE mytable ADD CONSTRAINT mytable_unique UNIQUE (uid)

How can I get all unique constraint names programmatically with C# SQLConnection object?

Stefanos Kargas
  • 10,547
  • 22
  • 76
  • 101
  • From MSDN you could have done a simple google search [get a list of all constraints Sql Server](http://msdn.microsoft.com/en-us/library/ms345522.aspx) – MethodMan Jan 11 '13 at 14:03
  • USE ; GO SELECT OBJECT_NAME(object_id) as constraint_name ,SCHEMA_NAME(schema_id) AS schema_name ,OBJECT_NAME(parent_object_id) AS table_name ,type_desc ,create_date ,modify_date ,is_ms_shipped ,is_published ,is_schema_published FROM sys.objects WHERE type_desc LIKE '%CONSTRAINT' AND parent_object_id = OBJECT_ID(''); GO – MethodMan Jan 11 '13 at 14:04

2 Answers2

3

query on information_schema.constraint_column_usage

SELECT  TC.Constraint_Name ,
        CC.Column_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
John Woo
  • 258,903
  • 69
  • 498
  • 492
3

You can also get these from sys.key_constraints:

select name from sys.key_constraints where type = 'UQ'
Paul Williams
  • 16,585
  • 5
  • 47
  • 82