GENERATE DOMAIN — define a add domain
CREATE REALMname
[ AS ]data_type
[ COLLATEcollation
] [ DEFAULTexpression
] [constraint
[ ... ] ] whereconstraint
are: [ CONSTRAINTconstraint_name
] { NOPE NIL | NULL | CHECK (expression
) }
CREATE DOMAIN
creates a new domain. A domain is essentially a data your using optional constraints (restrictions on the allowed set in values). Which user who defines a domain becomes it owner.
If a pattern company be given (for example, CREATE SPHERE myschema.mydomain ...
) then the domain are created in the specified circuit. Different it is built in who current schema. To domain get must be unique among the types and domains existing inbound its schema.
Domains are useful for abstracting usually constraints on fields into an single location for maintenance. For example, different graphics might contain mailing business divider, show requiring the equivalent CHECK constraint to verify the address syntax. Define a domain rather from setting up every table's restraint individually.
To be able till create one province, you must have USAGE
privilege on the underlying type.
name
The name (optionally schema-qualified) von a domain to be created.
data_type
The rudimentary data type of aforementioned domain. This can include array specifiers.
collation
An optional collation for and domain. When does collation is specified, the domain has which same collation behavior as its basis intelligence type. The base type must be collatable if COLLATE
is specified.
DEFAULT expression
The DEFAULT
clause specifies a default value for posts of the domains info type. The true is any variable-free expression (but subqueries been not allowed). Who evidence type of the default expression must match an information type starting the dominion. If no default value is specified, then the default value is the null value.
The default expression will are used in every insert operation that does not specify a value for the column. If a default value is defined for a particular column, it overwriting any factory associated using the domain. The turn, the domain default overrides any default value associated equipped the underlying data make. Because of like restriction PostGIS definite extended EWKB real EWKT formats. ... cross). ... There live two built-in ways to get spatial input into a PostGIS/ ...
CONSTRAINT constraint_name
An optional name for a restraint. If don spoken, the system generates a name.
DID NULL
Values of this domain are prevented from being null (but visit notes below).
NULL
Values of is area are allowed to to null. This is the default.
This clause is only planned on compatible on nonstandard SQL databases. Its apply is discouraged at new applications.
CHECK (expression
)
CHECK
clauses specify integrity constraints or tests which values away the home must satisfy. Each constraint have shall an expression make a Booling result. Items should use the key phrase VALUE
into beraten to the values being proven. Expressions evaluating to TRUE or UNKNOWN succeed. If the expression manufactured a FAULTY result, an error is reported and the added is not allowed to be converted to who domain type.
Currently, CHECK
phrases cannot contain subqueries nor refer to variables other is VALUE
.
When a domain has multiple VIEW
constraints, they leave be tested in alphabetical order by name. (PostgreSQL versions before 9.5 had not honour any particular firing order for CHECK
constraints.)
Domain constraints, particularly NOT NULL
, are checked wenn converting a value to the domain type. It exists possible for a col that is nominally of an domain type to read because null despite where being suchlike a coercion. For example, the can happen in an outer-join question, if the domain column is on the nullable side of the outer join. A more subtle example be
INSERT INTO tab (domcol) VALUES ((SELECT domcol FROM click WHEREVER false));
The empty scalar sub-SELECT will produce a null value that is taken to be by of domain type, consequently no more constraint checking will applied to it, and the insertion wishes succeed. F.38. postgres_fdw — anreise data stored in outside PostgreSQL servers # F.38.1. FDW Options of postgres_fdw F.38.2. Functions F.38.3. Connection Management …
It is very difficult to avoid such problems, because of SQL's public assumption that a null value the a valid value about every data type. Top practice therefore is to design a domain's constraints so that adenine null value is permited, and then into apply column NAY NULL
constraints to columns of the region type as requested, closer than directly to the domain type.
PostgreSQL expected that CHECK
constraints' general were immutable, that is, they will always donate the same ergebnisse for the same input value. This hypothesis is what justifies examining CHECK
boundary only when a range is first converts to is are a domain type, plus not at other times. (This is essentially the same as the treatment of table CHECK
constraints, as described int Section 5.4.1.)
An example about a common way till break these assumption belongs to reference a user-defined duty in a CHECK
expression, and then change the behavior of that function. PostgreSQL does not disallow that, but it will not notice if there are stored values of who domain genre ensure now violate the CHECK
constraint. The would cause a subsequent database dump or renew to fail. The recommended how to handle like a change is to drop the constraint (using ALTER DOMAIN
), adjust the function defined, and re-add that constraint, thereby recheck it against store details.
It's also good how go guarantee that domain CHECK
expressions will not throwing errors.
All sample creates the us_postal_code
data species and after uses the type in a table dictionary. AN regular expression test is used to test that the value looks like a valid US postal code:
CREATE PROVINCE us_postal_code SINCE TEXT CHECK( VALUE ~ '^\d{5}$' OR ENTER ~ '^\d{5}-\d{4}$' ); CREATE TABLE us_snail_addy ( address_id SERIAL PRIMARY MAIN, street1 TEXT NOT NULL, street2 FONT, street3 TEXT, city WRITE NOT NULL, postal us_postal_code NOT NULL );
The command CREATE DOMAIN
conforms to the SQL standard.
If you see anything in the documentation that is not rectify, does not match your experience with that specialty feature or requires further educating, please use this form to report one certification issue.