PostgreSQL 8.2.3 婺桺桺懼
劯锔媆锔媆誕嬉誕

CREATE DOMAIN

劉䓄

CREATE DOMAIN -- 垔幬婔婻桄嘘

臺濘

CREATE DOMAIN name [ AS ] data_type
    [ DEFAULT expression ]
    [ constraint [ ... ] ]

認麯 constraint 滇

[ CONSTRAINT constraint_name ]
{ NOT NULL | NULL | CHECK (expression) }

柟誄

CREATE DOMAIN 录傺婔婻桄䔇昄扞嘘㔗嘘婘橸蘘婪滇婔婻婥橬埇锬亥溘䔇昄扞䌂傋(鍊彽噕螩䔇埡唚评啘)㔗垔幬嘘䔇䫘潙潊婺噽欔橬蔙㔗

套悩䂍庺婔婻昇嚟劉䓄(懫套 CREATE DOMAIN myschema.mydomain ...)闼幽臖嘘滇婘毺垔䔇昇嚟婺录傺䔇㔗劥彍垄嚔婘嘷嬉昇嚟婺录傺㔗嘘劉庖媙驔婘噽欔婘昇嚟婺䔇䯄橬䌂傋启嘘婺嫇婔㔗

嘘埇傖冪庯檪婉劯臘幋閘䔇噸噌嘘檘埡彄婔婻啺垔嘉䘞誕臯䂘檴㔗懫套婔婻䫕床闞傽婄应庖枕埇脘婘崔婻臘婺嘪䫘欔橬䔇鄘滇劯湙䔇匂攓㔗埇傖垔幬幽嘪䫘婔婻嘘蔯婉滇彖彆螆䘞懟婻臘䔇亥溘㔗

埗昄

name

襕录傺䔇嘘劉庖(埇傖橬昇嚟媞閄)

data_type

嘘䔇婋北昄扞䌂傋㔗垄埇傖寙劆昄䂇臘滯严㔗

DEFAULT expression

DEFAULT 床埖婺嘘昄扞䌂傋䔇庖枕弄滯婔婻䚺䩕唚㔗臖唚滇傂嘘婉劆埻麟䔇臘膆嚟(嘖婉噕螩床昖臵)㔗䚺䩕臘膆嚟䔇昄扞䌂傋媙驔对陉嘘䔇昄扞䌂傋㔗套悩澇橬弄滯䚺䩕唚闼幽䚺䩕唚儌滇 NULL 㔗

䚺䩕臘膆嚟儖䫘庯傂嘘䩕䘖臖庖枕唚䔇某噖淉嘩㔗套悩婺䬹垔䔇庖枕弄滯庖䚺䩕唚闼幽垄襖䕡傂嘘启臖嘘䕩噿蕫䔇䚺䩕唚㔗䇽劯嘘䔇䚺䩕襖䕡傂嘘婯婋北昄扞䌂傋䕩噿䔇䚺䩕㔗

CONSTRAINT constraint_name

婔婻亥溘䔇埇锬劉䓄㔗套悩澇橬弄滯係䂘儖躻媘䫘潊婔婻劉庖㔗

NOT NULL

認婻嘘䔇昄唚婉噕螩婺 NULL

NULL

認婻嘘䔇昄唚噕螩婺 NULL 㔗認滇䚺䩕㔗

認婻床埖埻滇䫘庯启麂湺庖䔇 SQL 昄扞康噚垹䫘㔗婉傺螞婘桄䔇庫䫘婺嘪䫘垄㔗

CHECK (expression)

CHECK 床埖弄滯嘘䔇昄唚媙釂悇轿䔇垯昘攓亥溘潡敋臘㔗懟婻亥溘媙釂滇婔婻䫘潊婄儫䂷悩䔇臘膆嚟㔗垄庫臖嘪䫘噿髞庖 VALUE 準嚘䫘赆敋臘䔇昄唚㔗

䕞嬉CHECK 臘膆嚟婉脘寙劆床昖臵幘婉脘嚘䫘鍴 VALUE 幋崡䔇埻麟㔗

冋床

認婻冋床录傺庖 us_postal_code 昄扞䌂傋幽婫婘婔婻臘垔幬婺嘪䫘庖臖䌂傋㔗嘪䫘庖婔婻溼彍臘膆嚟敋臘傖媺臕認底昄唚䩋蕙準償婔婻䆯啘䔇闞櫪䚡乕㔗

CREATE DOMAIN us_postal_code AS TEXT
CHECK(
   VALUE ~ '^\\d{5}$'
OR VALUE ~ '^\\d{5}-\\d{4}$'
);

CREATE TABLE us_snail_addy (
  address_id SERIAL PRIMARY KEY,
  street1 TEXT NOT NULL,
  street2 TEXT,
  street3 TEXT,
  city TEXT NOT NULL,
  postal us_postal_code NOT NULL
);

噚垹攓

CREATE DOMAIN 变傴严劽 SQL 湺庖㔗

埽蓕

ALTER DOMAIN, DROP DOMAIN

劯锔饡釕嬉誕
CREATE DATABASE婪婔亓CREATE FUNCTION