Best unofficial Apache Server developers community
Username
Forgot password?
Sign in with Twitter account
Sign in with Facebook account

PostgreSQL - query syntax without quotas

0

38 views

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 quotas. I dont know why the quotas 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 quotas 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?

asked June 10, 2011 4:10 am CDT
posted via StackOverflow

4 Answers

1
 

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

answered June 10, 2011 4:23 am CDT
0
 

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.

answered June 10, 2011 4:23 am CDT
0
 

There are two kinds of identifiers:

  • unquoted identifier e.g. car_id
  • quoted identifier e.g. "CaA_iD"

First one is case insensitive, so you can write car_id or CaR_iD and it's same identifier as quoted "car_id" (such behavior is PostgreSQL oriented and is not compliant with SQL standard, because it should be equivalent to "CAR_ID"). Second one is case sensitive, so "car_id" is diffrent from "CaR_iD".

You can find more information at 4.1. Lexical Structure.

answered June 10, 2011 4:23 am CDT
0
 

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

answered June 10, 2011 4:23 am CDT

Your answer

Join with account you already have


Sign in with Twitter account
Sign in with Facebook account
Sign in with Google Friend Connect

Preview
Similar questions
Postgresql query
May 24, 2011