开发者

Java: generate CREATE TABLE code from an existing table

开发者 https://www.devze.com 2023-02-09 06:11 出处:网络
is there a way to generate the CREATE TABLE code from an existing table i开发者_运维技巧n a Derby database? Or a simple way to gather the necessary table information?You can try using the dblook tool

is there a way to generate the CREATE TABLE code from an existing table i开发者_运维技巧n a Derby database? Or a simple way to gather the necessary table information?


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


If you just 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 for some other purpose than creating a similar table.


Well, this question is 6 years old, but here the screen shot below shows an easy way to do what you want. I hope you haven't been waiting. ;)

Right click on the table you're interested in and select Scripts -> Create TableScript.

Java: generate CREATE TABLE code from an existing table

When you choose this menu item, you will get a window containing SQL, e.g.

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

If you also want SQL that will populate the table with the existing data, use Scripts -> Create Data Script. For 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');


I don't have any experience in using derby but you could try to use JDBC API to gather database metadata


This works fine in MySQL if you want the CREATE statement.

SHOW CREATE TABLE `table_name`

See mysql doc for more information

0

精彩评论

暂无评论...
验证码 换一张
取 消