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

CREATE SEQUENCE

劉䓄

CREATE SEQUENCE -- 垔幬婔婻桄废彖埏䫘単

臺濘

CREATE [ TEMPORARY | TEMP ] SEQUENCE name [ INCREMENT [ BY ] increment ]
    [ MINVALUE minvalue | NO MINVALUE ] [ MAXVALUE maxvalue | NO MAXVALUE ]
    [ START [ WITH ] start ] [ CACHE cache ] [ [ NO ] CYCLE ]
    [ OWNED BY { table.column | NONE } ]

柟誄

CREATE SEQUENCE 儖劏嘷嬉昄扞康麯嵂媹婔婻桄䔇废彖埙䫘潊単㔗寙拸录傺启彺哋寡婔婻桄䔇劉婺 name 䔇剘臯臘㔗䫘潊単儖赆嘪䫘溴变傴䔇䫘潙欔橬㔗

套悩䂍庺庖婔婻昇嚟劉闼幽臖废彖儌婘䂍垔䔇昇嚟婺录傺䔇㔗劥彍垄嚔婘嘷嬉昇嚟婺录傺㔗婘施废彖庻婘庯婔婻䬹枪䔇昇嚟婺啹溴录傺婘施废彖䔇施唍婉脘䂍庺昇嚟劉㔗废彖劉媙驔启劯婔昇嚟婺䔇噽垄废彖㔕臘㔕䘵嚘㔕蓖商䔇劉庖婉劯㔗

婘录傺废彖劯嘹埇傖嘪䫘 nextval, currval, setval 庘昄淉嘩废彖㔗認底庘昄婘誗9.12婺橬臥䂖桺懼㔗

儘䞇嘹婉脘䕘毖敘桄婔婻废彖嘖嘹埇傖嘪䫘

SELECT * FROM name;

演昖婔婻废彖䔇埗昄启嘷嬉䪽攕㔗䬹彆滇废彖䔇 last_value 庖枕滆䴺庖傂懟劯䆇誕䘋橔劯彖陉䔇昄唚㔗嘷䇽套悩噽垄誕䘋溼䓇悕婄嘪䫘 nextval 認底唚婘赆欷剄庺準䔇施唍埇脘儌噾䂟誺施庖㔗

埗昄

TEMPORARYTEMP

套悩弄滯庖認婻媞閄臉闼幽臖废彖凹茇埻婺認婻嚔臺录傺幽婫婘嚔臺䂷溘䔇施唍躻媘役鍴㔗婘婘施废彖庻婘䔇施唍鍴麂䫘昇嚟媞閄䔇劉庖嚘䫘劥彍劯劉愩幙废彖滇婉埇蓕䔇(婘劯婔嚔臺麯)㔗

name

儖襕录傺䔇废彖劉(埇傖䫘昇嚟媞閄)

increment

埇锬床埖 INCREMENT BY increment 毺垔废彖䔇準阪㔗婔婻溼昄儖䫘潊婔婻锐嵂䔇废彖婔婻蘘昄儖䫘潊婔婻锐废䔇废彖㔗䚺䩕唚滇 1 㔗

minvalue
NO MINVALUE

埇锬䔇床埖 MINVALUE minvalue 毺垔废彖䔇橔償唚㔗套悩澇橬弄滯認婻床埖潡蔙弄滯庖 NO MINVALUE 闼幽锐嵂废彖䔇䚺䩕婺 1 锐废废彖䔇䚺䩕婺 -263-1 㔗

maxvalue
NO MAXVALUE

埇锬䔇床埖 MAXVALUE maxvalue 毺垔废彖䔇橔崓唚㔗套悩澇橬弄滯認婻床埖潡蔙弄滯庖 NO MAXVALUE 闼幽锐嵂废彖䔇䚺䩕婺 263-1 锐废废彖䔇䚺䩕婺 -1 㔗

start

埇锬䔇床埖 START WITH start 毺垔废彖䔇蕙䗹㔗䚺䩕彺哋唚凹庯锐嵂废彖婺 minvalue 凹庯锐废废彖婺 maxvalue

cache

埇锬䔇床埖 CACHE cache 婺媆锘螪閞蔯婘喙庻麯鵇噽庻嗘崔儏婻废彖埙㔗橔償唚(幘滇䚺䩕唚)滇 1 臘䴺婔渇埻脘䫘潊婔婻唚幘儌滇臘澇橬䚷庻㔗

CYCLE
NO CYCLE

CYCLE 锬釹埇䫘庯嘪废彖彄膆 maxvalueminvalue 施埇冻䯇幽䂓䂺婋寂㔗幘儌滇套悩膆彄悕鍊䫘潊䔇婋婔婻昄扞儖彖彆滇 minvaluemaxvalue

套悩弄滯庖 NO CYCLE 闼幽婘废彖膆彄噽橔崓唚幋劯傂嘘凹 nextval 䔇脄䫘鄘嚺誫啂婔婻髍臇㔗套悩斵澇橬弄滯 CYCLE 幘澇橬弄滯 NO CYCLE 闼幽 NO CYCLE 滇䚺䩕㔗

OWNED BY table.column
OWNED BY NONE

OWNED BY 锬釹儖废彖噿蕫彄婔婻䬹垔䔇臘庖枕婪㔗認湙婘役鍴闼婻庖枕潡噽欔婘臘䔇施唍儖躻媘役鍴䂏垔䔇废彖㔗毺垔䔇臘启废彖媙釂赆劯婔婻䫘潙欔拖橬幽婫婘婘劯婔婻昇嚟婺㔗邻螴䔇 OWNED BY NONE 臘䴺婉庻婘認湙䔇噿蕫㔗

濘懟

嘪䫘 DROP SEQUENCE 役鍴婔婻废彖㔗

废彖滇嘺庯 bigint 誊䞖䔇啹溴噽评啘婉脘轙誺噆庖誗䔇昘昄评啘(-9223372036854775808 彄 9223372036854775807)㔗婘婔底埴蔕䔇广埄婪埇脘澇橬凹噆庖誗昘昄䔇䚡臏単櫇毕認䓉愙喕婋废彖嘪䫘捞锔䔇 integer 誊䞖评啘(-2147483648 彄 +2147483647)㔗

套悩 cache 崓庯婔幽婫認婻废彖凹茇儖赆䫘庯崔嚔臺幽埏䔇婺劽闼幽埇脘嚔橬婉埇鵇桍䔇䂷悩埏䫘㔗懟婻嚔臺婘懟渇螪閞废彖凹茇䔇誺䘋婺鄘儖彖陉幽䚷庻锟劯䔇废彖唚幽婫䕩庫嵂媹废彖凹茇䔇 last_value 㔗認湙劯婔婻庋媇婺䔇锟劯䔇 cache-1 渇 nextval 儖埻滇誫啂鵇噽彖陉䔇昄唚蔯婉滇嘪䫘媘废彖凹茇㔗啹溴傂嘘婘嚔臺婺彖陉庖剘澇橬嘪䫘䔇昄庖鄘儖婘嚔臺䂷溘施婵崌傯蔯凚躘废彖麯麵庺䯄"䷺昂"㔗

埥崡儘䞇係䂘媺臕婺崔婻嚔臺彖陉䋸䆋䔇废彖唚嘖滇套悩蔄荏欔橬嚔臺闼幽認婻昄唚埇脘嚔婵崌釺废㔗懫套套悩 cache 婺 10 闼幽嚔臺 A 媺䘍庖 1..10 幽婫誫啂 nextval=1 䇽劯嚔臺 B 埇脘嚔媺䘍 11..20 䇽劯婘嚔臺 A 䫘潊 nextval=2 幋嬉誫啂 nextval=11 㔗啹溴凹庯 cache 京庯婔䔇愙喕埇傖垬噘婄啺螆 nextval 唚滇釺废䫘潊䔇蔯套悩檪 cache 螆䘞婺崓庯婔闼幽嘹埻脘啺螆 nextval 唚攂滇媺毕嫇婔剘婉毬釺废䫘潊㔗劯湙last_value 儖埉滹傂嘘嚔臺媺䘍䔇橔劯昄唚婉䞇垄滇劥敆赆 nextval 誫啂㔗

埥崡婔婻蔄荏滇婘認湙䔇废彖婪欓臯䔇 setval 儖婉嚔赆噽垄嚔臺濘懟彄䕘彄垄傸䫘噬傡傸躻噌䚷庻䔇昄唚㔗

冋床

录傺婔婻埆 serial 䔇锐嵂废彖傯 101 嚔哋

CREATE SEQUENCE serial START 101;

傯溴废彖婺锬庺婋婔婻昄庖

SELECT nextval('serial');

 nextval
---------
     114

婘婔婻 INSERT 婺嘪䫘溴废彖

INSERT INTO distributors VALUES (nextval('serial'), 'nothing');

婘婔婻 COPY FROM 劯敘桄废彖

BEGIN;
COPY distributors FROM 'input_file';
SELECT setval('serial', max(id)) FROM distributors;
END;

噚垹攓

CREATE SEQUENCE 镕冻 SQL 埻橬婋麵䔇冋崡

埽蓕

ALTER SEQUENCE, DROP SEQUENCE

劯锔饡釕嬉誕
CREATE SCHEMA婪婔亓CREATE TABLE