oracle PostgreSQL - 不带引号的查询语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6304268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PostgreSQL - query syntax without quotes
提问by veselej
I have a little silly question. I have installed a PostgreSQL DB Server, but when I run query, there is a problem with column identifier without quotes. I don't know why the quotes around identifiers are needed. My query:
我有一个小愚蠢的问题。我已经安装了 PostgreSQL 数据库服务器,但是当我运行查询时,列标识符没有引号存在问题。我不知道为什么需要标识符周围的引号。我的查询:
SELECT vc."CAR_ID"
FROM "VEL_CAR" vc, "VEL_DRIVER" vd, "VEL_DRIVER_CAR" vdc
WHERE vc."CAR_ID" = vdc."CAR_ID" and
vdc."DRIVER_ID" = vd."DRIVER_ID";
My practice from Oracle DB is not to use ". So in Oracle:
我在 Oracle DB 中的做法是不使用“。所以在 Oracle 中:
SELECT vc.CAR_ID
FROM VEL_CAR vc, VEL_DRIVER vd, VEL_DRIVER_CAR vdc
WHERE vc.CAR_ID = vdc.CAR_ID and
vdc.DRIVER_ID = vd.DRIVER_ID;
When I run this query without quotes in PostgreSQL it throws error about syntax:
当我在 PostgreSQL 中不带引号运行此查询时,它会引发有关语法的错误:
ERROR: column vc.car_id does not exist
LINE 1: SELECT vc.CAR_ID
Do you know why?
你知道为什么吗?
--SOLVED--Thank you, now I solved the problem! It was about table creation. I created table objects using pgAdminIII and i wrote table name and column names uppercased. pgAdminIII created query with quotas - because of the names was uppercased. So query had to be written with quotas.
--SOLVED--谢谢,现在我解决了问题!这是关于表的创建。我使用 pgAdminIII 创建了表对象,并且我写了大写的表名和列名。pgAdminIII 创建了带有配额的查询 - 因为名称是大写的。因此必须使用配额编写查询。
回答by a_horse_with_no_name
When you create your tables using double quotes, column and table names become case sensitive. So "car_id"
is a different name than "CAR_ID"
使用双引号创建表时,列名和表名区分大小写。所以"car_id"
是一个不同的名字"CAR_ID"
You need to create your tables without using double quotes, then the names are not case sensitive: car_id
is the same as CAR_ID
(note the missing quotes!)
您需要在不使用双引号的情况下创建表,然后名称不区分大小写:car_id
与CAR_ID
(注意缺少引号!)相同
See the manual for details:
详情请参阅手册:
http://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
http://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
Edit:
Oracle behavesjust the same way. The only difference is that Oracle stores names in upper case and Postgres stores them in lower case. But the behaviour when using quotes is identical.
编辑:
Oracle 的行为方式相同。唯一的区别是 Oracle 以大写形式存储名称,而 Postgres 以小写形式存储名称。但是使用引号时的行为是相同的。
回答by ypercube??
From Postgres documentation:
Quoting an identifier also makes it case-sensitive, whereas unquoted names are always folded to lower case. For example, the identifiers FOO, foo, and "foo" are considered the same by PostgreSQL, but "Foo" and "FOO" are different from these three and each other. (The folding of unquoted names to lower case in PostgreSQL is incompatible with the SQL standard, which says that unquoted names should be folded to upper case. Thus, foo should be equivalent to "FOO" not "foo" according to the standard. If you want to write portable applications you are advised to always quote a particular name or never quote it.)
引用标识符也使其区分大小写,而未引用的名称始终折叠为小写。例如,标识符 FOO、foo 和 "foo" 被 PostgreSQL 认为是相同的,但是 "Foo" 和 "FOO" 与这三个不同,并且彼此不同。(PostgreSQL 中将未加引号的名称折叠为小写与 SQL 标准不兼容,该标准规定未加引号的名称应折叠为大写。因此,根据标准,foo 应等同于“FOO”而不是“foo”。如果如果您想编写可移植的应用程序,建议您始终引用特定名称或从不引用它。)
回答by Sorrow
Seems to me that the table vc does not have a column named car_id
. Are you sure it is there? Do \d vel_car
to see the structure of the table.
在我看来,表 vc 没有名为car_id
. 你确定它在那里?做\d vel_car
看看表的结构。
The quotes are optional and you can usually skip them.
引号是可选的,您通常可以跳过它们。