希望象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