tags:

views:

45

answers:

1

I am getting started with Postgresql, I converted a MS SQL DB but I cannot understand why

SELECT * FROM MYTABLE

doesn't work

while

SELECT * FROM "MYTABLE" does

Moreover from another machine, connecting to the same DB I can run

SELECT * FROM MYTABLE --(without ")

All tests were done by using PGAdmin III Windows client. Postgresql is installed on Linux server.

It's a PG-newbie question, I hope you can help.

+4  A: 

By default, postgresql will convert unquoted identifiers in a SQL statement to lower-case; which isn't the same as doing a case-insensitive match.

So if you have defined a table called "TABLE", then you need to address it as "TABLE", since just TABLE will be interpreted as "table".

Quite simply, the best tactic is to avoid using upper-case identifiers in postgresql: the catalogues and most examples I've seen use lower-case words separated by underscores (the_thing). You can use mixed-case identifiers, but in that case you have two alternatives:

  • you can use mixed-case identifiers in your statements and simply accept that they all get folded to lower-case when they're actually stored.
  • you can commit to quoting them all the time, and being consistent with the case all the time.

These outline my recommendations in preference order: stick to lower case style, mix case and accept the folding, insist on mixed case and deal with quoting identifiers everywhere.

PS don't get me started on calling a table "table". I'm assuming that was just a (silly) example. It's also a bad example since "select * from table" produces a syntax error, so clearly that's not the statement you're actually trying to run.

araqnid
Thanks, I modified my question renaming TABLE to MYTABLE so it is clearer. But how to change the default behaviour? You said default is lowercase but ano another machine I was able to run the Query without "". So somehow in that case default was overridden... hw can I check this?