Drop tables in Derby DB

The problem occurred in derbydb to drop all tables, which where also interrelated. First you have to drop the constraints, followed by the tables. I found the following two statements and mixed them up to the third one. So you generate a script for dropping all tables.

Source: http://stackoverflow.com/questions/171727/delete-all-tables-in-derby-db

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

Source: http://stackoverflow.com/questions/3676630/drop-all-constraints-in-derby-via-jdbc

SELECT
 C.CONSTRAINTNAME,
 T.TABLENAME
 FROM
 SYS.SYSCONSTRAINTS C,
 SYS.SYSSCHEMAS S,
 SYS.SYSTABLES T
 WHERE
 C.SCHEMAID = S.SCHEMAID
 AND
 C.TABLEID = T.TABLEID
 AND
 S.SCHEMANAME = 'MYSCHEMA';
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';

This are the tables in the Netbeans Service-Tab
Selection_012
This is the result of the query …
Selection_013
… and this is the result of the result (the error doesn’t matter):
Selection_014

This entry was posted in Derby DB. Bookmark the permalink.

Leave a comment