2010年5月28日 星期五

DB2: Select table Primary key and Foreign Key

Primary key

SELECT NAME
FROM
SYSIBM.SYSCOLUMNS
WHERE TBNAME = 'table name'
AND TBCREATOR = 'creator'
AND KEYSEQ > 0
ORDER BY KEYSEQ ASC

Foreign Key

SELECT B.REFTBNAME AS PARENTTABLE,COLNAME
FROM
SYSIBM.SYSFOREIGN KEYS A,
SYSIBM.SYSRELS B
WHERE A.RELNAME = B.RELNAME
AND B.TBNAME = 'table name'
AND B.REFTBCREATOR = 'creator'
AND B.REFTBCREATOR = A.CREATOR

沒有留言:

張貼留言