zoukankan      html  css  js  c++  java
  • 关于Oracle与SqlServer、Access中获取,查询所有字段、主键、外键的sql语句

    • --oracle:   
    • --查询某个表中的字段名称、类型、精度、长度、是否为空   
    • select COLUMN_NAME,DATA_TYPE,DATA_PRECISION,DATA_SCALE,NULLABLE    
    • from user_tab_columns    
    • where table_name ='YourTableName'  
    • --查询某个表中的主键字段名   
    • select col.column_name    
    • from user_constraints con,  user_cons_columns col    
    • where con.constraint_name = col.constraint_name    
    • and con.constraint_type='P'    
    • and col.table_name = 'YourTableName'  
    • --查询某个表中的外键字段名称、所引用表名、所应用字段名   
    • select distinct(col.column_name),r.table_name,r.column_name    
    • from    
    • user_constraints con,   
    • user_cons_columns col,    
    • (select t2.table_name,t2.column_name,t1.r_constraint_name    
    •  from user_constraints t1,user_cons_columns t2    
    •  where t1.r_constraint_name=t2.constraint_name    
    •  and t1.table_name='YourTableName'  
    •  ) r    
    • where con.constraint_name=col.constraint_name    
    • and con.r_constraint_name=r.r_constraint_name    
    • and con.table_name='YourTableName'    
    • --SQLServer中的实现:   
    • --字段:   
    • SELECT c.name,t.name,c.xprec,c.xscale,c.isnullable    
    • FROM systypes t,syscolumns c    
    • WHERE t.xtype=c.xtype    
    • AND c.id = (SELECT id FROM sysobjects WHERE name='YourTableName')    
    • ORDER BY c.colid   
    •   
    • --主键(参考SqlServer系统存储过程sp_pkeys):   
    • select COLUMN_NAME = convert(sysname,c.name)                  
    • from                                                          
    • sysindexes i, syscolumns c, sysobjects o                      
    • where o.id = object_id('[YourTableName]')                     
    • and o.id = c.id                                               
    • and o.id = i.id                                               
    • and (i.status & 0x800) = 0x800                                
    • and (c.name = index_col ('[YourTableName]', i.indid,  1) or        
    •      c.name = index_col ('[YourTableName]', i.indid,  2) or        
    •      c.name = index_col ('[YourTableName]', i.indid,  3) or        
    •      c.name = index_col ('[YourTableName]', i.indid,  4) or        
    •      c.name = index_col ('[YourTableName]', i.indid,  5) or        
    •      c.name = index_col ('[YourTableName]', i.indid,  6) or        
    •      c.name = index_col ('[YourTableName]', i.indid,  7) or        
    •      c.name = index_col ('[YourTableName]', i.indid,  8) or        
    •      c.name = index_col ('[YourTableName]', i.indid,  9) or        
    •      c.name = index_col ('[YourTableName]', i.indid, 10) or        
    •      c.name = index_col ('[YourTableName]', i.indid, 11) or        
    •      c.name = index_col ('[YourTableName]', i.indid, 12) or        
    •      c.name = index_col ('[YourTableName]', i.indid, 13) or        
    •      c.name = index_col ('[YourTableName]', i.indid, 14) or        
    •      c.name = index_col ('[YourTableName]', i.indid, 15) or        
    •      c.name = index_col ('[YourTableName]', i.indid, 16)          
    •      )   
    •   
    • --外键:   
    • select t1.name,t2.rtableName,t2.name    
    • from    
    • (select col.name, f.constid as temp    
    •  from syscolumns col,sysforeignkeys f    
    •  where f.fkeyid=col.id    
    •  and f.fkey=col.colid    
    •  and f.constid in    
    •  ( select distinct(id)     
    •    from sysobjects    
    •    where OBJECT_NAME(parent_obj)='YourTableName'    
    •    and xtype='F'    
    •   )    
    •  ) as t1 ,    
    • (select OBJECT_NAME(f.rkeyid) as rtableName,col.name, f.constid as temp    
    •  from syscolumns col,sysforeignkeys f    
    •  where f.rkeyid=col.id    
    •  and f.rkey=col.colid    
    •  and f.constid in    
    •  ( select distinct(id)    
    •    from sysobjects    
    •    where OBJECT_NAME(parent_obj)='YourTableName'    
    •    and xtype='F'    
    •  )    
    • as t2    
    • where t1.temp=t2.temp  
    • ------Access
    • 所有表清单
    •  conn.Open();
          dt= conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
    • 表结构
    • conn.Open();

                      dtColumnsInfo = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, strTableName,null });

    作者:VShawn

    出处:http://www.cnblogs.com/singlex/

    本文版权归作者所有,欢迎转载,但未经博客作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    Linux下zip命令使用
    docker镜像发布到阿里云镜像仓库
    基于官方镜像定制php-fpm容器
    docker-compose部署开发环境
    docker安装discuz!Q
    从零开始实现简单 RPC 框架 4:注册中心
    从零开始实现简单 RPC 框架 3:配置总线 URL
    从零开始实现简单 RPC 框架 2:扩展利器 SPI
    从零开始实现简单 RPC 框架 1:RPC 框架的结构和设计
    文本分类(六):pytorch实现DPCNN
  • 原文地址:https://www.cnblogs.com/singlex/p/2280045.html
Copyright © 2011-2022 走看看