我如何一般性地检测数据库是否为“空”? 来自爪哇
谁能建议一种从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有一些跨数据库 SQL-92 模式查询标准 - 当然,其效果因供应商而异。
对这些标准的支持因供应商而异,表视图的列内容也是如此。 信息模式文档的 SQL 实现可在此处找到:
更具体地说,在 SQL Server 中,sysobjects 元数据早于 SQL92 标准计划。
上面的查询返回数据库中用户表的数量。 有关 sysobjects 表的更多信息,请参见此处:
There are some cross-database SQL-92 schema query standards - mileage for this of course varies according to vendor
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:
More specifically in SQL Server, sysobjects metadata predates the SQL92 standards initiative.
Query above returns the count of User tables in the database. More information about the sysobjects table here:
我不知道这是否是一个完整的解决方案...但是您可以通过读取 getTables 返回的 ResultSet 的 table_type 列来确定表是否是系统表:
在实践中...我认为您可能必须工作得很好很难获得真正可靠、真正通用的解决方案。
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:
In practice ... I think you might have to work pretty hard to get a really reliable, truly generic solution.
您是否总是检查以相同方式创建的数据库? 如果是这样,您也许可以简单地从您熟悉的表子集中进行选择来查找数据。
您可能还需要关注可能添加到查找表中的静态数据,粗略地看去这些静态数据看起来像“数据”,但实际上从有趣的意义上来说可能并不是真正的“数据”。
您能否提供有关您试图解决的具体问题的更多信息? 我想知道如果有更多的数据,是否可以提供更简单、更可靠的答案。
您正在创建这些数据库吗?
您每次都使用大致相同的构造函数创建它们吗?
什么样的过程让这些家伙徘徊,那个构造函数可以破坏吗?
当然有一个元数据过程可以循环遍历表,只是通过一些可能存在的更自定义的东西。
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.
至少在 Oracle 中,您可以从 USER_TABLES 中进行选择以排除任何系统表。
In Oracle, at least, you can select from USER_TABLES to exclude any system tables.
我找不到标准的通用解决方案,因此每个数据库都需要自己的测试集。
例如,对于 Oracle,我曾经检查表、序列和索引:
对于 SqlServer,我曾经检查表、视图和 索引。存储过程:
我得到的最好的通用(和直观)解决方案是使用 ANT SQL 任务 - 我所需要做的就是为每种类型的数据库传递不同的参数。
即 ANT 构建文件看起来像this:
更多详细信息请参考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:
For SqlServer I used to check tables, views and stored procedures:
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:
For more details, please refer to ANT:
https://ant.apache.org/manual/Tasks/sql.html