DOMAIN (used with the CREATE DOMAIN statement) lets you define a named, reusable data type that builds on an existing base type while adding business-specific rules such as NOT NULL, DEFAULT values, and CHECK constraints. Once created, the domain can be referenced in any table or view column exactly like a built-in type, centralizing data-quality logic and making future schema changes easier. Domains are stored in the system catalog, can be altered or dropped, and automatically propagate their rules to every column that uses them. They validate data on INSERT and UPDATE but do not retroactively scan existing rows unless those rows are touched. A single domain can be used thousands of times, ensuring consistency without duplicating constraint code. Support is defined in the SQL Standard; popular implementations include PostgreSQL and, more recently, MySQL and MariaDB.
domain_name
(identifier) - The new domain's name.base_data_type
(data type) - Any built-in or existing type the domain is based on.default_expression
(expression) - Value assigned when none is supplied.search_condition
(boolean expression) - Rule every value must satisfy.constraint_name
(identifier) - Optional name for a CHECK constraint.SQL-92 Standard; first major implementation in PostgreSQL 6.0
Inside a DOMAIN's CHECK clause, VALUE represents the data being validated. Use it just like a column name: `CHECK (VALUE > 0)`.
Yes. `ALTER DOMAIN domain_name SET NOT NULL;` will apply the NOT NULL rule to all future inserts and updates on columns that use the domain.
Use `DROP DOMAIN name CASCADE;` to automatically drop or alter dependent objects. Without CASCADE, the command fails if dependencies exist.
No. ENUM stores a fixed set of literal values, while DOMAIN can wrap any base type and attach arbitrary constraints, giving greater flexibility.