开发者

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