CREATE DOMAIN 中文man頁面
NAME
CREATE DOMAIN - 定義一個新域
SYNOPSIS
CREATE DOMAIN name [AS] data_type [ DEFAULT expression ] [ constraint [ ... ] ] where constraint is: [ CONSTRAINT constraint_name ] { NOT NULL | NULL | CHECK (expression) }
DESCRIPTION 描述
CREATE DOMAIN 創(chuàng)建一個新的數(shù)據(jù)域。 定義域的用戶成為其所有者。
如果給出一個模式名稱(比如,CREATE DOMAIN myschema.mydomain ...), 那么該域是在指定的模式中創(chuàng)建的。否則它會在當(dāng)前模式中創(chuàng)建。 域名字必需在其所在模式中的現(xiàn)有類型和域中***。
域可以便于我們把不同表之間的公共域抽取到一個位置進行維護。 比如,一個電子郵件地址字段可能在多個表中使用,所有的都是同樣的屬性。 我們可以定義并使用一個域,而不是分別設(shè)置每個表的約束。
PARAMETERS 參數(shù)
- name
要創(chuàng)建的域名字(可以有模式修飾)。- data_type
域的下層數(shù)據(jù)類型。它可以包含數(shù)組聲明字。- DEFAULT expression
- DEFAULT 子句為域數(shù)據(jù)類型的字段聲明一個缺省值。 該值是任何不含變量的表達式(但不允許子查詢)。 缺省表達式的數(shù)據(jù)類型必需匹配域的數(shù)據(jù)類型。如果沒有聲明缺省值, 那么缺省值就是空值。
缺省表達式將用在任何不為該字段聲明數(shù)值的插入操作。 如果為特定的字段聲明了缺省值,那么它覆蓋任何和該域相關(guān)聯(lián)的缺省值。 然后,域的缺省覆蓋任何與下層數(shù)據(jù)類型相關(guān)的缺省。 - CONSTRAINT constraint_name
一個約束的可選名稱。如果沒有聲明,系統(tǒng)生成一個名字。- NOT NULL
這個域的數(shù)值不允許為 NULL。- NULL
這個域的數(shù)值允許為空。它是缺省。
這個子句只是用于和非標(biāo)準(zhǔn)的 SQL 數(shù)據(jù)庫兼容用。 我們不建議在新的應(yīng)用中使用它。- CHECK (expression)
CHECK 子句聲明完整性約束或者是測試,域地數(shù)值必須滿足這些要求。 每個約束必須是一個生成一個布爾結(jié)果的表達式。它應(yīng)該使用名字 VALUE 來引用被測試的數(shù)值。
目前,CHECK 表達式不能包含子查詢,也不能引用除 VALUE 之外的變量。
EXAMPLES 例子
這個例子創(chuàng)建了 country_code 數(shù)據(jù)類型并且在一個表定義中使用了該類型:
CREATE DOMAIN country_code char(2) NOT NULL; CREATE TABLE countrylist (id integer, country country_code);
COMPATIBILITY 兼容性
CREATE DOMAIN 命令符合 SQL 標(biāo)準(zhǔn)。
SEE ALSO 參見
DROP DOMAIN [drop_domain(7)]
#p#
NAME
CREATE DOMAIN - define a new domain
SYNOPSIS
CREATE DOMAIN name [AS] data_type [ DEFAULT expression ] [ constraint [ ... ] ] where constraint is: [ CONSTRAINT constraint_name ] { NOT NULL | NULL | CHECK (expression) }
DESCRIPTION
CREATE DOMAIN creates a new data domain. The user who defines a domain becomes its owner.
If a schema name is given (for example, CREATE DOMAIN myschema.mydomain ...) then the domain is created in the specified schema. Otherwise it is created in the current schema. The domain name must be unique among the types and domains existing in its schema.
Domains are useful for abstracting common fields between tables into a single location for maintenance. For example, an email address column may be used in several tables, all with the same properties. Define a domain and use that rather than setting up each table's constraints individually.
PARAMETERS
- name
- The name (optionally schema-qualified) of a domain to be created.
- data_type
- The underlying data type of the domain. This may include array specifiers.
- DEFAULT expression
- The DEFAULT clause specifies a default value for columns of the domain data type. The value is any variable-free expression (but subqueries are not allowed). The data type of the default expression must match the data type of the domain. If no default value is specified, then the default value is the null value.
The default expression will be used in any insert operation that does not specify a value for the column. If a default value is defined for a particular column, it overrides any default associated with the domain. In turn, the domain default overrides any default value associated with the underlying data type.
- CONSTRAINT constraint_name
- An optional name for a constraint. If not specified, the system generates a name.
- NOT NULL
- Values of this domain are not allowed to be null.
- NULL
- Values of this domain are allowed to be null. This is the default.
This clause is only intended for compatibility with nonstandard SQL databases. Its use is discouraged in new applications.
- CHECK (expression)
- CHECK clauses specify integrity constraints or tests which values of the domain must satisfy. Each constraint must be an expression producing a Boolean result. It should use the name VALUE to refer to the value being tested.
Currently, CHECK expressions cannot contain subqueries nor refer to variables other than VALUE.
EXAMPLES
This example creates the country_code data type and then uses the type in a table definition:
CREATE DOMAIN country_code char(2) NOT NULL; CREATE TABLE countrylist (id integer, country country_code);
COMPATIBILITY
The command CREATE DOMAIN conforms to the SQL standard.
SEE ALSO
DROP DOMAIN [drop_domain(7)]