Documentation Index
Fetch the complete documentation index at: https://docs.turso.tech/llms.txt
Use this file to discover all available pages before exploring further.
CREATE DOMAIN
Turso Extension: CREATE DOMAIN is a Turso-specific statement not available in standard SQLite. Domains require STRICT tables and the custom types experimental feature. This feature must be enabled before use.
Syntax
Description
A domain wraps a base type with validation constraints. When a value is written to a column of a domain type, the CHECK constraints and NOT NULL restriction (if any) are verified. If validation passes, the value is stored using the base type’s native format. When a value is read, it is returned unchanged. Domains are transparent to the query engine for operations like ORDER BY, indexing, arithmetic, and aggregation. A domain column behaves exactly like a column of its base type, with the addition of input validation. Domains work only with STRICT tables. Using a domain name in a non-STRICT table has no effect.Clauses
IF NOT EXISTS
Suppresses the error that would occur if a domain with the same name already exists. The existing domain is left unchanged.AS base-type
Specifies the underlying type. The base type can be a primitive type (integer, real, text, blob) or another domain, allowing domains to be layered.
DEFAULT
Sets a default value for columns of this domain type. A column-level DEFAULT overrides the domain-level DEFAULT.NOT NULL
Adds a NOT NULL constraint to the domain. Any attempt to insert or update a NULL value into a column of this domain type will fail, even if the column definition does not specify NOT NULL.CHECK
Adds a validation constraint. The expression can referencevalue to refer to the input value being checked. Multiple CHECK constraints can be specified and all must pass.
CONSTRAINT name CHECK
CHECK constraints can optionally be given a name for documentation purposes.Domain Chaining
A domain can use another domain as its base type. When domains are chained, all constraints in the chain are enforced from child to ancestor.UPDATE Enforcement
Domain constraints are enforced on UPDATE as well as INSERT.CAST Support
CAST applies the domain’s validation constraints:CHECK Constraints with Table-Level Checks
Domain CHECK constraints and table-level CHECK constraints both apply. The domain constraint is checked during encoding and the table CHECK is checked separately.Operations on Domain Columns
Domain columns support the same operations as their base type: arithmetic, comparisons, ordering, and aggregation.Dropping Domains
Domains are dropped with DROP DOMAIN, not DROP TYPE. Attempting to use DROP TYPE on a domain (or DROP DOMAIN on a type) results in an error. A domain cannot be dropped while any table column or another domain references it.Restrictions
- STRICT tables only: Using a domain type on a non-STRICT table is rejected at CREATE TABLE and ALTER TABLE ADD COLUMN time. Non-STRICT tables allow arbitrary type names as affinity hints, but domain constraints (CHECK, NOT NULL, DEFAULT) are only enforced on STRICT tables, so permitting them would be misleading.
- Cannot drop while in use: A domain cannot be dropped while any table column or another domain references it.
If a non-STRICT table was created with a type name that did not exist at the time (e.g.,
CREATE TABLE t(x mydom)), and a domain with that name is created afterwards (CREATE DOMAIN mydom AS ...), the domain constraints will not be retroactively enforced on the existing table. This is not a bug: non-STRICT tables treat all type names as plain affinity hints regardless of whether a matching domain exists. STRICT tables prevent this scenario because they reject unknown type names at CREATE TABLE time.- No UNIQUE, PRIMARY KEY, or REFERENCES: These constraints are not supported in domain definitions. Use table-level constraints instead.
- No duplicate clauses: Multiple DEFAULT or NOT NULL clauses in the same definition are rejected. Conflicting NULL and NOT NULL clauses are also rejected.
See Also
- DROP DOMAIN for removing domains
- CREATE TYPE for defining custom types with ENCODE/DECODE logic
- Data Types for an overview of the type system
- CREATE TABLE for STRICT table definitions