ITPub博客

首页 > 数据库 > PostgreSQL > PostgreSQL DBA(108) - pgAdmin(Don't do this:Don't use upper names)

PostgreSQL DBA(108) - pgAdmin(Don't do this:Don't use upper names)

原创 PostgreSQL 作者:husthxd 时间:2019-10-16 10:12:10 0 删除 编辑

no zuo no die系列,来自于pg的wiki。
这一节的内容是:不要区分大小写(表名&列名不要使用大写)。
理由是:

PostgreSQL folds all names - of tables, columns, functions and everything else - to lower case unless they’re “double quoted”.
So create table Foo() will create a table called foo, while create table “Bar”() will create a table called Bar.
These select commands will work: select from Foo, select from foo, select from “Bar”.
These will fail with “no such table”: select from “Foo”, select from Bar, select from bar.
This means that if you use uppercase characters in your table or column names you have to either always double quote them or never double quote them. That’s annoying enough by hand, but when you start using other tools to access the database, some of which always quote all names and some don’t, it gets very confusing.
Stick to using a-z, 0-9 and underscore for names and you never have to worry about quoting them.

跟Oracle类似,只不过Oracle默认是大写而PG默认是小写,同时在Oracle中如区分大小写使用起来也是相当的麻烦,PG也是如此。
如果想输出的区分大小写,那么通过设置alias name来处理:

[pg12@localhost ~]$ psql -d testdb
Timing is on.
Expanded display is used automatically.
psql (12.0)
Type "help" for help.
[local]:5432 pg12@testdb=# select id as "Id" from tbl;
 Id 
----
  1
(1 row)
Time: 78.231 ms
[local]:5432 pg12@testdb=#

参考资料
Don’t Do This

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/6906/viewspace-2660128/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
ITPUB数据库版块资深版主,对Oracle、PostgreSQL有深入研究。

注册时间:2007-12-28

  • 博文量
    1416
  • 访问量
    3855869