gpt4 book ai didi

db2 - 如何在 DB2 中查找约束列

转载 作者:行者123 更新时间:2023-12-03 16:06:48 26 4
gpt4 key购买 nike

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

最佳答案

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

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

关于db2 - 如何在 DB2 中查找约束列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31271694/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com