ALTER SEQUENCE
是用于修改序列定义的 DDL 命令。
ALTER SEQUENCE
添加于 PostgreSQL 7.4。
更改历史记录
- PostgreSQL 15
- 语法
SET [ LOGGED | UNLOGGED ]
添加 (提交 344d62fb)
- 语法
- PostgreSQL 14
ALTER SEQUENCE ... OWNER TO CURRENT_ROLE
语法添加 (提交 45b98057)
- PostgreSQL 10
- PostgreSQL 9.5
ALTER SEQUENCE ... OWNER TO { CURRENT_USER | SESSION_USER }
语法添加 (提交 31eae602)
- PostgreSQL 9.2
ALTER SEQUENCE IF EXISTS ...
语法添加 (提交 b8a91d9d)
- PostgreSQL 8.4
- PostgreSQL 8.3
ALTER SEQUENCE ... RENAME TO ...
语法添加 (提交 a5589813)
- PostgreSQL 8.2
ALTER SEQUENCE ... OWNED BY ...
语法添加 (提交 2b2a5072)
- PostgreSQL 8.1
ALTER SEQUENCE ... SET SCHEMA ...
语法添加 (提交 35508d1c)
- PostgreSQL 7.4
- 添加 (提交 5f65225f)
示例
ALTER SEQUENCE
的基本用法示例
postgres=# ALTER SEQUENCE foo_id_seq RENAME TO bar_id_seq; ALTER SEQUENCE
更改序列数据类型
postgres=# ALTER SEQUENCE bar_id_seq AS integer; ALTER SEQUENCE
如果序列的当前值超过新数据类型的最大值,则无法更改数据类型。
postgres=# CREATE SEQUENCE type_test_seq AS bigint; CREATE SEQUENCE postgres=# ALTER SEQUENCE type_test_seq RESTART 32768; ALTER SEQUENCE postgres=# ALTER SEQUENCE type_test_seq AS smallint; ERROR: RESTART value (32768) cannot be greater than MAXVALUE (32767)
参考文献
- PostgreSQL 文档: ALTER SEQUENCE