【问题标题】:List of tables (in DB2, SQL Server, Informix and Oracle)表列表(在 DB2、SQL Server、Informix 和 Oracle 中)
【发布时间】:2015-12-15 13:41:06
【问题描述】:

我需要编写代码来了解有关多个数据库的一些信息。执行代码的将是我们的客户,所以我将无法“在线”调整它。 数据库将是 Oracle(9、10、11)、DB2、AS400 DB2、Informix 和 MS SQL(2000 和 2008)。 我编写了 Oracle 所需的代码,但我对其他数据库一无所知。你能帮我为其他数据库复制它吗? 非常感谢!

1

select owner, count(*) -- number of tables in schemes
  from all_tables
 where owner not in ('SYS', 'SYSTEM', 'SYSMAN') and temporary = 'N'
 group by owner

2

 select atc.owner, atc.data_type, count(*) --number of tables by schemes and datatypes
      from all_tab_columns atc
     inner join all_tables t
        on t.OWNER = atc.OWNER
       and t.TABLE_NAME = atc.TABLE_NAME
     where atc.owner not in ('SYS', 'SYSTEM', 'SYSMAN')
       and t.temporary = 'N'
     group by atc.owner, atc.data_type

3

select atcom.owner, count(*) --number of comments by schemes
  from all_tab_comments atcom
   inner join all_tables t
    on t.OWNER = atcom.OWNER
   and t.TABLE_NAME = atcom.TABLE_NAME
 where atcom.comments is not null
 and atcom.owner not in ('SYS', 'SYSTEM', 'SYSMAN')
 group by atcom.owner

4

 select owner, constraint_type, count(*) --number of constraints by schemes and types
      from all_constraints ac
     where status = 'ENABLED' and owner not in ('SYS', 'SYSTEM', 'SYSMAN')
     group by owner, constraint_type

【问题讨论】:

  • 查看 INFORMATION_SCHEMA。
  • 您使用的是什么 API?如果您支持连接到许多不同的数据库,那么您很可能正在使用跨平台 API 来连接到数据库。如果是这种情况,该 API(ODBC、JDBC、OLE DB、ODP.Net 等)将提供执行此操作的函数,而不是为每个数据库编写特定的 SQL。

标签: sql oracle db2 ibm-midrange informix


【解决方案1】:

你有一些选择。

  1. 借助 ODBC 或 JDBC 等技术,您可以使用其 API 来读取此类信息。如果您使用 JDBC,则有 DatabaseMetaDatahttp://docs.oracle.com/javase/8/docs/api/java/sql/DatabaseMetaData.html

  2. 您可以通过 SQL 查询数据库提供的架构信息。我在将数据库模式转储到文本文件的工具中使用此类查询:https://code.activestate.com/recipes/576621-dump-informix-schema-to-text/?in=user-186902。对于表名的 Informix qyerying 如下所示:

    SELECT tabname
    FROM systables
    WHERE tabtype='T'
    AND tabid >= 100
    AND tabname[1] <> '_'
    ORDER BY tabname
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-16
    • 1970-01-01
    • 2010-11-25
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多