开发者

PostgreSQL - query syntax without quotes

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:

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:

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:

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.


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"

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!)

See the manual for details:

http://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS

Edit:
Oracle behaves just 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.


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.)


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.

The quotes are optional and you can usually skip them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