【问题标题】:How to get unique constraint names from SQL Server with C#如何使用 C# 从 SQL Server 获取唯一的约束名称
【发布时间】:2013-01-11 13:53:44
【问题描述】:

我使用以下语句在 SQL Server 数据库中创建了唯一约束:

ALTER TABLE mytable ADD CONSTRAINT mytable_unique UNIQUE (uid)

如何使用 C# SQLConnection 对象以编程方式获取所有唯一约束名称?

【问题讨论】:

  • 从 MSDN 你可以做一个简单的谷歌搜索get a list of all constraints Sql Server
  • 使用 ; 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 = '');去

标签: c# sql sql-server unique-constraint


【解决方案1】:

查询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

【讨论】:

    【解决方案2】:

    你也可以从sys.key_constraints获得这些:

    select name from sys.key_constraints where type = 'UQ'
    

    【讨论】:

      猜你喜欢
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      • 2010-09-09
      • 1970-01-01
      • 2013-01-01
      • 1970-01-01
      • 2013-11-01
      • 1970-01-01
      相关资源
      最近更新 更多