获取唯一约束列列表(在SQL中)?

时间:2010-04-20 12:46:33

标签: sql-server tsql metadata constraints

我可以使用以下查询轻松获得唯一约束列表:

select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where CONSTRAINT_TYPE='UNIQUE'

但是如何获得每个唯一约束适用的列的列表?

6 个答案:

答案 0 :(得分:45)

Ed是正确的,列在约束列使用视图中公开,这是它的SQL。

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

答案 1 :(得分:28)

请参阅INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE

答案 2 :(得分:3)

SELECT *
FROM    sys.indexes i
        JOIN sys.index_columns ic
            ON i.index_id = ic.index_id
               AND i.object_id = ic.object_id
WHERE i.is_unique_constraint = 1;

答案 3 :(得分:0)

仅供mySQL用户参考, 使用以下查询可以实现同样的目的:

在表格上找到任何唯一约束

select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where CONSTRAINT_TYPE='UNIQUE' and table_name='db_my_table'

查找包含所有列的唯一列列表

select * from INFORMATION_SCHEMA.KEY_COLUMN_USAGE where CONSTRAINT_NAME='cons_name' and TABLE_NAME='db_my_table'

查找具有所需视图的唯一列列表

select CONSTRAINT_NAME,COLUMN_NAME,TABLE_NAME from INFORMATION_SCHEMA.KEY_COLUMN_USAGE where CONSTRAINT_NAME='cons_name' and TABLE_NAME='db_my_table'

答案 4 :(得分:0)

不断创新。
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE非常慢。

使用sys.indexes,联接表,模式,对象,并且作为一个额外的奖励,您不仅会获得唯一的约束,而且还会获得包括过滤器在内的唯一索引。

-- CREATE TABLE dbo.T_User( USR_ID int NOT NULL, USR_User nvarchar(256) NULL, USR_Status int NOT NULL );
-- ALTER TABLE dbo.T_User ADD CONSTRAINT UC_T_User_USR_User UNIQUE(USR_User)

-- IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'dbo.T_User') AND name = N'IX_T_User_USR_User')
-- CREATE UNIQUE INDEX IX_T_User_USR_User ON dbo.T_User(USR_User)
-- WHERE (USR_User IS NOT NULL AND USR_Status=(1))

SELECT 
     sch.name 
    ,st.name 
    ,i.name 
    ,i.has_filter 
    ,i.filter_definition 
    ,i.is_unique 
    ,i.is_primary_key 
    ,i.is_unique_constraint 

    ,CASE WHEN i.is_unique_constraint = 1 
        THEN N'ALTER TABLE ' + QUOTENAME(sch.name) + N'.' + QUOTENAME(st.name) + N' DROP CONSTRAINT ' + QUOTENAME(i.name) + N'; ' 
        ELSE N'DROP INDEX ' + QUOTENAME(i.name)  + N' ON ' + QUOTENAME(sch.name)  + '.' + QUOTENAME(st.name) + '; ' 
    END AS sql 
FROM sys.indexes AS i

INNER JOIN sys.index_columns AS ic 
    ON i.index_id = ic.index_id 
    AND i.object_id = ic.object_id 

INNER JOIN sys.tables AS st 
    ON st.object_id = i.object_id 

INNER JOIN sys.objects AS syso 
    ON syso.object_id = st.object_id 
    AND syso.is_ms_shipped = 0 

INNER JOIN sys.schemas AS sch 
    ON sch.schema_id = st.schema_id 

WHERE (1=1) 
AND NOT EXISTS  
(
    SELECT * 
    FROM sys.extended_properties AS xp 
    WHERE xp.minor_id = 0 
    AND xp.major_id = st.object_id 
    AND xp.name = 'microsoft_database_tools_support' 
)

-- AND sch.name = 'dbo' 
-- AND st.name = 'T_Benutzer'

AND 
(
    i.is_unique_constraint = 1 
    OR 
    (i.is_unique = 1 AND i.is_primary_key = 0)
)
;

答案 5 :(得分:0)

这是一个更好的解决方案,它以适当的排序顺序列出了约束列,并添加了ASC / DESC标志。它还允许按目录,架构或表名进行过滤。

       <select name="opt_lable">

            <?php foreach ($sidePosts as $post) {?>


            <option value="<?php echo $post['label'];?>"><?php echo $post['label'];?></option>
                <?php if($post['label']=='home'){
                    header('location:index.php');
                }elseif ($post['label']=='world') {
                   header('location:world.php');
                };?>


            <?php };?>

        </select>