Title 查詢表主鍵外來鍵資訊的SQL

2021-04-12 21:57:47 字數 1487 閱讀 7985

我的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'

其它資料庫還沒時間去實現.

查詢表主鍵外來鍵資訊的SQL

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 ...

查詢表主鍵 外來鍵

專案中用到的一些sql oracle下的 總結 1 查詢表的所有索引 包括索引名,型別,構成列 select t.i.index type from user ind columns t,user indexes i where t.index name i.index name and t.tab...

主鍵,子外來鍵查詢

主鍵 資料庫主鍵是指表中乙個列或列的組合,其值能唯一地標識表中的每一行。這樣的一列或多列稱為表的主鍵,通過它可強制表的實體完整性。當建立或更改表時可通過定義 primary key約束來建立主鍵。乙個表只能有乙個 primary key 約束,而且 primary key 約束中的列不能接受空值。由...