AppDBDocsOpen console

Documentation DBL guide

Tables and constraints

Define the rows your application stores and the invariants each write must preserve.

Identify a row

A table declares named fields and a primary key. Supply a scalar for a single-column key and a tuple in declaration order for a compound key. Use Id<T> generated when the database should allocate a typed ID. A primary-key field cannot be changed by a patch.

notebook.dbl · books
table Book {
    id: UInt8,
    slug: Text,
    primary key(id),
    unique index by_slug {
        key: (slug),
        value: {id},
    },
}

The primary lookup for a book uses Book.get(id). The unique index reserves a second lookup key, its slug. Index entries store the projection listed in value, here the book ID.

Constrain individual writes

notebook.dbl · notes
table Note {
    id: UInt8,
    book: UInt8,
    body: Text,
    primary key(id),
    index by_book {
        key: (book, id),
        value: {body},
    },
    foreign key(book) references Book(id),
    check body != "",
}

The body check runs when a note is inserted or changed. A write with an empty body fails even if the caller intended to repair it later in the same transaction. A check can also call a pure helper.

The foreign key points at the complete primary key of Book. Its referencing field must be non-null and type-compatible. Supply an unconditional index starting with the referencing fields. Here by_book covers book and also serves a query.

Understand uniqueness and deletion

A unique index applies to its declared key components, independently of the projected fields. Null participates in uniqueness: two equal key tuples containing null still conflict. An ordinary secondary index can contain repeated keys.

The foreign key rejects an insert referencing a missing book and prevents deleting a book that still has notes. To remove a family of records, delete its notes first, then delete the book. Write the operations in that order inside a write function if they must commit together.

Change a live table

A default uses field: Type = expression. When submitting changes to a live database, choose the mode for the change and review the plan. A newly active version can overlap older clients that still use their own constraints. Retirement and verification matter when tightening rules. See Deploy and retire versions.