ddl.md

July 23, 2026 ยท View on GitHub

Ddl is a helper object that generates DDL (Data Definition Language) SQL fragments for creating and dropping tables. ColumnDef is a simple data class pairing a column name with a SQL type string and a nullability flag.

Normally you don't call Ddl directly โ€” Table#createTable and Table#dropTable use it internally. You may need Ddl directly when building custom DDL tooling.

Core API

object Ddl {
  def createTable(tableName: String, columns: IndexedSeq[ColumnDef]): Frag
  def dropTable(tableName: String): Frag
}

final case class ColumnDef(name: String, sqlType: String, nullable: Boolean)

Usage

Create a ColumnDef for each column, then pass them to Ddl.createTable:

import zio.blocks.sql._

val transactor: Transactor = JdbcTransactor.fromUrl("jdbc:sqlite::memory:", SqlDialect.SQLite)

val columns = IndexedSeq(
  ColumnDef("id",         "INTEGER", nullable = false),
  ColumnDef("name",       "TEXT",    nullable = false),
  ColumnDef("created_at", "TEXT",    nullable = true)
)

val createFrag = Ddl.createTable("users", columns)
val dropFrag   = Ddl.dropTable("users")

// Execute the fragments
transactor.transact {
  createFrag.update
  // ... do work ...
  dropFrag.update
}

How It Works

Ddl is typically used by Table#createTable, which derives ColumnDef from schema metadata:

  1. Table.derived[A] builds a schema.
  2. Table#createTable(dialect) converts schema columns to ColumnDef using dialect.typeName.
  3. Ddl.createTable receives the ColumnDef list and generates the SQL fragment.

You call Ddl directly only when you need custom DDL that doesn't fit the Table abstraction.

See Table for the high-level DDL API.