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

本文涉及的产品
RDS SQL Server Serverless,2-4RCU 50GB 3个月
推荐场景:
云数据库 RDS SQL Server,基础系列 2核4GB
简介:

一、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


本文转自贺满博客园博客,原文链接:http://www.cnblogs.com/puresoul/archive/2010/06/29/1767455.html,如需转载请自行联系原作者。


相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情: https://www.aliyun.com/product/rds/sqlserver
目录
相关文章
|
2月前
|
SQL Java Scala
flink-cdc SQL Server op 字段如何获取?
Flink CDC 是 Apache Flink 的组件,用于捕获数据库变更事件。对 SQL Server,通过 Debezium 连接器支持变更数据捕获。`op` 字段标识操作类型(INSERT、UPDATE、DELETE)。配置包括添加依赖及设定 Source 连接器,可通过 Flink SQL 或 Java/Scala 完成。示例查询利用 `op` 字段筛选处理变更事件。
70 1
|
3月前
|
开发框架 Oracle 关系型数据库
ABP框架使用Oracle数据库,并实现从SQLServer中进行数据迁移的处理
ABP框架使用Oracle数据库,并实现从SQLServer中进行数据迁移的处理
|
3月前
|
Oracle 关系型数据库 Java
mybatis使用statement.getGenreatedKeys(); useGeneratedKeys=”true”;使用自增主键获取主键值策略和Oracle不支持自增,Oracle使用序列
mybatis使用statement.getGenreatedKeys(); useGeneratedKeys=”true”;使用自增主键获取主键值策略和Oracle不支持自增,Oracle使用序列
|
3月前
|
SQL Oracle 关系型数据库
MySQL、SQL Server和Oracle数据库安装部署教程
数据库的安装部署教程因不同的数据库管理系统(DBMS)而异,以下将以MySQL、SQL Server和Oracle为例,分别概述其安装部署的基本步骤。请注意,由于软件版本和操作系统的不同,具体步骤可能会有所变化。
186 3
|
4月前
|
SQL 存储 测试技术
|
3月前
|
SQL 存储 Oracle
TDengine 3.3.2.0 发布:新增 UDT 及 Oracle、SQL Server 数据接入
**TDengine 3.3.2.0 发布摘要** - 开源与企业版均强化性能,提升WebSocket、stmt模式写入与查询效率,解决死锁,增强列显示。 - taos-explorer支持geometry和varbinary类型。 - 企业版引入UDT,允许自定义数据转换。 - 新增Oracle和SQL Server数据接入。 - 数据同步优化,支持压缩,提升元数据同步速度,错误信息细化,支持表名修改。 - 扩展跨平台支持,包括麒麟、Euler、Anolis OS等。
101 0
|
4月前
|
SQL Oracle 关系型数据库
实时计算 Flink版产品使用问题之oracle无主键的表支持同步吗如何实现
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStream API、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
|
5月前
|
SQL Java 网络安全
实时计算 Flink版操作报错合集之SQLserver表没有主键,同步的时候报错如何解决
在使用实时计算Flink版过程中,可能会遇到各种错误,了解这些错误的原因及解决方法对于高效排错至关重要。针对具体问题,查看Flink的日志是关键,它们通常会提供更详细的错误信息和堆栈跟踪,有助于定位问题。此外,Flink社区文档和官方论坛也是寻求帮助的好去处。以下是一些常见的操作报错及其可能的原因与解决策略。
87 1
|
5月前
|
SQL Oracle 关系型数据库
常用数据库的分页语句(mySQL、oracle、PostgreSQL、SQL Server)
常用数据库的分页语句(mySQL、oracle、PostgreSQL、SQL Server)
|
21天前
|
SQL 数据库
数据库数据恢复—SQL Server数据库报错“错误823”的数据恢复案例
SQL Server附加数据库出现错误823,附加数据库失败。数据库没有备份,无法通过备份恢复数据库。 SQL Server数据库出现823错误的可能原因有:数据库物理页面损坏、数据库物理页面校验值损坏导致无法识别该页面、断电或者文件系统问题导致页面丢失。
88 12
数据库数据恢复—SQL Server数据库报错“错误823”的数据恢复案例

推荐镜像

更多