CREATE TABLE
Table of contents
- Overview
- ColumnType — type-safe column definitions
- IF NOT EXISTS
- Composite primary key
- Method reference
Overview
CreateBuilder builds CREATE TABLE statements with optional IF NOT EXISTS guards and composite primary keys. Column types can be specified as raw SQL strings or as type-safe ColumnType constants and factory methods.
import com.github.ezframework.javaquerybuilder.query.builder.ColumnType;
import com.github.ezframework.javaquerybuilder.query.builder.QueryBuilder;
import com.github.ezframework.javaquerybuilder.query.sql.SqlResult;
SqlResult result = QueryBuilder.createTable("users")
.column("id", ColumnType.INT.notNull().autoIncrement())
.column("username", ColumnType.varChar(64).notNull().unique())
.column("balance", ColumnType.decimal(10, 2))
.column("created_at", ColumnType.TIMESTAMP)
.primaryKey("id")
.build();
// → CREATE TABLE users (
// id INT NOT NULL AUTO_INCREMENT,
// username VARCHAR(64) NOT NULL UNIQUE,
// balance DECIMAL(10, 2),
// created_at TIMESTAMP,
// PRIMARY KEY (id))
ColumnType — type-safe column definitions
ColumnType provides pre-defined constants for all common SQL types, factory methods for parameterised types, and chainable modifier methods for column-level constraints.
Fixed-width types
Parameterised factory methods
| Method | SQL output | Description |
|---|---|---|
ColumnType.varChar(n) | VARCHAR(n) | Variable-length string up to n chars |
ColumnType.charType(n) | CHAR(n) | Fixed-length string of n chars |
ColumnType.decimal(p, s) | DECIMAL(p, s) | Exact decimal with p total and s fraction digits |
ColumnType.numeric(p, s) | NUMERIC(p, s) | Exact numeric; equivalent to DECIMAL in most databases |
ColumnType.binary(n) | BINARY(n) | Fixed-length binary of n bytes |
ColumnType.varBinary(n) | VARBINARY(n) | Variable-length binary up to n bytes |
ColumnType.timestamp(p) | TIMESTAMP(p) | Timestamp with p fractional-seconds digits (0–6) |
Modifier methods (chainable)
Modifiers return a new ColumnType instance and can be chained in any order:
| Method | Appends | Example |
|---|---|---|
.notNull() | NOT NULL | ColumnType.INT.notNull() → INT NOT NULL |
.unique() | UNIQUE | ColumnType.varChar(64).unique() → VARCHAR(64) UNIQUE |
.autoIncrement() | AUTO_INCREMENT | ColumnType.INT.notNull().autoIncrement() → INT NOT NULL AUTO_INCREMENT |
.defaultValue(val) | DEFAULT val | ColumnType.BOOLEAN.defaultValue("false") → BOOLEAN DEFAULT false |
.defaultValue(val)inserts the string verbatim into the SQL. Use only static, known-safe values (e.g."0","false","CURRENT_TIMESTAMP"). Never pass user-supplied input.
Custom types
For database-specific types not covered by the built-in constants, pass a raw SQL string directly to column() or use the public ColumnType constructor:
// Raw SQL string (backward compatible)
.column("geom", "GEOMETRY NOT NULL")
// Public constructor for custom type
.column("geom", new ColumnType("GEOMETRY NOT NULL"))
IF NOT EXISTS
SqlResult result = QueryBuilder.createTable("sessions")
.ifNotExists()
.column("token", ColumnType.varChar(128).notNull())
.column("user_id", ColumnType.INT)
.column("expires_at", ColumnType.TIMESTAMP)
.primaryKey("token")
.build();
// → CREATE TABLE IF NOT EXISTS sessions (
// token VARCHAR(128) NOT NULL,
// user_id INT,
// expires_at TIMESTAMP,
// PRIMARY KEY (token))
Composite primary key
SqlResult result = QueryBuilder.createTable("user_roles")
.column("user_id", ColumnType.INT)
.column("role_id", ColumnType.INT)
.primaryKey("user_id")
.primaryKey("role_id")
.build();
// → CREATE TABLE user_roles (user_id INT, role_id INT, PRIMARY KEY (user_id, role_id))
