Create, Alter and Drop a Table

For the creation, modification, or deletion of a database object, such as a table or sequence, use the execute() method.

Create

Here’s how you can create a table:

String create = "CREATE TABLE person (" +
	"id BIGINT NOT NULL, " +
	"name VARCHAR, " +
	"lastName VARCHAR, " +
	"birth_date DATE, " +
	"passport VARCHAR, " +
	"phone VARCHAR,, " +
	"PRIMARY KEY (id))";
try (Statement st = conn.createStatement()) {
	st.execute(create);
}

Alter

Here is a demonstration of how to modify a table:

String alter = "ALTER TABLE person ADD PARTITION(id) FOR VALUES(100)";
try (Statement st = conn.createStatement()) {
	st.execute(alter);
}

Drop

And now, this is how you drop the table previously created:

String drop = "DROP TABLE person";
try (Statement st = conn.createStatement()) {
	st.execute(drop);
}