Delete all tables in Derby DB

后端 未结 10 1032
离开以前
离开以前 2020-12-30 05:42

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

相关标签:
10条回答
  • 2020-12-30 05:59

    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.

    0 讨论(0)
  • 2020-12-30 05:59

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

    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
    0 讨论(0)
  • 2020-12-30 06:02

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

    0 讨论(0)
  • 2020-12-30 06:05

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

    0 讨论(0)
  • 2020-12-30 06:07
    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.
    0 讨论(0)
  • 2020-12-30 06:11

    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).

    0 讨论(0)
提交回复
热议问题