ColumnDefinitionBuilder class is used to define column properties when creating or altering tables. It provides a fluent API for specifying constraints, defaults, and other column attributes.
Methods
autoIncrement
Addsauto_increment or autoincrement to the column definition depending on the dialect.
Some dialects like PostgreSQL don’t support this. On PostgreSQL you can use the serial or bigserial data type instead.
A new builder with auto increment enabled
Examples
identity
Makes the column an identity column. This only works on some dialects like MS SQL Server (MSSQL). For PostgreSQL’sgenerated always as identity use generatedAlwaysAsIdentity.
A new builder with identity enabled
Examples
primaryKey
Makes the column the primary key. If you want to specify a composite primary key use the CreateTableBuilder.addPrimaryKeyConstraint method.A new builder with primary key constraint
Examples
references
Adds a foreign key constraint for the column. If your database engine doesn’t support foreign key constraints in the column definition (like MySQL 5) you need to call the table level CreateTableBuilder.addForeignKeyConstraint method instead.Reference in format
table.column or schema.table.columnA new builder with the foreign key reference
Examples
onDelete
Adds anON DELETE constraint for the foreign key column.
If your database engine doesn’t support foreign key constraints in the column definition (like MySQL 5) you need to call the table level CreateTableBuilder.addForeignKeyConstraint method instead.
The action to take on delete (e.g.,
cascade, set null, restrict, no action)A new builder with the on delete action
Examples
onUpdate
Adds anON UPDATE constraint for the foreign key column.
If your database engine doesn’t support foreign key constraints in the column definition (like MySQL 5) you need to call the table level CreateTableBuilder.addForeignKeyConstraint method instead.
The action to take on update (e.g.,
cascade, set null, restrict, no action)A new builder with the on update action
Examples
unique
Adds a unique constraint for the column.A new builder with unique constraint
Examples
notNull
Adds aNOT NULL constraint for the column.
A new builder with not null constraint
Examples
unsigned
Adds anUNSIGNED modifier for the column.
This only works on some dialects like MySQL.
A new builder with unsigned modifier
Examples
defaultTo
Adds a default value constraint for the column.The default value (can be a primitive value or a SQL expression)
A new builder with default value
Examples
check
Adds a check constraint for the column.The check expression
A new builder with check constraint
Examples
generatedAlwaysAs
Makes the column a generated column using aGENERATED ALWAYS AS statement.
The generation expression
A new builder with generated column definition
Examples
generatedAlwaysAsIdentity
Adds theGENERATED ALWAYS AS IDENTITY specifier.
This only works on some dialects like PostgreSQL.
For MS SQL Server (MSSQL)‘s identity column use identity.
A new builder with generated always as identity
Examples
generatedByDefaultAsIdentity
Adds theGENERATED BY DEFAULT AS IDENTITY specifier on supported dialects.
This only works on some dialects like PostgreSQL.
For MS SQL Server (MSSQL)‘s identity column use identity.
A new builder with generated by default as identity
Examples
stored
Makes a generated column stored instead of virtual. This method can only be used with generatedAlwaysAs.A new builder with stored modifier
Examples
modifyFront
Adds any additional SQL right after the column’s data type.The SQL expression to add
A new builder with the front modifier
Examples
nullsNotDistinct
AddsNULLS NOT DISTINCT specifier. Should be used with unique constraint.
This only works on some dialects like PostgreSQL.
A new builder with nulls not distinct
Examples
ifNotExists
AddsIF NOT EXISTS specifier. This only works for PostgreSQL.
A new builder with if not exists
Examples
modifyEnd
Adds any additional SQL to the end of the column definition.The SQL expression to add
A new builder with the end modifier
Examples
$call
Calls the provided function passingthis as the only argument.
A function that receives the builder and returns a value
The return value of the provided function