Java: generate CREATE TABLE code from an existing table

Is there a way to generate CREATE TABLE code from an existing table in a Derby database? Or an easy way to gather the necessary table information?

+6
java sql derby
source share
4 answers

I have no experience using derby, but you can try using the JDBC API to collect database metadata

0
source share

You can try using the dblook tool to dump the Apache Derby database table into sql file.

+5
source share

If you want the CREATE-statement to recreate the table, this works for me:

CREATE TABLE new_table AS SELECT * FROM old_table WITH NO DATA; 

But this does not help if you really want the CREATE statement to perform some other purpose than creating a similar table.

+1
source share

Well, this question is 6 years old, but the screenshot below shows an easy way to do what you want. I hope you did not wait.;)

Right-click the table you are interested in and select Scripts β†’ Create TableScript .

Create a screenshot of the table

When you select this menu item, you will get a window containing SQL, for example.

 CREATE TABLE "JOOMP"."METRICLEVELS" ( LEVELID int NOT NULL, LEVELNAME varchar(30) NOT NULL ) ; 

If you also want SQL to populate the table with existing data, use Scripts Create Data Script . In this example, you will get:

 INSERT INTO "JOOMP"."METRICLEVELS" (LEVELID,LEVELNAME) VALUES (2,'Project'); INSERT INTO "JOOMP"."METRICLEVELS" (LEVELID,LEVELNAME) VALUES (3,'Package Fragment Root'); INSERT INTO "JOOMP"."METRICLEVELS" (LEVELID,LEVELNAME) VALUES (4,'Package Fragment'); INSERT INTO "JOOMP"."METRICLEVELS" (LEVELID,LEVELNAME) VALUES (5,'Compilation Unit'); INSERT INTO "JOOMP"."METRICLEVELS" (LEVELID,LEVELNAME) VALUES (7,'Type'); INSERT INTO "JOOMP"."METRICLEVELS" (LEVELID,LEVELNAME) VALUES (9,'Method'); 
0
source share

All Articles