希望象Sqlserver中一樣能清晰的分析主外鍵表和欄位資訊,用一個SQL語句寫出主外鍵表的欄位一一對應關係,在網上找了半天,有能列出主外鍵表和欄位的,但欄位不是一一對應好的,所以自己寫了一個。
Oracle中擷取主外鍵表名欄位名約束名
select b.table_name as pktable_name,b.column_name pkcolumn_name,c.table_name fktable_name,c.column_name fkcolumn_name,c.position ke_seq,c.constraint_name fk_name from (select * from user_cons_columns ) b left join (select * from user_constraints where user_constraints.constraint_type='R' ) a on b.constraint_name=a.r_constraint_name left join user_cons_columns c on c.constraint_name=a.constraint_name where c.position is not null and c.position=b.position order by c.table_name,c.position
查詢某表的外鍵表和外鍵欄位對應關係
查pktable_name表的外鍵表和約束名欄位對應關係等資訊
string sql="select
b.table_name,b.column_name,b.position,c.table_name,c.column_name,c.position
,c.constraint_name from (select * from user_cons_columns where
table_name='"+pktable_name+"') b left join (select * from user_constraints
where user_constraints.constraint_type='R' and r_constraint_name like
'%"+pktable_name+"') a on b.constraint_name=a.r_constraint_name left join
user_cons_columns c on c.constraint_name=a.constraint_name where
c.position is not null and c.position=b.position order by
c.table_name,c.position "
SqlServer中執行預存程序
1
Sqlserver中查看主外鍵表和欄位對應關係
sp_fkeys pktable_name
pktable_name是指主鍵表的名稱
2
Code
擷取表主外鍵約束
exec sp_helpconstraint 'fktable_name' ;
fktable_name is Foreign Key Table's Name