The jOOQ User Manual : SQL building : SQL Statements (DDL) : The CREATE statement : CREATE TABLE | previous : next |
New versions: Dev (3.15) | Latest (3.14) | 3.13 | 3.12 | 3.11 | 3.10 | 3.9 | 3.8 | Old versions: 3.7 | 3.6 | 3.5
CREATE TABLE
Applies to ✅ Open Source Edition ✅ Express Edition ✅ Professional Edition ✅ Enterprise Edition
Arguably the most used DDL statement is the CREATE TABLE
statement.
Create a table with columns
There are a few overloads to achieve the same thing with different types of parameters, as always. To keep things simple, only one example is given.
// Create a new table with a column create.createTable("table") .column("column1", INTEGER) .execute();
// Create a new table with columns and unnamed constraints create.createTable("table") .column("column1", INTEGER) .column("column2", VARCHAR(10).nullable(false)) .constraints( primaryKey("column1"), unique("column2"), foreignKey("column2").references("some_other_table"), check(field(name("column2")).like("A%")) ) .execute(); // Create a new table with columns and named constraints create.createTable("table") .column("column1", INTEGER) .column("column2", VARCHAR(10).nullable(false)) .constraints( constraint("pk").primaryKey("column1"), constraint("uk").unique("column2"), constraint("fk").foreignKey("column2").references("some_other_table"), constraint("ck").check(field(name("column2")).like("A%")) ) .execute();
CREATE TABLE AS SELECT
Occasionally, creating a table from a SELECT statement is very useful, copying the source table's data types and data.
// Create a new table from a source SELECT statement create.createTable("book_archive") .as(select(BOOK.ID, BOOK.TITLE).from(BOOK)) .execute();
CREATE [ GLOBAL ] TEMPORARY TABLE
Many dialects support different notions of "temporary" tables, i.e. tables whose data and/or meta data is stored only temporarily. The details of these temporary are implementation specific. jOOQ supports the following syntaxes, both with explicit column lists or as CREATE TABLE AS SELECT
:
// Create a new temporary table create.createTemporaryTable("book_archive") .column("column1", INTEGER) .execute(); // Create a new temporary table create.createGlobalTemporaryTable("book_archive") .column("column1", INTEGER) .execute();
Feedback
Do you have any feedback about this page? We'd love to hear it!