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

2021-04-01 20:25:46 字數 1411 閱讀 4536

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'

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

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

查詢表主鍵 外來鍵

專案中用到的一些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...

sql 主鍵表與外來鍵表的區分

主鍵 表和外建表是相對來說的,簡單的說就是一個表的 主鍵是另外一張表的外來鍵。例如class 班級表主要欄位如下 classid primary key 主鍵 班級id classname 班級名稱 studen 學生表主要欄位如下 stuid primary key 主鍵 學生id stuname...

SQL主鍵表與外來鍵表的區分

主鍵 表和外建表是相對來說的,簡單的說就是一個表的 主鍵是另外一張表的 外來鍵。例如 class 班級表主要欄位如下 classid primary key 主鍵 班級id classname 班級名稱 studen 學生表主要欄位如下 stuid primary key 主鍵 學生id stuna...

sql 中查詢表的外來鍵

一 查詢出所有表的鍵 select from information schema.key column usage where table name you tablename 二 查詢出所有的外來鍵 select from select from information schema.key c...