日期:2014-05-16  浏览次数:20770 次

oracle、sqlserver和mysql获取表外键的方法
----oracle------------------------------------

通过select * from user_constraints where constraint_name like '%1FF%';--查找外键所在的表

select a.owner ,a.table_name ,b.column_name ,C.OWNER ,c.table_name ,d.column_name from user_constraints a
left join user_cons_columns b
on a.constraint_name=b.constraint_name
left join user_constraints C
ON C.R_CONSTRAINT_NAME=a.constraint_name
left join user_cons_columns d
on c.constraint_name=d.constraint_name
where a.constraint_type='P'
and a.table_name='DOCUMENTS' --需要查看主外键关系的表
order by a.table_name


-----------------sqlserver----------------------------------
SELECT 主键列ID=b.rkey
    ,主键列名=(SELECT name FROM syscolumns WHERE colid=b.rkey AND id=b.rkeyid)
    ,外键表ID=b.fkeyid
    ,外键表名称=object_name(b.fkeyid)
    ,外键列ID=b.fkey
    ,外键列名=(SELECT name FROM syscolumns WHERE colid=b.fkey AND id=b.fkeyid)
    ,级联更新=ObjectProperty(a.id,'CnstIsUpdateCascade')
    ,级联删除=ObjectProperty(a.id,'CnstIsDeleteCascade')
FROM sysobjects a
    join sysforeignkeys b on a.id=b.constid
    join sysobjects c on a.parent_obj=c.id
where a.xtype='f' AND c.xtype='U'
    and object_name(b.rkeyid)='documents'

-------------------------------------------mysql----------------------------------
select * from INFORMATION_SCHEMA.KEY_COLUMN_USAGE where REFERENCED_TABLE_NAME ='documents'