---
title: "CREATE DOMAIN"
url: "https://docs.yugabyte.com/stable/api/ysql/the-sql-language/statements/ddl_create_domain/"
---

# CREATE DOMAIN

Use the CREATE DOMAIN statement to create a user-defined data type with optional constraints.

## Synopsis

Use the `CREATE DOMAIN` statement to create a user-defined data type with optional constraints, such as range of valid values, `DEFAULT`, `NOT NULL`, and `CHECK`. Domains are useful to abstract data types with common constraints. For example, domain can be used to represent phone number columns that will require the same `CHECK` constraints on the syntax.

## Syntax

- [![Grammar Icon](/icons/file-lines.svg)Grammar](#grammar-1)
- [![Diagram Icon](/icons/diagram.svg)Diagram](#diagram-1)

```output.ebnf
create_domain ::= CREATE DOMAIN name [ AS ] data_type 
                  [ DEFAULT expression ] 
                  [ [ domain_constraint [ ... ] ] ]

domain_constraint ::= [ CONSTRAINT constraint_name ] 
                      { NOT NULL | NULL | CHECK ( expression ) }
```

#### create\_domain

CREATEDOMAIN[name](/stable/api/ysql/syntax_resources/grammar_diagrams#name)AS[data\_type](/stable/api/ysql/syntax_resources/grammar_diagrams#data-type)DEFAULT[expression](/stable/api/ysql/syntax_resources/grammar_diagrams#expression)[domain\_constraint](#domain-constraint)

#### domain\_constraint

CONSTRAINT[constraint\_name](/stable/api/ysql/syntax_resources/grammar_diagrams#constraint-name)NOTNULLNULLCHECK([expression](/stable/api/ysql/syntax_resources/grammar_diagrams#expression))

## Semantics

### *create\_domain*

### CREATE DOMAIN *name*

Specify the name of the domain. An error is raised if `name` already exists in the specified database.

### AS *data\_type*

Specify the underlying data type.

### DEFAULT *expression*

Set the default value for columns of the domain data type.

### *domain\_constraint*

#### CONSTRAINT *constraint\_name*

Specify the optional name for the constraint.

##### NOT NULL

Do not allow null values.

##### NULL

Allow null values (default).

##### CHECK ( *expression* )

Enforce a constraint that the values of the domain must satisfy and returns a Boolean value.

The key word VALUE should be used to refer to the value being tested. Expressions evaluating to TRUE or UNKNOWN succeed.

## Examples

```plpgsql
yugabyte=# CREATE DOMAIN phone_number AS TEXT CHECK(VALUE ~ '^\d{3}-\d{3}-\d{4}$');
```

```plpgsql
yugabyte=# CREATE TABLE person(first_name TEXT, last_name TEXT, phone_number phone_number);
```

## See also

- [DROP DOMAIN](/stable/api/ysql/the-sql-language/statements/ddl_drop_domain "DROP DOMAIN")
- [ALTER DOMAIN](/stable/api/ysql/the-sql-language/statements/ddl_alter_domain "ALTER DOMAIN")
