如何从derby db获取所有外键信息?

问题描述 投票:0回答:1

在 SQL 中,我通常会像这样获取外键信息...

SELECT
    TABLE_SCHEMA as fromSchema,
    TABLE_NAME as fromTable,
    COLUMN_NAME as fromColumn,
    REFERENCED_TABLE_SCHEMA as toSchema,
    REFERENCED_TABLE_NAME as toTable,
    REFERENCED_COLUMN_NAME as toColumn
FROM information_schema.key_column_usage col
where REFERENCED_COLUMN_NAME is not null
  and REFERENCED_TABLE_NAME is not null
  and REFERENCED_TABLE_SCHEMA is not null

但是如何从DERBY数据库中获取相同的信息呢? 我不知道如何获取列

toColumn
fromColumn
的信息??? 我扫描了整个德比文档,但我不知道如何获取外键列的名称。

select FS.SCHEMANAME as "fromScheme",
       FT.TABLENAME  as "fromTable",
       TS.SCHEMANAME as "toScheme",
       TT.TABLENAME  as "toTable"
       -- How to get toColumn???
       -- How to get fromColumn???
from sys.SYSFOREIGNKEYS FK
         join SYS.SYSCONSTRAINTS S0 on FK.CONSTRAINTID = S0.CONSTRAINTID
         join SYS.SYSCONSTRAINTS S1 on FK.KEYCONSTRAINTID = S1.CONSTRAINTID
         join sys.SYSTABLES FT on S0.TABLEID = FT.TABLEID
         join sys.SYSSCHEMAS FS on FS.SCHEMAID = S0.SCHEMAID
         join sys.SYSTABLES TT on S1.TABLEID = TT.TABLEID
         join sys.SYSSCHEMAS TS on TS.SCHEMAID = S1.SCHEMAID;
database derby
1个回答
0
投票

sys
数据库中,您有以下字段:

  • sysconstraints.constraintid
  • sysforeignkeys.constraintid
  • sysforeignkeys.conglomerateid
  • sysconglomerates.conglomerateid
  • sysconglomerates.tableid
  • systables.tableid
  • systables.schemaid
  • sysschemas.schemaid
  • sysconstraints.schemaid

将这些连接在一起,按

sysconstraints.type = 'F'
进行过滤并选择您感兴趣的列。

© www.soinside.com 2019 - 2024. All rights reserved.