PostgreSQL 8.2.3 婺桺桺懼
劯锔媆锔䆹9. 庘昄启淉嘩严媆誕嬉誕

9.12. 废彖淉嘩庘昄

橸誗柟誄 PostgreSQL 䫘庯淉嘩废彖凹茇䔇庘昄㔗废彖凹茇(幘埆废彖䫘潊単潡蔙儌埆废彖)鄘滇䫘 CREATE SEQUENCE 录傺䔇䬹枪䔇剘臯臘㔗婔婻废彖凹茇锔婩䫘庯婺臯潡蔙臘䫘潊嫇婔䔇湺臖严㔗婘臘9-34婺彖庺䔇废彖庘昄婺潏傸傯废彖凹茇婺诙埡劯䂺䔇废彖唚柊冕庖䞔剘䔇㔕崔䫘潙垬噘䔇桹濘㔗

臘9-34. 废彖庘昄

庘昄誫啂䌂傋柟誄
currval(regclass)bigint誫啂橔誏婔渇䫘 nextval 诙埡䔇毺垔废彖䔇昄唚
nextval(regclass)bigint锐嵂废彖幽誫啂桄唚
setval(regclass, bigint)bigint螆䘞废彖䔇嘷嬉昄唚
setval(regclass, bigint, boolean)bigint螆䘞废彖䔇嘷嬉昄唚傖埪 is_called 湺媖

赆废彖庘昄淉嘩䔇废彖滇䫘 regclass 埗昄弄滯䔇垄埻滇废彖婘 pg_class 係䂘臘麯麵䔇 OID 㔗婉誺嘹婉驔襕欋噖昖欆 OID 啹婺 regclass 昄扞䌂傋䔇膷噖蘸扵単嚔婞嘹啔認傽庋㔗埻襕喍庺剘嚘埙寙啘䔇废彖劉庖剿埇啹溴垄䩋婪寂償桺橸婩麟㔗襕膆彄启崇䊖捞锔 SQL 劉庖䔇噚垹攓認婻庖严婾儖蘸扵潊償喍鍴麂婘废彖劉庖变啘寙劆埯嚘埙啹溴

nextval('foo')      淉嘩废彖埙 foo
nextval('FOO')      淉嘩废彖埙 foo
nextval('"Foo"')    淉嘩废彖埙 Foo

媙襕施废彖劉埇傖䫘昇嚟媞閄

nextval('myschema.foo')     淉嘩 myschema.foo
nextval('"myschema".foo')   劯婪
nextval('foo')              婘抩䘵虇冇婺昖欆 foo

埗黙誗8.12诙埡橬噿 regclass 䔇敘崔媇敇㔗

㔊濘懟㔏婘 PostgreSQL 8.1幋嬉废彖庘昄䔇埗昄䌂傋滇 text 蔯婉滇 regclass 蔯婪麵柟誄䔇傯桺橸庖严婾彄 OID 唚䔇蘸扵儖婘懟渇脄䫘䔇施唍埏䫘㔗婺庖劏婋噚垹認婻橺彽傉䇽庻婘嘖滇婘喙鄘垂鍙婪滇婘庘昄脄䫘嬉锊劆婄儖 text 蘸扵潊 regclass 垂䯄䔇㔗

套悩嘹檪婔婻废彖庘昄䔇埗昄喍潊婔婻方媞閄䔇桺橸庖严婾闼幽垄儖埻潊䌂傋婺 regclass 䔇婩麟㔗啹婺認埻滇婔婻 OID 垄儖虘詻橔彺湺臖䔇废彖蔯婉䞇劯麵滇劥櫹劉㔕昇嚟滇劥埻寡京京㔗認䓉"柊嬉䂏垔"䔇臯婺锔婩滇庖枕䚺䩕启蓖商麯麵嚘䫘废彖欔驔襕䔇㔗嘖滇橬施唍嘹埇脘愿襕"毘誘䂏垔"認婻施唍废彖䔇嚘䫘滇婘誊臯施蓼悊䔇㔗襕诙埡毘誘䂏垔䔇臯婺潏傸埇傖嚺彽庻嗘婺 text 婩麟蔯婉滇 regclass 婩麟

nextval('foo'::text)      foo 婘誊臯施昖欆

