CREATE DOMAIN creates a new in a . A domain is based on an existing data type, with optional that restrict its allowed values and an optional value. After the domain is created, it can only be referenced from the database that contains the domain.
The
CREATE DOMAIN statement performs a schema change. For more information about how online schema changes work in CockroachDB, refer to .Synopsis
Parameters
Required privileges
To create a domain, the user must have on the parent database and the schema in which the domain is being created. To use a domain in a table (for example, when defining a column’s type), the user must have on the domain.Details
- Columns that use a domain data type enforce the domain’s constraints on , , and cast operations.
- Domain
DEFAULTandCHECKexpressions cannot reference database objects such as tables, sequences, , or user-defined types. A domain can be based on a user-defined type such as an . - The base type of a domain cannot be an array, composite, or domain data type.
Examples
Create a domain with a default value and constraints
The following statement creates a domain based onDECIMAL that defaults to 0, disallows NULL values, and rejects negative values:
Create a domain with a named constraint
The following statement creates a domain with aCHECK constraint named us_phone_format:
Use a domain in a table
Use the domains created in the preceding examples as column types:list_price column inherits the domain’s DEFAULT value:
price_check constraint returns an error:

