【问题标题】:Why I am not able to refer to my sequence with double quotes in postgres?为什么我不能在 postgres 中用双引号引用我的序列?
【发布时间】:2018-03-29 11:59:05
【问题描述】:

我已经在 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 也是用户定义的对象。那么,为什么我无法访问它?

【问题讨论】:

    标签: sql postgresql database-sequence


    【解决方案1】:

    TL;DR:使用单引号,例如 in

    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 接受表、索引或序列名称作为输入。

    所以你可以用表的name作为参数调用nextval,字符串用单引号而不是双引号括起来。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-27
      • 1970-01-01
      相关资源
      最近更新 更多