Java 删除 Derby DB 中的所有表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/171727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:01:31  来源:igfitidea点击:

Delete all tables in Derby DB

javasqljdbcderby

提问by Shimi Bandiel

How do i delete all the tables in the schema on Apache Derby DB using JDBC?

如何使用 JDBC 在 Apache Derby DB 上删除架构中的所有表?

采纳答案by Bryan Pendleton

For actual code that does this, check CleanDatabaseTestSetup.javain the Derby test suite section of the Derby distribution.

对于执行此操作的实际代码,请检查Derby 发行版的 Derby 测试套件部分中的CleanDatabaseTestSetup.java

回答by graham.reeds

I think most db providers don't allow DROP TABLE * (or similar).

我认为大多数数据库提供者不允许 DROP TABLE * (或类似的)。

I think the best way would be to SHOW TABLES and then go through each deleting in a loop via a resultset.

我认为最好的方法是 SHOW TABLES 然后通过结果集在循环中完成每个删除。

HTH.

哈。

回答by Telcontar

Do a little method in java in which you execute a

在java中做一个小方法,在其中执行一个

DROP TABLE [tablename]

tablenameis passed by parameter.

tablename是通过参数传递的。

And another method in which you loop over a record set formed by the query

还有另一种方法,您可以在其中循环查询形成的记录集

SELECT tablename FROM SYSTABLES

calling the first method.

调用第一个方法。

Derby latest documentation

德比最新文档

回答by Telcontar

JDBC allows you to solve your task in a database agnostic way:

JDBC 允许您以与数据库无关的方式解决您的任务:

  1. Open the connection
  2. Grab the DatabaseMetaData
  3. Use it to list all tables in your database JavaDoc
  4. Iterate over the resultset and fire the DROP TABLE for each table
  1. 打开连接
  2. 获取数据库元数据
  3. 使用它列出数据库JavaDoc中的所有表
  4. 迭代结果集并为每个表触发 DROP TABLE

回答by Fuangwith S.

  1. you must generate schema and table name from Derby DB system catalog.
  2. Order all tables by relation.
  3. Generate java statement for drop all tables
  4. Use autoCommit() method and set this method to false. for manual commit or rollback transactions when got errors.
  5. Run you java process. Good Luck.
  1. 您必须从 Derby DB 系统目录生成模式和表名。
  2. 按关系排序所有表。
  3. 生成用于删除所有表的 java 语句
  4. 使用 autoCommit() 方法并将此方法设置为 false。用于在出现错误时手动提交或回滚事务。
  5. 运行你的java进程。祝你好运。

回答by Dónal

A simpler solution is to use JDBC to run "drop database foo" then "create database foo". However, this will cause all objects in the DB to be deleted (i.e. not just tables).

一个更简单的解决方案是使用 JDBC 运行“drop database foo”然后“create database foo”。但是,这将导致数据库中的所有对象都被删除(即不仅仅是表)。

回答by gregn

If you're working from the command prompt rather than through JDBC, this should get you started.

如果您是从命令提示符而不是通过 JDBC 工作,这应该可以帮助您入门。

SELECT 'DROP TABLE ' || schemaname ||'.' || tablename || ';'
FROM SYS.SYSTABLES
INNER JOIN SYS.SYSSCHEMAS ON SYS.SYSTABLES.SCHEMAID = SYS.SYSSCHEMAS.SCHEMAID
;

回答by Ben

A simple solution is to do right click -> disconnect then delete the folder containing your database and reconnect it.

一个简单的解决方案是右键单击-> 断开连接,然后删除包含数据库的文件夹并重新连接。

回答by Brij

Download Squirrel SQL from http://squirrel-sql.sourceforge.net/

http://squirrel-sql.sourceforge.net/下载 Squirrel SQL

Connect to the database.

连接到数据库。

Expand the TABLE node.

展开 TABLE 节点。

Select the tables that you want to drop.

选择要删除的表。

Right click and select -> Scripts -> Drop table scripts

右键单击并选择 -> 脚本 -> 删除表脚本

Run the generated queries

运行生成的查询

You can even select delete records to empty the selected tables.

您甚至可以选择删除记录来清空选定的表。

回答by Sym-Sym

Thanks are due to the blog:

感谢博客

Step 1:

第1步:

Run the SQL statement, but don't forget to replace the schema name 'APP' with your your schema name in the 2 occurrences below:

运行 SQL 语句,但不要忘记在以下 2 次出现时将架构名称“APP”替换为您的架构名称:

SELECT
'ALTER TABLE '||S.SCHEMANAME||'.'||T.TABLENAME||' DROP CONSTRAINT '||C.CONSTRAINTNAME||';'
FROM
    SYS.SYSCONSTRAINTS C,
    SYS.SYSSCHEMAS S,
    SYS.SYSTABLES T
WHERE
    C.SCHEMAID = S.SCHEMAID
AND
    C.TABLEID = T.TABLEID
AND
S.SCHEMANAME = 'APP'
UNION
SELECT 'DROP TABLE ' || schemaname ||'.' || tablename || ';'
FROM SYS.SYSTABLES
INNER JOIN SYS.SYSSCHEMAS ON SYS.SYSTABLES.SCHEMAID = SYS.SYSSCHEMAS.SCHEMAID
where schemaname='APP';

Step 2:

第2步:

The result of the above execution is a set of SQL statements, copy them to the SQL editor, execute them, then the constraints and the tables are dropped.

上述执行的结果是一组SQL语句,将它们复制到SQL编辑器中,执行它们,然后约束和表被删除。