注意PostgreSQL的关键字(保留字)和identified的用法和位置

关键字即词法解析时用到的一些固定的单词,identifier则是用户定义的一些名词(如表名,索引名,字段名,函数名等等)
PostgreSQL 有一张关键字列表
https://www.postgresql.org/docs/9.5/static/sql-keywords-appendix.html
在这个列表中的关键字,如果出现的位置可以是identified,则会报错。
https://www.postgresql.org/docs/9.5/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
例子:

postgres=# select 1 and;
ERROR:  syntax error at or near ";"
LINE 1: select 1 and;
                    ^

and就是一个关键字,并且他出现在了可以是identifer的位置,所以要解决这个问题就是加双引号,或者排除歧义。
使用双引号引排除歧义,把它变成别名identifier。

postgres=# select 1 "and";
 and 
-----
   1
(1 row)

使用as改变词法,排除歧义,把它变成别名。

postgres=# select 1 as and;
 and 
-----
   1
(1 row)

还有,如果key words出现在identifier位置时,还可能是定义名称的位置。
例子

postgres=# create table and (id int);
ERROR:  syntax error at or near "and"
LINE 1: create table and (id int);
                     ^
postgres=# create table "and" (id int);
CREATE TABLE

postgres=# drop table and;
ERROR:  42601: syntax error at or near "and"  -- 这里明确告诉你and错误了
LINE 1: drop table and;
                   ^
LOCATION:  scanner_yyerror, scan.l:1087
postgres=# drop table "and";
DROP TABLE

所以,如果你遇到类似的错误,用双引号,或者换名字即可。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值