CreateTableBuilder API for defining database tables. Access it through db.schema.createTable().
Basic Table Creation
Create a simple table with columns:Column Types and Constraints
Common Data Types
Kysely supports standard SQL data types:sql tag:
Column Constraints
The column builder provides these constraint methods:primaryKey()- Mark as primary keynotNull()- Disallow NULL valuesunique()- Ensure uniquenessdefaultTo(value)- Set default valueunsigned()- Unsigned integers (MySQL)autoIncrement()- Auto-incrementing (MySQL, SQLite)check(expression)- Add check constraint
Auto-Incrementing IDs
Different databases use different approaches: MySQL/SQLite:Foreign Keys
Column-Level Foreign Keys
Add foreign key constraints directly to columns:Table-Level Foreign Keys
Some databases (like older MySQL versions) require table-level constraints:Composite Foreign Keys
Table Constraints
Primary Key Constraints
Define composite primary keys:Unique Constraints
nulls not distinct:
Check Constraints
Generated Columns
Virtual Generated Columns
Stored Generated Columns
Special Table Types
Temporary Tables
Conditional Creation
Create Table From SELECT
Advanced Modifiers
Front Modifiers
Add SQL after thecreate keyword: