我如何一般性地检测数据库是否为“空”? 来自爪哇

发布于 2024-07-06 02:22:20 字数 370 浏览 6 评论 0原文

谁能建议一种从 Java 检测数据库是否为空的好方法(至少需要支持 Microsoft SQL Server、Derby 和 Oracle)?

我所说的空是指如果数据库是使用新的创建数据库语句新创建的,则处于这种状态,尽管如果覆盖 99% 的情况,检查不必是 100% 完美。

我的第一个想法是做这样的事情......

tables = metadata.getTables(null, null, null, null);
Boolean isEmpty = !tables.next();
return isEmpty;

但不幸的是,这给了我一堆底层系统表(至少在 Microsoft SQL Server 中)。

Can anyone suggest a good way of detecting if a database is empty from Java (needs to support at least Microsoft SQL Server, Derby and Oracle)?

By empty I mean in the state it would be if the database were freshly created with a new create database statement, though the check need not be 100% perfect if covers 99% of cases.

My first thought was to do something like this...

tables = metadata.getTables(null, null, null, null);
Boolean isEmpty = !tables.next();
return isEmpty;

...but unfortunately that gives me a bunch of underlying system tables (at least in Microsoft SQL Server).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

苍风燃霜 2024-07-13 02:22:20

有一些跨数据库 SQL-92 模式查询标准 - 当然,其效果因供应商而异。

SELECT COUNT(*) FROM [INFORMATION_SCHEMA].[TABLES] WHERE [TABLE_TYPE] = <tabletype>

对这些标准的支持因供应商而异,表视图的列内容也是如此。 信息模式文档的 SQL 实现可在此处找到:

http://msdn.microsoft.com/ en-us/library/aa933204(SQL.80).aspx

更具体地说,在 SQL Server 中,sysobjects 元数据早于 SQL92 标准计划。

SELECT COUNT(*) FROM [sysobjects] WHERE [type] = 'U'

上面的查询返回数据库中用户表的数量。 有关 sysobjects 表的更多信息,请参见此处:

http://msdn.microsoft.com/ en-us/library/aa260447(SQL.80).aspx

There are some cross-database SQL-92 schema query standards - mileage for this of course varies according to vendor

SELECT COUNT(*) FROM [INFORMATION_SCHEMA].[TABLES] WHERE [TABLE_TYPE] = <tabletype>

Support for these varies by vendor, as does the content of the columns for the Tables view. SQL implementation of Information Schema docs found here:

http://msdn.microsoft.com/en-us/library/aa933204(SQL.80).aspx

More specifically in SQL Server, sysobjects metadata predates the SQL92 standards initiative.

SELECT COUNT(*) FROM [sysobjects] WHERE [type] = 'U'

Query above returns the count of User tables in the database. More information about the sysobjects table here:

http://msdn.microsoft.com/en-us/library/aa260447(SQL.80).aspx

梦中的蝴蝶 2024-07-13 02:22:20

我不知道这是否是一个完整的解决方案...但是您可以通过读取 getTables 返回的 ResultSet 的 table_type 列来确定表是否是系统表:

int nonSystemTableCount = 0;
tables = metadata.getTables(null, null, null, null);
while( tables.next () ) {
    if( !"SYSTEM TABLE".equals( tables.getString( "table_type" ) ) ) {
        nonSystemTableCount++;
    }
}
boolean isEmpty = nonSystemTableCount == 0;
return isEmpty;

在实践中...我认为您可能必须工作得很好很难获得真正可靠、真正通用的解决方案。

I don't know if this is a complete solution ... but you can determine if a table is a system table by reading the table_type column of the ResultSet returned by getTables:

int nonSystemTableCount = 0;
tables = metadata.getTables(null, null, null, null);
while( tables.next () ) {
    if( !"SYSTEM TABLE".equals( tables.getString( "table_type" ) ) ) {
        nonSystemTableCount++;
    }
}
boolean isEmpty = nonSystemTableCount == 0;
return isEmpty;

In practice ... I think you might have to work pretty hard to get a really reliable, truly generic solution.

屌丝范 2024-07-13 02:22:20

