我已经在 postgres 中创建了序列。
postgres=# create sequence my_sequence start 5 minvalue 3 increment 1 cycle;
CREATE SEQUENCE
现在我正在尝试查询序列中的下一个值。
postgres=# select nextval("my_sequence");
ERROR: column "my_sequence" does not exist
LINE 1: select nextval("my_sequence");
但它给我错误,该序列不存在。但是,当我在 sequence_name 中使用单引号时,它工作正常:-
postgres=# select nextval('my_sequence');
nextval
---------
5
(1 row)
但根据 difference between single quote and double quote in sql , 双引号可以与任何用户定义的 sql 对象一起使用。因此,相应地 my_sequence 也是用户定义的对象。那么,为什么我无法访问它?
最佳答案
TL;DR:使用单引号,如
SELECT nextval('my_sequence');
nextval
的参数不是标识符,但类型为 regclass
:
\df nextval
List of functions
Schema | Name | Result data type | Argument data types | Type
------------+---------+------------------+---------------------+--------
pg_catalog | nextval | bigint | regclass | normal
(1 row)
regclass
是一种便利类型,在内部与无符号 4 字节对象标识符类型 oid
相同,但具有 type input function接受表、索引或序列名称作为输入。
因此您可以调用 nextval
并将表的名称 作为参数,字符串用单引号而不是双引号括起来。
关于sql - 为什么我不能在 postgres 中用双引号引用我的序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49555457/