PostgreSQL:“用户”处或附近的语法错误。 [英] Postgresql: syntax error at or near "user"
问题描述
这可能是一个简单的问题,但我花了很长时间才能找出问题所在。
This is probably a simple question, but I've spent an embarrassing amount of time trying to figure out what's wrong.
我正在尝试运行查询在包含两列用户名和ID的表用户上。
I'm trying to run a query on a table "user" containing two columns username and id.
INSERT INTO user
(username, id)
VALUES
("user", 2)
我最终遇到此错误。
ERROR: syntax error at or near "user"
LINE 1: INSERT INTO user
^
********** Error **********
ERROR: syntax error at or near "user"
SQL state: 42601
Character: 13
此处是供参考的表创建
-- Table: public."user"
-- DROP TABLE public."user";
CREATE TABLE public."user"
(
id integer NOT NULL,
username text COLLATE pg_catalog."default",
CONSTRAINT user_pkey PRIMARY KEY (id)
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE public."user"
OWNER to postgres;
推荐答案
在Postgres 用户中
是保留的SQL关键字。您应该避免使用保留关键字来命名表。作为一种解决方法,您可以在引用表名时将其放在双引号中:
In Postgres user
is a reserved SQL keyword. You should avoid naming your tables using reserved keywords. As a workaround here, you can place your table name in double quotes when referring to it:
INSERT INTO "user"
(username, id)
VALUES
('user', 2)
我也切换为字符串文字使用单引号。这有助于区分双引号和单引号的使用。
I also switched to using single quotes for string literals. This helps to distinguish the use of double quotes from single ones.
这篇关于PostgreSQL:“用户”处或附近的语法错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!