postgresql - 为什么 PostgreSQL 不喜欢大写的表名?

标签 postgresql quoted-identifier

我最近尝试在 PostgreSQL 中以大写名称创建一些表。但是为了查询它们,我需要将表名放在引号“TABLE_NAME”中。有什么方法可以避免这种情况并告诉 postgres 正常使用大写名称吗?

更新

此查询创建一个小写的表table_name

create table TABLE_NAME 
(
id integer,
name varchar(255)
)

但是,这个查询创建了一个大写名称的表 "TABLE_NAME"

create table "TABLE_NAME"
(
id integer,
name varchar(255)
)

问题是引号现在是名称的一部分了!! 在我的例子中,我没有手动创建表,另一个应用程序创建表并且名称是大写字母。当我想使用 CQL 时,这会导致问题通过 Geoserver 过滤。

最佳答案

如果您希望 postgres 保留关系名称的大小写,请将表名放在双引号中。

Quoting an identifier also makes it case-sensitive, whereas unquoted names are always folded to lower case. For example, the identifiers FOO, foo, and "foo" are considered the same by PostgreSQL, but "Foo" and "FOO" are different from these three and each other. (The folding of unquoted names to lower case in PostgreSQL is incompatible with the SQL standard, which says that unquoted names should be folded to upper case. Thus, foo should be equivalent to "FOO" not "foo" according to the standard. If you want to write portable applications you are advised to always quote a particular name or never quote it.)

来自 docs (强调我的)

引用示例:

t=# create table "UC_TNAME" (i int);
CREATE TABLE
t=# \dt+ UC

t=# \dt+ "UC_TNAME"
                      List of relations
 Schema |   Name   | Type  |  Owner   |  Size   | Description
--------+----------+-------+----------+---------+-------------
 public | UC_TNAME | table | postgres | 0 bytes |
(1 row)

没有引号的例子:

t=# create table UC_TNAME (i int);
CREATE TABLE
t=# \dt+ UC_TNAME
                      List of relations
 Schema |   Name   | Type  |  Owner   |  Size   | Description
--------+----------+-------+----------+---------+-------------
 public | uc_tname | table | postgres | 0 bytes |
(1 row)

所以如果你用引号创建表,你不应该跳过引号查询它。但是,如果您跳过创建对象的引号,该名称将折叠为小写,因此在查询中将使用大写名称 - 这样您就“不会注意到”它。

关于postgresql - 为什么 PostgreSQL 不喜欢大写的表名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43111996/

相关文章:

javascript - Node - 数据库查询回调未触发

postgresql - 如何交叉引用其他表的数据进行分区检查?

sql - 模式中所有表的 PL/PGSQL 动态触发器

sql-server-2008 - 在全局范围内实现 QUOTED_IDENTIFIER

sql - Postgresql 列不存在

sql - 错误 : Column does not exist

sql语句错误: "column .. does not exist"

oracle - 如何使用 oracle 创建一个带有小字符的表?

mysql - 将数据插入数据库以实现一对多关系的最快方法

sql - 导入 pg_dump sql 文件时出现语法错误