如何在DB2中查找约束列

时间:2015-07-07 14:36:56

标签: db2 constraints db2-luw

我想知道属于每个唯一约束的列(名称)。 我可以在 syscat.tabconst 中轻松找到约束和表名。 但是我找不到列名。

2 个答案:

答案 0 :(得分:3)

我猜你正在寻找索引中的唯一约束。这是我过去用来查看特定表的查询。您应该能够根据您的需求调整您的需求:

SELECT
     T.TABSCHEMA AS TABLE_SCHEMA
    ,T.TABNAME   AS TABLE_NAME
    ,CASE T.TYPE
        WHEN 'F' THEN 'Foreign Key'
        WHEN 'I' THEN 'Functional Dependency'
        WHEN 'K' THEN 'Check'
        WHEN 'P' THEN 'Primary Key'
        WHEN 'U' THEN 'Unique'
     END AS Type
    ,I.INDSCHEMA AS INDEX_SCHEMA
    ,I.INDNAME   AS INDEX_NAME
    ,U.COLNAME   AS COLUMN_NAME
    ,U.COLSEQ    AS COLUMN_ORDINAL
    ,CASE U.COLORDER 
        WHEN 'A' THEN 'Ascending'
        WHEN 'D' THEN 'Descending'
        WHEN 'I' THEN 'Included (unordered)'
     END AS COLUMN_SORRING
FROM SYSCAT.TABCONST T
JOIN SYSCAT.CONSTDEP C
  ON T.CONSTNAME = C.CONSTNAME
JOIN SYSCAT.INDEXES I 
  ON C.BSCHEMA = I.INDSCHEMA
 AND C.BNAME   = I.INDNAME
JOIN SYSCAT.INDEXCOLUSE U
  ON I.INDSCHEMA = U.INDSCHEMA
 AND I.INDNAME   = U.INDNAME
WHERE T.TABSCHEMA = @schema
  AND T.TABNAME   = @table
  AND C.BTYPE     = 'I' --Indexes Only
ORDER BY 
     T.TABSCHEMA
    ,T.TABNAME
    ,I.INDSCHEMA
    ,I.INDNAME
    ,U.COLSEQ

以下是所涉及表格的信息中心文章:

SYSCAT.TABCONST

SYSCAT.CONSTDEP

SYSCAT.INDEXES

SYSCAT.INDEXCOLUSE

答案 1 :(得分:0)

也许这有帮助。 SYSCAT.COLCHECKS

SYSCAT.CHECKS

Source