简体   繁体   中英

Which are the criteria for defining " as mandatory or not in a Postgresql query?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM