create_sequence.7

来自「PostgreSQL 8.2中增加了很多企业用户所需要的功能和性能上的提高,其开」· 7 代码 · 共 221 行

7
221 行
字号
.\\" auto-generated by docbook2man-spec $Revision: 1.1.1.1 $.TH "CREATE SEQUENCE" "" "2008-01-03" "SQL - Language Statements" "SQL Commands".SH NAMECREATE SEQUENCE \- define a new sequence generator.SH SYNOPSIS.sp.nfCREATE [ TEMPORARY | TEMP ] SEQUENCE \fIname\fR [ INCREMENT [ BY ] \fIincrement\fR ]    [ MINVALUE \fIminvalue\fR | NO MINVALUE ] [ MAXVALUE \fImaxvalue\fR | NO MAXVALUE ]    [ START [ WITH ] \fIstart\fR ] [ CACHE \fIcache\fR ] [ [ NO ] CYCLE ]    [ OWNED BY { \fItable\fR.\fIcolumn\fR | NONE } ].sp.fi.SH "DESCRIPTION".PP\fBCREATE SEQUENCE\fR creates a new sequence numbergenerator. This involves creating and initializing a new specialsingle-row table with the name \fIname\fR. The generator will beowned by the user issuing the command..PPIf a schema name is given then the sequence is created in thespecified schema. Otherwise it is created in the current schema.Temporary sequences exist in a special schema, so a schema name may not begiven when creating a temporary sequence.The sequence name must be distinct from the name of any other sequence,table, index, or view in the same schema..PPAfter a sequence is created, you use the functions\fBnextval\fR,\fBcurrval\fR, and\fBsetval\fRto operate on the sequence. These functions are documented inin the documentation..PPAlthough you cannot update a sequence directly, you can use a query like.sp.nfSELECT * FROM \fIname\fR;.sp.fito examine the parameters and current state of a sequence. In particular,the last_value field of the sequence shows the last valueallocated by any session. (Of course, this value may be obsoleteby the time it's printed, if other sessions are actively doing\fBnextval\fR calls.).SH "PARAMETERS".TP\fBTEMPORARY or TEMP\fRIf specified, the sequence object is created only for thissession, and is automatically dropped on session exit. Existingpermanent sequences with the same name are not visible (in thissession) while the temporary sequence exists, unless they arereferenced with schema-qualified names..TP\fB\fIname\fB\fRThe name (optionally schema-qualified) of the sequence to be created..TP\fB\fIincrement\fB\fRThe optional clause INCREMENT BY \fIincrement\fR specifieswhich value is added to the current sequence value to create anew value. A positive value will make an ascending sequence, anegative one a descending sequence. The default value is 1..TP\fB\fIminvalue\fB\fR.TP\fBNO MINVALUE\fRThe optional clause MINVALUE \fIminvalue\fR determinesthe minimum value a sequence can generate. If this clause is notsupplied or \fBNO MINVALUE\fR is specified, thendefaults will be used. The defaults are 1 and-263-1 for ascending and descending sequences,respectively..TP\fB\fImaxvalue\fB\fR.TP\fBNO MAXVALUE\fRThe optional clause MAXVALUE \fImaxvalue\fR determinesthe maximum value for the sequence. If this clause is notsupplied or \fBNO MAXVALUE\fR is specified, thendefault values will be used. The defaults are263-1 and -1 for ascending and descendingsequences, respectively..TP\fB\fIstart\fB\fRThe optional clause START WITH \fIstart\fR  allows thesequence to begin anywhere. The default starting value is\fIminvalue\fR forascending sequences and \fImaxvalue\fR for descending ones..TP\fB\fIcache\fB\fRThe optional clause CACHE \fIcache\fR specifies howmany sequence numbers are to be preallocated and stored inmemory for faster access. The minimum value is 1 (only one valuecan be generated at a time, i.e., no cache), and this is also thedefault..TP\fBCYCLE\fR.TP\fBNO CYCLE\fRThe CYCLE option allows the sequence to wraparound when the \fImaxvalue\fR or \fIminvalue\fR has been reached by anascending or descending sequence respectively. If the limit isreached, the next number generated will be the \fIminvalue\fR or \fImaxvalue\fR, respectively.If NO CYCLE is specified, any calls to\fBnextval\fR after the sequence has reached itsmaximum value will return an error. If neitherCYCLE or NO CYCLE arespecified, NO CYCLE is the default..TP\fBOWNED BY \fItable\fB.\fIcolumn\fB\fR.TP\fBOWNED BY NONE\fRThe OWNED BY option causes the sequence to beassociated with a specific table column, such that if that column(or its whole table) is dropped, the sequence will be automaticallydropped as well. The specified table must have the same owner and be inthe same schema as the sequence.OWNED BY NONE, the default, specifies that thereis no such association..SH "NOTES".PPUse \fBDROP SEQUENCE\fR to remove a sequence..PPSequences are based on \fBbigint\fR arithmetic, so the rangecannot exceed the range of an eight-byte integer(-9223372036854775808 to 9223372036854775807). On some olderplatforms, there may be no compiler support for eight-byteintegers, in which case sequences use regular \fBinteger\fRarithmetic (range -2147483648 to +2147483647)..PPUnexpected results may be obtained if a \fIcache\fR setting greater than one isused for a sequence object that will be used concurrently bymultiple sessions. Each session will allocate and cache successivesequence values during one access to the sequence object andincrease the sequence object's last_value accordingly.Then, the next \fIcache\fR-1uses of \fBnextval\fR within that session simply return thepreallocated values without touching the sequence object. So, anynumbers allocated but not used within a session will be lost whenthat session ends, resulting in ``holes'' in thesequence..PPFurthermore, although multiple sessions are guaranteed to allocatedistinct sequence values, the values may be generated out ofsequence when all the sessions are considered. For example, witha \fIcache\fR setting of 10,session A might reserve values 1..10 and return\fBnextval\fR=1, then session B might reserve values11..20 and return \fBnextval\fR=11 before session Ahas generated nextval=2. Thus, with a\fIcache\fR setting of oneit is safe to assume that \fBnextval\fR values are generatedsequentially; with a \fIcache\fR setting greater than one youshould only assume that the \fBnextval\fR values are alldistinct, not that they are generated purely sequentially. Also,last_value will reflect the latest value reserved byany session, whether or not it has yet been returned by\fBnextval\fR..PPAnother consideration is that a \fBsetval\fR executed onsuch a sequence will not be noticed by other sessions until theyhave used up any preallocated values they have cached..SH "EXAMPLES".PPCreate an ascending sequence called serial, starting at 101:.sp.nfCREATE SEQUENCE serial START 101;.sp.fi.PPSelect the next number from this sequence:.sp.nfSELECT nextval('serial'); nextval---------     114.sp.fi.PPUse this sequence in an \fBINSERT\fR command:.sp.nfINSERT INTO distributors VALUES (nextval('serial'), 'nothing');.sp.fi.PPUpdate the sequence value after a \fBCOPY FROM\fR:.sp.nfBEGIN;COPY distributors FROM 'input_file';SELECT setval('serial', max(id)) FROM distributors;END;.sp.fi.SH "COMPATIBILITY".PP\fBCREATE SEQUENCE\fR conforms to the SQLstandard, with the following exceptions:.TP 0.2i\(buThe standard's AS <data type> expression is notsupported..TP 0.2i\(buObtaining the next value is done using the \fBnextval()\fRfunction instead of the standard's \fBNEXT VALUE FOR\fRexpression..TP 0.2i\(buThe OWNED BY clause is a PostgreSQLextension. .PP.SH "SEE ALSO"ALTER SEQUENCE [\fBalter_sequence\fR(7)], DROP SEQUENCE [\fBdrop_sequence\fR(l)]

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?