2014-03-07 73 views
0

我正在嘗試創建一個select語句,該語句允許我概覽所有外鍵。 它應該是這樣的:Oracle:所有約束表

 
Table 1 | FK_Value | Table 2 | FK_Value 

我想通了,必須有與「USER_CONSTRAINTS」表的方式,但我需要一些幫助。

回答

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

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 
+0

的問題是ORACL e,不適用於SQL Server。 –