CREATE DOMAIN statement

Use this statement to create a domain in a database.

Syntax
CREATE { DOMAIN | DATATYPE } [ AS ] domain-name data-type
[ [ NOT ] NULL ]
[ DEFAULT default-value ]
[ CHECK ( condition ) ]
domain-name : identifier
data-type :  built-in data type, with precision and scale
Parameters
  • DOMAIN | DATATYPE clause   It is recommended that you use CREATE DOMAIN, rather than CREATE DATATYPE, because CREATE DOMAIN is the ANSI/ISO SQL3 term.

  • NULL clause   This clause allows you to specify the nullability of a domain. When a domain is used to define a column, nullability is determined as follows:

    • Nullability specified in the column definition
    • Nullability specified in the domain definition
    • If the nullability was not explicitly specified in either the column definition or the domain definition, then the setting of the allow_nulls_by_default option is used.

  • CHECK clause   When creating a CHECK condition, you can use a variable name prefixed with the @ sign in the condition. When the data type is used in the definition of a column, such a variable is replaced by the column name. This allows CHECK conditions to be defined on data types and used by columns of any name.

Remarks

Domains are aliases for built-in data types, including precision and scale values where applicable. They improve convenience and encourage consistency in the database.

Domains are objects within the database. Their names must conform to the rules for identifiers. Domain names are always case insensitive, as are built-in data type names.

The user who creates a data type is automatically made the owner of that data type. No owner can be specified in the CREATE DATATYPE statement. The domain name must be unique, and all users can access the data type without using the owner as prefix.

Domains can have CHECK conditions and DEFAULT values, and you can indicate whether the data type permits NULL values or not. These conditions and values are inherited by any column defined on the data type. Any conditions or values explicitly specified on the column override those specified for the data type.

To drop the data type from the database, use the DROP statement. You must be either the owner of the data type or have DBA authority to drop a domain.

Permissions

Must have RESOURCE authority.

Side effects

Automatic commit.

See also
Standards and compatibility
  • SQL/2003   SQL/foundation feature outside of core SQL.

Example

The following statement creates a data type named address, which holds a 35-character string, and which may be NULL.

CREATE DOMAIN address CHAR( 35 ) NULL;

The following statement creates a data type named ID, which does not allow NULLS, and which is autoincremented by default.

CREATE DOMAIN ID INT
NOT NULL
DEFAULT AUTOINCREMENT;