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
¶
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.
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.
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.
DropTable
¶
Bases: _SingleObjectDropBuilder
Builder for DROP TABLE [IF EXISTS] ... [CASCADE|RESTRICT].
RenameTable
¶
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
¶
create_temporal_table
¶
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
¶
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].