我正在尝试创建一个select语句,它允许我对所有外键进行概述。 它应该是这样的:
Table 1 | FK_Value | Table 2 | FK_Value
我已经想到必须有一个“user_constraints”表的方法,但我需要一些帮助。
答案 0 :(得分:0)
我认为你需要这个:
SELECT a.table_name, a.column_name, a.constraint_name, c.owner,
-- referenced pk
c.r_owner, c_pk.table_name r_table_name, c_pk.constraint_name r_pk
FROM all_cons_columns a
JOIN all_constraints c ON a.owner = c.owner
AND a.constraint_name = c.constraint_name
JOIN all_constraints c_pk ON c.r_owner = c_pk.owner
AND c.r_constraint_name = c_pk.constraint_name
WHERE c.constraint_type = 'R'
AND a.table_name = :TableName
来源:stackoverflow.com/questions/1729996/list-of-foreign-keys-and-the-tables-they-reference
答案 1 :(得分:0)
这在SQL2008(没有2005)
中对我有用获取引用表和列名列表...
select t.name as TableWithForeignKey, fk.constraint_column_id as FK_PartNo , c.name as ForeignKeyColumn
from sys.foreign_key_columns as fk
inner join sys.tables as t on fk.parent_object_id = t.object_id
inner join sys.columns as c on fk.parent_object_id = c.object_id and fk.parent_column_id = c.column_id
where fk.referenced_object_id = (select object_id from sys.tables where name = 'TableOthersForeignKeyInto')
order by TableWithForeignKey, FK_PartNo