我的bsooc里需要一个查询表主键外键信息的sql,昨晚研究到凌晨1点,终于能实现这个目标:
oracle:
select o.obj# as objectid, o.name as tablename, oc.name as constraintname,
decode(c.type#, 1, 'c', 2, 'p', 3, 'u',
4, 'r', 5, 'v', 6, 'o', 7,'c', '?') as constrainttype,
col.name as columnname
from sys.con$ oc, sys.con$ rc,
sys.obj$ ro,sys.obj$ o, sys.obj$ oi,
sys.cdef$ c,
sys.col$ col, sys.ccol$ cc, sys.attrcol$ ac
where oc.con# = c.con#
and c.obj# = o.obj#
and c.rcon# = rc.con#(+)
and c.enabled = oi.obj#(+)
and c.robj# = ro.obj#(+)
and c.type# != 8
and c.type# != 12 /* don't include log groups */
and c.con# = cc.con#
and cc.obj# = col.obj#
and cc.intcol# = col.intcol#
and cc.obj# = o.obj#
and col.obj# = ac.obj#(+)
and col.intcol# = ac.intcol#(+)
and o.name = 'your table'
sql server:
select sysobjects.id objectid,
object_name(sysobjects.parent_obj) tablename,
sysobjects.name constraintname,
sysobjects.xtype as constrainttype,
syscolumns.name as columnname
from sysobjects inner join sysconstraints
on sysobjects.xtype in('c', 'f', 'pk', 'uq', 'd')
and sysobjects.id = sysconstraints.constid
left outer join syscolumns on sysconstraints.id = syscolumns.id
where object_name(sysobjects.parent_obj)='your table'
其它数据库还没时间去实现.
新闻热点
疑难解答