·您的位置: 首页 » 资源教程 » 编程开发 » 数据库 » 查询表主键外键信息的SQL

查询表主键外键信息的SQL

类别: 数据库教程  评论数:0 总得分:0
我的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\'

其它数据库还没时间去实现.
-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1