获取Oracle、SqlServer、Access中表名、字段和主键(转)

本文涉及的产品
云数据库 RDS SQL Server,基础系列 2核4GB
简介: 一、oracle 1、获取当前oracle数据库中的所有表 select table_name from user_tables 2、查询某个表中的字段名称、类型、精度、长度、是否为空    select COLUMN_NAME,DATA_TYPE,DATA_PRECISION,DATA...

一、oracle

1、获取当前oracle数据库中的所有表

select table_name from user_tables

2、查询某个表中的字段名称、类型、精度、长度、是否为空   

  • select COLUMN_NAME,DATA_TYPE,DATA_PRECISION,DATA_SCALE,NULLABLE    
  • from user_tab_columns    
  • where table_name ='YourTableName'  

3、查询某个表中的主键字段名   

  • 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'  

4、查询某个表中的外键字段名称、所引用表名、所应用字段名   

  • 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' 

5、如何从Oracle中取得表的注释

       user_tab_comments;表注释

        user_col_comments;表字段注释

  以上两个只能获取自己用户的表的注释信息,如果要访问自己

能够访问的其他用户的表,则需要使用:

        all_tab_comments;表注释

        all_col_comments;表字段注释

  当然,如果有DBA权限,则可以使用

        dba_tab_comments;表注释

        dba_col_comments;表字段注释

  dba*和all*最好指定owner条件。user*没有该字段

        user_tab_comments;表注释

        user_col_comments;表字段注释

  

二、SQLServer

1、读取库中的所有表名

  select name from sysobjects where xtype='u'  

2、字段  

  • 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   

3、主键(参考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)          
  •      )   

4、外键   

  • 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

1、所有表清单

  •  conn.Open();
        dt= conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });

2、表结构

conn.Open();

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

 

 

原文链接:http://blog.sina.com.cn/dongshengjing

目录
相关文章
|
存储 Oracle 关系型数据库
MySQL数据库: 添加列、修改列、删除列、修改列属性、修改表名(包括MySQL、SQLServer、Oracle)
MySQL数据库: 添加列、修改列、删除列、修改列属性、修改表名(包括MySQL、SQLServer、Oracle)
501 0
MySQL数据库: 添加列、修改列、删除列、修改列属性、修改表名(包括MySQL、SQLServer、Oracle)
|
Oracle 关系型数据库
Oracle迁移PPAS:中文表名的处理
Oracle迁移到RDS for PPAS(PostgreSQL)时我们会用到很多不同的工具,在中国有些用户会用 中文 作为表名,甚至字段名。迁移可能会出现ERROR: zero-length delimited identifier at or near """"的错误。针对于此,做了以下DEM.
4802 0
|
Oracle 关系型数据库
Oracle性能优化学习笔记之选择最有效率的表名顺序
        选择最有效率的表名顺序(只在基于规则的优化器中有效)        ORACLE的解析器按照从右到左的顺序处理FROM子句中的表名,因此FROM子句中写在最后的表(基础表 driving table)将被最先处理. 在FROM子句中包含多个表的情况下,你必须选择记录条数最少的表作为基础表.当ORACLE处理多个表时, 会运用排序及合并的方式连接它们.首先,扫描第一个表(FR
977 0
|
Oracle 关系型数据库 索引
如何在oracle中查询所有用户表的表名、主键名称、索引、外键等
1、查找表的所有索引(包括索引名,类型,构成列):  select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_name = i.index_name and t.table_name = i.table_name and t.table_name = 要查询的表  2、查找表的主键(包括名
2709 0
|
Oracle 关系型数据库 数据库
Oracle列出数据库所有表名和列名
1 显示所有表名:select table_name from user_tables; 2 显示所有列名: select OWNER, TABLE_NAME, COLUMN_NA...
1528 0
|
19天前
|
Oracle 关系型数据库 Linux
【赵渝强老师】Oracle数据库配置助手:DBCA
Oracle数据库配置助手(DBCA)是用于创建和配置Oracle数据库的工具,支持图形界面和静默执行模式。本文介绍了使用DBCA在Linux环境下创建数据库的完整步骤,包括选择数据库操作类型、配置存储与网络选项、设置管理密码等,并提供了界面截图与视频讲解,帮助用户快速掌握数据库创建流程。
201 93
|
3月前
|
存储 Oracle 关系型数据库
服务器数据恢复—光纤存储上oracle数据库数据恢复案例
一台光纤服务器存储上有16块FC硬盘,上层部署了Oracle数据库。服务器存储前面板2个硬盘指示灯显示异常,存储映射到linux操作系统上的卷挂载不上,业务中断。 通过storage manager查看存储状态,发现逻辑卷状态失败。再查看物理磁盘状态,发现其中一块盘报告“警告”,硬盘指示灯显示异常的2块盘报告“失败”。 将当前存储的完整日志状态备份下来,解析备份出来的存储日志并获得了关于逻辑卷结构的部分信息。
|
1月前
|
SQL Oracle 关系型数据库
Oracle数据库创建表空间和索引的SQL语法示例
以上SQL语法提供了一种标准方式去组织Oracle数据库内部结构,并且通过合理使用可以显著改善查询速度及整体性能。需要注意,在实际应用过程当中应该根据具体业务需求、系统资源状况以及预期目标去合理规划并调整参数设置以达到最佳效果。
98 8
|
3月前
|
SQL Oracle 关系型数据库
比较MySQL和Oracle数据库系统,特别是在进行分页查询的方法上的不同
两者的性能差异将取决于数据量大小、索引优化、查询设计以及具体版本的数据库服务器。考虑硬件资源、数据库设计和具体需求对于实现优化的分页查询至关重要。开发者和数据库管理员需要根据自身使用的具体数据库系统版本和环境,选择最合适的分页机制,并进行必要的性能调优来满足应用需求。
129 11
|
3月前
|
Oracle 关系型数据库 数据库
数据库数据恢复—服务器异常断电导致Oracle数据库报错的数据恢复案例
Oracle数据库故障: 某公司一台服务器上部署Oracle数据库。服务器意外断电导致数据库报错,报错内容为“system01.dbf需要更多的恢复来保持一致性”。该Oracle数据库没有备份,仅有一些断断续续的归档日志。 Oracle数据库恢复流程: 1、检测数据库故障情况; 2、尝试挂起并修复数据库; 3、解析数据库文件; 4、导出并验证恢复的数据库文件。

推荐镜像

更多