↑↓ 选择 ↵ 打开 ⌫ 改范围 完整检索页

pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。

受支持版本: 当前版本 (18) / 17 / 16 / 15 / 14
测试与开发版本: 19 / devel
不受支持的版本: 13 / 12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0 / 8.4 / 8.3 / 8.2 / 8.1 / 8.0 / 7.4 / 7.3 / 7.2 / 7.1 / 7.0 / 6.5 / 6.4
历史版本PostgreSQL 6.4 已于 2003 年 10 月结束社区维护,本页译文保留供仍在使用旧版本的读者参考。新系统请看当前版本。

CREATE SEQUENCE

CREATE SEQUENCE — 创建一个新的序列号发生器

大纲

CREATE SEQUENCE seqname
    [ INCREMENT increment ]
    [ MINVALUE  minvalue ]
    [ MAXVALUE  maxvalue ]
    [ START     start ]
    [ CACHE     cache ]
    [ CYCLE ]
  

输入

seqname

要创建的序列的名称。

increment

INCREMENT increment 子句是可选的。正值将生成 递增序列,负值生成递减序列。 默认值为一(1)。

minvalue

可选子句 MINVALUE minvalue 决定序列可以生成的 最小值。递增和递减序列的默认值分别是 1 和 -2147483647。

maxvalue

用可选子句 MAXVALUE maxvalue 决定序列的 最大值。递增和递减序列的默认值分别是 2147483647 和 -1。

start

可选的 START start 子句让序列可以从任何地方开始。 默认起始值:递增序列为 minvalue, 递减序列为 maxvalue。

cache

CACHE cache 选项 使序列号可以预分配 并存储在内存中以便更快访问。最小值 为 1(一次只能生成一个值,即不缓存), 这也是默认值。

CYCLE

可选的 CYCLE 关键字可以让序列在 递增或递减序列分别达到 maxvalue 或 minvalue 时 继续。如果到达了限制, 生成的下一个数将是 minvalue 或 maxvalue, 视情况而定。

输出

→ CREATE

命令成功时返回的消息。

→ ERROR: amcreate: 'seqname' relation already exists

指定的序列已存在。

→ ERROR: DefineSequence: START value (start) can't be > MAXVALUE (maxvalue)

指定的起始值超出范围。

→ ERROR: DefineSequence: START value (start) can't be < MINVALUE (minvalue)

指定的起始值超出范围。

→ ERROR: DefineSequence: MINVALUE (minvalue) can't be >= MAXVALUE (maxvalue)

如果最小值和最大值不一致。

描述

CREATE SEQUENCE will enter a new sequence number generator into the current data base. This involves creating and initialising a new single-row table with the name seqname. The generator will be "owned" by the user issuing the command.

序列创建之后,可以用函数 nextval(seqname) 从序列获取一个新的数。 函数 currval('seqname') 可用来确定当前会话中对指定序列最后一次调用 nextval(seqname) 所返回的数。 函数 setval('seqname', newvalue) 可用来设置指定序列的当前值。 下一次调用 nextval(seqname) 将返回给定的值加上序列增量。

Use a query like

SELECT * FROM sequence_name;
   

to get the parameters of a sequence. Aside from fetching the original parameters, you can use

SELECT last_value FROM sequence_name;
   

to obtain the last value allocated by any backend. parameters, you can use

使用低级锁来支持对一个发生器的多个 同时调用。

小心

Unexpected results may be obtained if a cache setting greater than one is used for a sequence object that will be used concurrently by multiple backends. Each backend will allocate "cache" successive sequence values during one access to the sequence object and increase the sequence object's last_value accordingly. Then, the next cache-1 uses of nextval within that backend simply return the preallocated values without touching the shared object. So, numbers allocated but not used in the current session will be lost. Furthermore, although multiple backends are guaranteed to allocate distinct sequence values, the values may be generated out of sequence when all the backends are considered. (For example, with a cache setting of 10, backend A might reserve values 1..10 and return nextval=1, then backend B might reserve values 11..20 and return nextval=11 before backend A has generated nextval=2.) Thus, with a cache setting of one it is safe to assume that nextval values are generated sequentially; with a cache setting greater than one you should only assume that the nextval values are all distinct, not that they are generated purely sequentially. Also, last_value will reflect the latest value reserved by any backend, whether or not it has yet been returned by nextval.

注解

要删除序列,请参阅 DROP SEQUENCE 语句。

每个后端用自己的缓存存储已分配的数字。 当前会话中已缓存但未使用的数字将会 丢失,从而在序列中留下"空洞"。

用法

创建一个名为 serial 的递增序列,从 101 开始:

CREATE SEQUENCE serial START 101;
  

从该序列中选择下一个数

SELECT NEXTVAL ('serial');
    
nextval
-------
    114
   

在 INSERT 中使用该序列:

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

在 COPY FROM 之后设置序列值:

CREATE FUNCTION distributors_id_max() RETURNS INT4
    AS 'SELECT max(id) FROM distributors' 
    LANGUAGE 'sql';
BEGIN;
    COPY distributors FROM 'input_file';
    SELECT setval('serial', distributors_id_max());
END;
   

兼容性

CREATE SEQUENCE 是一种 Postgres 语言扩展。

SQL92

SQL92 中没有 CREATE SEQUENCE 语句。

提交更正

译文有误、术语不当或页面显示问题,请到译文仓库 pgsty/pgdoc 报告译文问题。 英文原文本身的问题,请在当前版本的对应页面向上游反馈;上游不再修订已结束维护的版本。