3.15. Domains
Domains behave as data types with optional built-in constraints. The main advantage of using domains is to have in one place a data type with a specific validation instead of creating separate columns in tables and applying to them the needed constraints to provide correctness when assigning values.
One simple example is an email column in different tables: the data type, semantics, and validation for this column are the same; thus, a neat approach is to create a domain called email with the needed constraints and assign it as the data type of the columns that require it. This is the principle of object reuse: no redundancy and no repeated creation of the same object with the same purpose. Additionally, you have only one object to make the changes when needed.
1234567
| General | |||
|---|---|---|---|
| # | Field | Group box | Description |
| 1 | Type | Data type | The data type. Besides the built-in types, the list also holds the user-defined types, domains and other types of the model. |
| 2 | Length | Data type | Length, or number of characters or bits, for the types that accept one, like |
| 3 | Precision | Data type | Precision, for the types that accept it, like |
| 4 | Dimension | Data type | Number of dimensions when the type is an array ( |
| 5 | Format | Data type | Read-only preview of the data type as it is written in the SQL code, with length, precision, dimension and other modifiers applied. |
| 6 | Default Value | The default value of the domain. It must be written in the format accepted by the domain's data type. | |
| 7 | Not null | Indicates whether the domain accepts null values. When checked, null values are rejected ( | |
123
| Check constraints | ||
|---|---|---|
| # | Field | Description |
| 1 | Name | The name of the check constraint being added to the domain. |
| 2 | Expression | The SQL expression of the check constraint, used to validate any value assigned to the domain. |
| 3 | Check constraints | The check constraints of the domain. Type the name and the expression in the fields above and use the add button |
References
Domain DDL
https://www.postgresql.org/docs/current/static/sql-createdomain.html