首页 > 开发 > 综合 > 正文

查询表主键外键信息的SQL

2024-07-21 02:10:35
字体:
来源:转载
供稿:网友

我的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'
 
其它数据库还没时间去实现.

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表