您是否总是检查以相同方式创建的数据库? 如果是这样,您也许可以简单地从您熟悉的表子集中进行选择来查找数据。

您可能还需要关注可能添加到查找表中的静态数据,粗略地看去这些静态数据看起来像“数据”,但实际上从有趣的意义上来说可能并不是真正的“数据”。

您能否提供有关您试图解决的具体问题的更多信息? 我想知道如果有更多的数据,是否可以提供更简单、更可靠的答案。

您正在创建这些数据库吗?
您每次都使用大致相同的构造函数创建它们吗?
什么样的过程让这些家伙徘徊,那个构造函数可以破坏吗?

当然有一个元数据过程可以循环遍历表,只是通过一些可能存在的更自定义的东西。

Are you always checking databases created in the same way? If so you might be able to simply select from a subset of tables that you are familiar with to look for data.

You also might need to be concerned about static data perhaps added to a lookup table that looks like 'data' from a cursory glance, but might in fact not really be 'data' in an interesting sense of the term.

Can you provide any more information about the specific problem you are trying to tackle? I wonder if with more data a simpler and more reliable answer might be provided.

Are you creating these databases?
Are you creating them with roughly the same constructor each time?
What kind of process leaves these guys hanging around, and can that constructor destruct?

There is certainly a meta data process to loop through tables, just through something a little more custom might exist.

心欲静而疯不止 2024-07-13 02:22:20

至少在 Oracle 中,您可以从 USER_TABLES 中进行选择以排除任何系统表。

In Oracle, at least, you can select from USER_TABLES to exclude any system tables.

躲猫猫 2024-07-13 02:22:20

我找不到标准的通用解决方案,因此每个数据库都需要自己的测试集。

例如,对于 Oracle,我曾经检查表、序列和索引:

select count(*) from user_tables
select count(*) from user_sequences
select count(*) from user_indexes

对于 SqlServer,我曾经检查表、视图和 索引。存储过程:

SELECT * FROM sys.all_objects where type_desc in ('USER_TABLE', 'SQL_STORED_PROCEDURE', 'VIEW')

我得到的最好的通用(和直观)解决方案是使用 ANT SQL 任务 - 我所需要做的就是为每种类型的数据库传递不同的参数。

即 ANT 构建文件看起来像this:

<project name="run_sql_query" basedir="." default="main">
    <!-- run_sql_query: --> 
    <target name="run_sql_query">
        <echo message="=== running sql query from file ${database.src.file}; check the result in ${database.out.file} ==="/>
        <sql classpath="${jdbc.jar.file}" 
            driver="${database.driver.class}" 
            url="${database.url}" 
            userid="${database.user}" 
            password="${database.password}" 
            src="${database.src.file}"
            output="${database.out.file}"
            print="yes"/>
    </target>

    <!-- Main: --> 
    <target name="main" depends="run_sql_query"/>   
</project> 

更多详细信息请参考ANT:

https://ant.apache.org /manual/Tasks/sql.html

I could not find a standard generic solution, so each database needs its own tests set.

For Oracle for instance, I used to check tables, sequences and indexes:

select count(*) from user_tables
select count(*) from user_sequences
select count(*) from user_indexes

For SqlServer I used to check tables, views and stored procedures:

SELECT * FROM sys.all_objects where type_desc in ('USER_TABLE', 'SQL_STORED_PROCEDURE', 'VIEW')

The best generic (and intuitive) solution I got, is by using ANT SQL task - all I needed to do is passing different parameters for each type of database.

i.e. The ANT build file looks like this:

<project name="run_sql_query" basedir="." default="main">
    <!-- run_sql_query: --> 
    <target name="run_sql_query">
        <echo message="=== running sql query from file ${database.src.file}; check the result in ${database.out.file} ==="/>
        <sql classpath="${jdbc.jar.file}" 
            driver="${database.driver.class}" 
            url="${database.url}" 
            userid="${database.user}" 
            password="${database.password}" 
            src="${database.src.file}"
            output="${database.out.file}"
            print="yes"/>
    </target>

    <!-- Main: --> 
    <target name="main" depends="run_sql_query"/>   
</project> 

For more details, please refer to ANT:

https://ant.apache.org/manual/Tasks/sql.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文