两种数据库中查询表主键外键信息的SQL语句

下文为您介绍Oracle及SQL Server两种数据库中查询表主键外键信息的SQL语句写法,供您参考,希望对您学习SQL语句的使用有所启迪。

Oracle:

 
 
 
  1. select o.obj# as objectId, o.name AS tableName, oc.name AS constraintName,  
  2.        decode(c.type#, 1, 'C', 2, 'P', 3, 'U',  
  3.               4, 'R', 5, 'V', 6, 'O', 7,'C', '?') as constraintType,   
  4.        col.name AS columnName  
  5.         
  6. from sys.con$ oc, sys.con$ rc,   
  7.      sys.obj$ ro,sys.obj$ o, sys.obj$ oi,  
  8.      sys.cdef$ c,  
  9.      sys.col$ col, sys.ccol$ cc, sys.attrcol$ ac  
  10. where oc.con# = c.con#  
  11.   and c.obj# = o.obj#  
  12.   and c.rcon# = rc.con#(+)  
  13.   and c.enabled = oi.obj#(+)  
  14.   and c.robj# = ro.obj#(+)  
  15.   and c.type# != 8  
  16.   and c.type# != 12       /* don't include log groups */   
  17.  
  18. 字串9  
  19.  
  20.   and c.con# = cc.con#  
  21.   and cc.obj# = col.obj#  
  22.   and cc.intcol# = col.intcol#  
  23.   and cc.obj# = o.obj#  
  24.   and col.obj# = ac.obj#(+)  
  25.   and col.intcol# = ac.intcol#(+)  
  26.   and o.name = 'your table' 

SQL Server:
 

 
 
 
  1. SELECT sysobjects.id objectId,  
  2. OBJECT_NAME(sysobjects.parent_obj) tableName,  
  3. sysobjects.name constraintName,   
  4. sysobjects.xtype AS constraintType,  
  5. syscolumns.name AS columnName  
  6. FROM sysobjects INNER JOIN sysconstraints  
  7. ON sysobjects.xtype in('C', 'F', 'PK', 'UQ', 'D')   
  8.  AND sysobjects.id = sysconstraints.constid  
  9. LEFT OUTER JOIN syscolumns ON sysconstraints.id = syscolumns.id  
  10. WHERE OBJECT_NAME(sysobjects.parent_obj)='your table'  

 

 

【编辑推荐】

修改SQL主键约束的SQL语句写法

对存储过程代替SQL语句的讨论

试SQL语句执行时间的方法

SQL语句中的SELECT DISTINCT

如何使用SQL语句修改字段默认值

THE END