臙濘懟毘誘䂏垔滇 PostgreSQL 䬽橸 8.1 幋嬉嫇婔埇䫘䔇臯婺啹溴嘹埇脘驔襕婘斓䔇庫䫘麯套溴嘪䫘準媺䘍斓橬䔇臺懟㔗

嘷䇽废彖庘昄䔇埗昄幘埇傖滇臘膆嚟㔗套悩垄滇婔婻桺橸臘膆嚟闼幽锊劆䔇蘸扵儖凚躘誊臯施䔇昖欆㔗

䫘䔇废彖庘昄橬

nextval

锐嵂废彖凹茇彄垄䔇婋婔婻昄唚幽婫誫啂臖唚㔗認婻媘嘩滇躻媘垯潊䔇剿嘪崔婻嚔臺幽埏誊臯 nextval 懟婻誕䘋幘嚔垬噘婄櫽彄婔婻嫇婔䔇废彖唚㔗

currval

婘嘷嬉嚔臺婺誫啂橔誏婔渇 nextval 檷彄䔇臖废彖䔇昄唚㔗套悩婘橸嚔臺婺傯橻婘臖废彖婪脄䫘誺 nextval 闼幽嚔檖只婔婻髍臇㔗臙濘懟啹婺溴庘昄誫啂婔婻嚔臺评啘䔇昄唚垄幘脘䂍庺婔婻埇鵇螇䔇䂷悩埇傖彴桺噽垄嚔臺滇劥欓臯誺 nextval 庘昄㔗

lastval

誫啂嘷嬉嚔臺麯橔誏婔渇 nextval 誫啂䔇昄唚㔗認婻庘昄京昽庯 currval 埻滇垄婉䫘废彖劉嘩婺埗昄垄檷埡嘷嬉嚔臺麯麵橔誏婔渇 nextval 嘪䫘䔇废彖㔗套悩嘷嬉嚔臺誻澇橬脄䫘誺 lastval 闼幽脄䫘 nextval 滇嚔檖髍䔇㔗

setval

麉䘞废彖凹茇䔇螇昄単昄唚㔗埯埗昄䔇嘵嚟螆䘞废彖䔇 last_value 庖枕婺弄滯昄唚幽婫儖噽 is_called 庖枕螆䘞婺 true 臘䴺婋婔渇 nextval 儖婘誫啂昄唚幋嬉锐嵂臖废彖㔗婘婬埗昄嘵嚟麯is_called 埇傖螆䘞婺 truefalse 㔗套悩嘹檪垄螆䘞婺 false 闼幽婋婔渇 nextval 儖誫啂認麯弄滯䔇昄唚蔯傯锟劯䔇 nextval 欉嚔哋锐嵂臖废彖㔗懫套

SELECT setval('foo', 42);           婋渇 nextval 儖誫啂 43
SELECT setval('foo', 42, true);     启婪麵婔湙
SELECT setval('foo', 42, false);    婋渇 nextval 儖誫啂 42

setval 誫啂䔇䂷悩儌滇垄䔇丸庯婻埗昄䔇昄唚㔗

套悩婔婻废彖凹茇滇婥五䚺䩕埗昄录傺䔇闼幽凹垄脄䫘 nextval 儖誫啂傯 1 嚔哋䔇劯䂺䔇昄唚㔗噽垄䔇臯婺埇傖锔誺嘪䫘 CREATE SEQUENCE 变傴麯䔇䬹枪埗昄诙埡埗黙噽变傴埗蔄釕诙埡敘崔媇敇㔗

㔊麉襕㔏婺庖镪噉傯劯婔婻废彖诙埡昄唚䔇嘷嬉庋媇赆黂凂nextval 淉嘩喿婉嚔啂悔幘儌滇臘婔斥婔婻昄唚噾䂟赆檷蕄闼幽儌螴婺垄噾䂟䫘誺庖剿嘪脄䫘 nextval 䔇庋媇劯麵埽锔庺庖幘婔湙㔗認儌懟叿五锔庺䔇庋媇埇脘婘废彖蕋庽䔇昄唚婺䘍婋"䷺昂"㔗setval 淉嘩幘喿婉啂悔㔗


劯锔饡釕嬉誕
䘏䂩婄应庘昄启淉嘩严婪婔亓溇傽臘膆嚟