PostgreSQL: Advanced SQL: Domains
- Creates a basic type with optional constraints
- Symbolic link to an existing type
- Centralise application logic
01 CREATE DOMAIN name [AS] data_type
02 [ DEFAULT expression ]
03 [ [ CONSTRAINT constraint_name ]
04 { NOT NULL | NULL | CHECK (expression) } ]
- data_type = base data type
- DEFAULT = default value for type
- CONSTRAINT = declare constraint name (system generates one by default)
- NOT NULL = values in domain cannot be NULL
- NULL = values can be NULL
- CHECK() = a check constraint
- Example: an email type
01 template1=# create domain email as text NOT NULL CHECK(length(value)>0
AND position('@' in value> 0);
02 CREATE DOMAIN
03 template1=# select 'this'::email;
04 ERROR: value for domain email violates check constraint "$1"
Page 28
|