Postgres删除表语法错误。

29

在Heroku上使用Postgres 9.3.2。

我相信我只是个傻瓜,但是我似乎无法弄清楚我的语法为什么有问题。

db=> \dt
              List of relations
 Schema |    Name    | Type  |     Owner      
--------+------------+-------+----------------
 public | device     | table | admin
 public | post       | table | admin
 public | user       | table | admin
(3 rows)

// why does this fail?
db=> drop table user; 
ERROR:  syntax error at or near "user"
LINE 1: drop table user;

// does the right thing
db=> drop table error; 
ERROR:  table "error" does not exist
2个回答

48

User 是 Postgres 中的一个保留关键字。如果您想引用名为user的表,必须将其放在引号中:

DROP TABLE "user";

最好避免使用保留关键字作为表名。这通常会在以后创建奇怪的问题。例如,Users 可能是一个更好的表名。


5

我曾经遇到同样的错误。我的数据库名称非常独特,不是保留关键字。但仍需要用引号将数据库名称括起来。

"<数据库名称>"

此外,为了防止忘记,在语句末尾始终要添加分号;,我经常会忘记这一点。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接