Home >Backend Development >PHP Tutorial >SQL_PHP tutorial for querying table primary key and foreign key information

SQL_PHP tutorial for querying table primary key and foreign key information

WBOY
WBOYOriginal
2016-07-13 17:02:49877browse

In my BSOOC, I need a SQL to query the primary key and foreign key information of the table. I studied until 1 am last night and finally achieved this goal:
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'

Other databases have not had time to implement it yet.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631014.htmlTechArticleI need a SQL to query the primary key and foreign key information of the table in my BSOOC. I studied it until 1 am last night and finally finally This goal can be achieved: Oracle: select o.obj# as objectId, o.name AS tableName, oc.n...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn