问题
How do i delete all the tables in the schema on Apache Derby DB using JDBC?
回答1:
For actual code that does this, check CleanDatabaseTestSetup.java in the Derby test suite section of the Derby distribution.
回答2:
Thanks are due to the blog:
Step 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:
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:
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.
回答3:
Do a little method in java in which you execute a
DROP TABLE [tablename]
tablename
is passed by parameter.
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
回答4:
I think most db providers don't allow DROP TABLE * (or similar).
I think the best way would be to SHOW TABLES and then go through each deleting in a loop via a resultset.
HTH.
回答5:
JDBC allows you to solve your task in a database agnostic way:
- Open the connection
- Grab the DatabaseMetaData
- Use it to list all tables in your database JavaDoc
- Iterate over the resultset and fire the DROP TABLE for each table
回答6:
- you must generate schema and table name from Derby DB system catalog.
- Order all tables by relation.
- Generate java statement for drop all tables
- Use autoCommit() method and set this method to false. for manual commit or rollback transactions when got errors.
- Run you java process. Good Luck.
回答7:
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).
回答8:
If you're working from the command prompt rather than through JDBC, this should get you started.
SELECT 'DROP TABLE ' || schemaname ||'.' || tablename || ';'
FROM SYS.SYSTABLES
INNER JOIN SYS.SYSSCHEMAS ON SYS.SYSTABLES.SCHEMAID = SYS.SYSSCHEMAS.SCHEMAID
;
回答9:
A simple solution is to do right click -> disconnect then delete the folder containing your database and reconnect it.
回答10:
Download Squirrel SQL from http://squirrel-sql.sourceforge.net/
Connect to the database.
Expand the TABLE node.
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.
来源:https://stackoverflow.com/questions/171727/delete-all-tables-in-derby-db