Skip to main content
The 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

create_domain syntax diagram

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 DEFAULT and CHECK expressions 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 on DECIMAL 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 a CHECK constraint named us_phone_format:
To view the domains in the current database, use :

Use a domain in a table

Use the domains created in the preceding examples as column types:
The list_price column inherits the domain’s DEFAULT value:
A value that violates the price_check constraint returns an error:
The error message includes the constraint name that was specified at creation:

Known limitations

Refer to .

See also