在 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;
在
sys
数据库中,您有以下字段:
sysconstraints.constraintid
sysforeignkeys.constraintid
sysforeignkeys.conglomerateid
sysconglomerates.conglomerateid
sysconglomerates.tableid
systables.tableid
systables.schemaid
sysschemas.schemaid
sysconstraints.schemaid
将这些连接在一起,按
sysconstraints.type = 'F'
进行过滤并选择您感兴趣的列。