The jOOQ User Manual : SQL building : SQL Statements (DDL) : The ALTER statement : ALTER 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 | 3.4
ALTER TABLE
Applies to ✅ Open Source Edition ✅ Express Edition ✅ Professional Edition ✅ Enterprise Edition
The ALTER TABLE
statement is certainly the most powerful among DDL statements, as tables are the most important object type in a database catalog. The following types of statements are supported when altering a table:
ADD
Columns can be added to a table using the following API:
// Adding a single column to a table create.alterTable("table").add("column", INTEGER).execute();
There exists alternative API representing optional keywords, such as e.g. addColumn()
, which have been omitted from the examples.
Note that some dialects also consider indexes to be a part of a table, but jOOQ does not yet support ALTER TABLE
subclauses modifying indexes. Consider CREATE INDEX or DROP INDEX, instead.
ALTER
Both of the above objects can be altered in a table using the following API:
// Specify a new default value for a column create.alterTable("table").alter("column").default_(1).execute(); // Set a new data type on the column create.alterTable("table").alter("column").set(VARCHAR(50)).execute();
There exists alternative API representing optional keywords, such as e.g. alterColumn()
, which have been omitted from the examples.
DROP
Both columns and constraints can also be dropped from tables using this API:
// Drop a single column create.alterTable("table").drop("column").execute(); // Add CASCADE or RESTRICT clauses when dropping columns (or constraints) create.alterTable("table").drop("column").cascade().execute(); create.alterTable("table").drop("column").restrict().execute(); // Drop a constraint create.alterTable("table").dropConstraint("uk").execute();
Feedback
Do you have any feedback about this page? We'd love to hear it!