Skip to content

DDL Builders

from sqlspec.builder import (
    AlterOperation,
    AlterTable,
    ColumnDefinition,
    CommentOn,
    ConstraintDefinition,
    CreateIndex,
    CreateMaterializedView,
    CreateSchema,
    CreateTable,
    CreateTableAsSelect,
    CreateView,
    DDLBuilder,
    DropIndex,
    DropMaterializedView,
    DropSchema,
    DropTable,
    DropView,
    RenameTable,
    Truncate,
    create_temporal_table,
)

Schema, table, column, constraint, index, view, and materialized view DDL builders.

Table & Schema DDL

DDLBuilder

Bases: QueryBuilder

Base class for DDL builders (CREATE, DROP, ALTER, etc).

CreateTable

Bases: DDLBuilder, _IfNotExistsDDLMixin

Builder for CREATE TABLE statements with columns and constraints.

column

column(
    name,
    dtype,
    default=None,
    not_null=False,
    primary_key=False,
    unique=False,
    auto_increment=False,
    comment=None,
    check=None,
    generated=None,
    collate=None,
)

Add a column definition to the table.

primary_key_constraint

primary_key_constraint(columns, name=None)

Add a primary key constraint.

foreign_key_constraint

foreign_key_constraint(
    columns,
    references_table,
    references_columns,
    name=None,
    on_delete=None,
    on_update=None,
    deferrable=False,
    initially_deferred=False,
)

Add a foreign key constraint.

unique_constraint

unique_constraint(columns, name=None)

Add a unique constraint.

check_constraint

check_constraint(condition, name=None)

Add a check constraint.

in_schema

in_schema(schema_name)

Set the schema for the table.

partition_by

partition_by(partition_spec)

Set partitioning specification.

CreateTableAsSelect

Bases: DDLBuilder, _IfNotExistsDDLMixin

Builder for CREATE TABLE [IF NOT EXISTS] ... AS SELECT ... (CTAS).

Methods:

Name Description
- name

str): Set the table name.

- if_not_exists

Add IF NOT EXISTS.

- columns

str): Set explicit column list (optional).

- as_select

Set the SELECT source (SQL, SelectBuilder, or str).

AlterTable

Bases: DDLBuilder, _IfExistsDDLMixin

Builder for ALTER TABLE operations.

add_column

add_column(
    name,
    dtype,
    default=None,
    not_null=False,
    unique=False,
    comment=None,
    after=None,
    first=False,
)

Add a new column to the table.

drop_column

drop_column(name, cascade=False)

Drop a column from the table.

alter_column_type

alter_column_type(name, new_type, using=None)

Change the type of an existing column.

rename_column

rename_column(old_name, new_name)

Rename a column.

drop_constraint

drop_constraint(name, cascade=False)

Drop a constraint from the table.

DropTable

Bases: _SingleObjectDropBuilder

Builder for DROP TABLE [IF EXISTS] ... [CASCADE|RESTRICT].

RenameTable

Bases: DDLBuilder

Builder for ALTER TABLE ... RENAME TO ... statements.

Truncate

Bases: DDLBuilder, _CascadeRestrictDDLMixin

Builder for TRUNCATE TABLE ... [CASCADE|RESTRICT] [RESTART IDENTITY|CONTINUE IDENTITY].

CreateSchema

Bases: DDLBuilder, _IfNotExistsDDLMixin

Builder for CREATE SCHEMA [IF NOT EXISTS] schema_name [AUTHORIZATION user_name].

DropSchema

Bases: _SingleObjectDropBuilder

Builder for DROP SCHEMA [IF EXISTS] ... [CASCADE|RESTRICT].

Columns, Constraints & Comments

ColumnDefinition

Column definition for CREATE TABLE.

ConstraintDefinition

Constraint definition for CREATE TABLE.

AlterOperation

Represents a single ALTER TABLE operation.

CommentOn

Bases: DDLBuilder

Builder for COMMENT ON ... IS ... statements.

create_temporal_table

create_temporal_table(table, as_of, kind=None)

Create a table expression with temporal (time-travel) version clause.

Parameters:

Name Type Description Default
table str | Table | Expr

Table name or expression.

required
as_of Expr | str

Timestamp or SCN expression for the point-in-time.

required
kind str | None

Optional version kind. Defaults to dialect-appropriate value.

None

Returns:

Type Description
Table

Table expression with version clause that generates dialect-specific SQL.

Index & View DDL

CreateIndex

Bases: DDLBuilder, _IfNotExistsDDLMixin

Builder for CREATE [UNIQUE] INDEX [IF NOT EXISTS] ... ON ... (...).

DropIndex

Bases: _SingleObjectDropBuilder

Builder for DROP INDEX [IF EXISTS] ... [ON table] [CASCADE|RESTRICT].

CreateView

Bases: DDLBuilder, _IfNotExistsDDLMixin

Builder for CREATE VIEW [IF NOT EXISTS] ... AS SELECT ...

DropView

Bases: _SingleObjectDropBuilder

Builder for DROP VIEW [IF EXISTS] ... [CASCADE|RESTRICT].

CreateMaterializedView

Bases: DDLBuilder, _IfNotExistsDDLMixin

Builder for CREATE MATERIALIZED VIEW [IF NOT EXISTS] ... AS SELECT ...

DropMaterializedView

Bases: _SingleObjectDropBuilder

Builder for DROP MATERIALIZED VIEW [IF EXISTS] ... [CASCADE|RESTRICT].