简体   繁体   English

哪些是在Postgresql查询中定义“强制与否”的标准?

[英]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 我开始使用Postgresql,我转换了MS SQL DB,但我无法理解为什么

SELECT * FROM MYTABLE

doesn't work 不起作用

while

SELECT * FROM "MYTABLE" does

Moreover from another machine, connecting to the same DB I can run 此外,从另一台机器,连接到相同的DB我可以运行

SELECT * FROM MYTABLE --(without ")

All tests were done by using PGAdmin III Windows client. 所有测试均使用PGAdmin III Windows客户端完成。 Postgresql is installed on Linux server. Postgresql安装在Linux服务器上。

It's a PG-newbie question, I hope you can help. 这是一个PG新手问题,我希望你能提供帮助。

By default, postgresql will convert unquoted identifiers in a SQL statement to lower-case; 默认情况下,postgresql会将SQL语句中的不带引号的标识符转换为小写; 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". 因此,如果您已经定义了一个名为“TABLE”的表,那么您需要将其称为“TABLE”,因为只有TABLE将被解释为“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). 很简单,最好的策略是避免在postgresql中使用大写标识符:目录和我见过的大多数示例使用由下划线(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". PS不要让我开始调用表“表”。 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. 这也是一个不好的例子,因为“select * from table”会产生语法错误,所以很明显,这不是你实际尝试运行的语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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