对于不需要我使用引号 ("") 进行查询的 PostgreSQL 对象(字段、表等),替代单词分隔符而不是下划线 (_)?
Alternative word delimiter than underscore (_) for PostgreSQL objects (fields, tables, etc.) which doesn't require me to use quotes ("") to query?
对于 PostgreSQL 对象(字段、tables 等),是否有除下划线 (_) 之外的其他单词分隔符不需要我使用引号 ("") 来查询?
特殊分隔符将用于根据来源分隔同名字段名称 table。我不想为此使用模式或不同的 table。一些虚拟示例字段名称为:
product_line^product_name
product_line^product_description
service_station^station_name
我测试了帽子 (^) 和管道 (|),但它们不起作用。
CREATE TABLE "my^test^table" ( "my^test^field" text )
(我使用 GUI 来构建 table,我现在才注意到您必须引用名称才能创建 table/field。)
这是我查询它必须要做的事情:
SELECT "my^test^field" FROM "my^test^table"
...虽然我想做:
SELECT my^test^field FROM my^test^table
...如果我使用了下划线 (_),这是可能的:
SELECT my_test_field FROM my_test_table
除了下划线 (_) 之外,还有其他可行的分隔符吗?最好是非字母数字的普通 ASCII 字符。
编辑:目前我最好的选择是使用双下划线。但是,我更喜欢别的东西。
请不要将此答案作为行动建议。我认为你正在做的事情会让你的生活变得更加复杂。但是你正在寻找的是可行的。要获得接受的未引用字符列表,我 运行:
t=# create schema s;
CREATE SCHEMA
Time: 0.931 ms
t=# do
$$
declare i record;
begin
for i in 1..255 loop
begin
execute format ('create table s.%s(i int)',chr(i)); --I deliberately don't use %I - so it would not add dblquotes for Identifier
exception when others then NULL;
end;
end loop;
end;
$$
;
DO
Time: 49.443 ms
t=# select tablename from pg_tables where tablename !~ '[aA-zZ]';
这将为您提供接受的字符列表。您可以根据它进行实验,例如:
t=# create table a©b (i int);
CREATE TABLE
Time: 2.920 ms
t=# select * from a©b;
i
---
(0 rows)
或
t=# create table a-b (i int);
CREATE TABLE
Time: 1.707 ms
t=# select * from a-b;
i
---
(0 rows)
注意不是连字符 (45),而是 (173) 一个:
t=# select ascii('-'),ascii('-');
ascii | ascii
-------+-------
173 | 45
(1 row)
所以任何 ÷, ® © ¤ £ ¡¦
等等都可以解决问题。但现在看着他们思考 - 也许你应该解决不同的任务?..
您可以使用美元符号。来自 the documentation:
SQL identifiers and key words must begin with a letter (a-z, but also letters with diacritical marks and non-Latin letters) or an underscore (_). Subsequent characters in an identifier or key word can be letters, underscores, digits (0-9), or dollar signs ($). Note that dollar signs are not allowed in identifiers according to the letter of the SQL standard, so their use might render applications less portable.
我认为这不是个好主意。
我经常使用 主题模式。在单独的模式中收集属于特定子模型的对象(表、视图、函数等),例如:
inventory.items
inventory.get_items()
inventory.document_workflows
sale.contractors
sale.local_orders
etc
其中 inventory
和 sale
是模式。
根据此处的回答,我决定不使用其他字符作为前缀分隔符。我要使用双下划线 (__
),这很好解析。
我应该更清楚为什么我最终想要这个。我在这里留下的各种评论对此进行了解释。
这是一个非常接近我们可能结束的例子:
CREATE VIEW flat_data AS
SELECT
hub.id,
idx_dly.ror_sd AS idx_dly__ror_sd,
fx_dly.ror_sd AS fx_dly__ror_sd
FROM
hub
JOIN
index_daily_movement AS idx_dly ON hub.id = idx_dly.hub_id
JOIN
forex_daily_movement AS fx_dly ON hub.id = fx_dly.hub_id
...
在应用层现在可以做类似
的事情
field_name = ... e.g. 'idx_dly__ror_sd'
table_abr, base_field_name = field_name.split('__')
对于 PostgreSQL 对象(字段、tables 等),是否有除下划线 (_) 之外的其他单词分隔符不需要我使用引号 ("") 来查询?
特殊分隔符将用于根据来源分隔同名字段名称 table。我不想为此使用模式或不同的 table。一些虚拟示例字段名称为:
product_line^product_name
product_line^product_description
service_station^station_name
我测试了帽子 (^) 和管道 (|),但它们不起作用。
CREATE TABLE "my^test^table" ( "my^test^field" text )
(我使用 GUI 来构建 table,我现在才注意到您必须引用名称才能创建 table/field。)
这是我查询它必须要做的事情:
SELECT "my^test^field" FROM "my^test^table"
...虽然我想做:
SELECT my^test^field FROM my^test^table
...如果我使用了下划线 (_),这是可能的:
SELECT my_test_field FROM my_test_table
除了下划线 (_) 之外,还有其他可行的分隔符吗?最好是非字母数字的普通 ASCII 字符。
编辑:目前我最好的选择是使用双下划线。但是,我更喜欢别的东西。
请不要将此答案作为行动建议。我认为你正在做的事情会让你的生活变得更加复杂。但是你正在寻找的是可行的。要获得接受的未引用字符列表,我 运行:
t=# create schema s;
CREATE SCHEMA
Time: 0.931 ms
t=# do
$$
declare i record;
begin
for i in 1..255 loop
begin
execute format ('create table s.%s(i int)',chr(i)); --I deliberately don't use %I - so it would not add dblquotes for Identifier
exception when others then NULL;
end;
end loop;
end;
$$
;
DO
Time: 49.443 ms
t=# select tablename from pg_tables where tablename !~ '[aA-zZ]';
这将为您提供接受的字符列表。您可以根据它进行实验,例如:
t=# create table a©b (i int);
CREATE TABLE
Time: 2.920 ms
t=# select * from a©b;
i
---
(0 rows)
或
t=# create table a-b (i int);
CREATE TABLE
Time: 1.707 ms
t=# select * from a-b;
i
---
(0 rows)
注意不是连字符 (45),而是 (173) 一个:
t=# select ascii('-'),ascii('-');
ascii | ascii
-------+-------
173 | 45
(1 row)
所以任何 ÷, ® © ¤ £ ¡¦
等等都可以解决问题。但现在看着他们思考 - 也许你应该解决不同的任务?..
您可以使用美元符号。来自 the documentation:
SQL identifiers and key words must begin with a letter (a-z, but also letters with diacritical marks and non-Latin letters) or an underscore (_). Subsequent characters in an identifier or key word can be letters, underscores, digits (0-9), or dollar signs ($). Note that dollar signs are not allowed in identifiers according to the letter of the SQL standard, so their use might render applications less portable.
我认为这不是个好主意。
我经常使用 主题模式。在单独的模式中收集属于特定子模型的对象(表、视图、函数等),例如:
inventory.items
inventory.get_items()
inventory.document_workflows
sale.contractors
sale.local_orders
etc
其中 inventory
和 sale
是模式。
根据此处的回答,我决定不使用其他字符作为前缀分隔符。我要使用双下划线 (__
),这很好解析。
我应该更清楚为什么我最终想要这个。我在这里留下的各种评论对此进行了解释。
这是一个非常接近我们可能结束的例子:
CREATE VIEW flat_data AS
SELECT
hub.id,
idx_dly.ror_sd AS idx_dly__ror_sd,
fx_dly.ror_sd AS fx_dly__ror_sd
FROM
hub
JOIN
index_daily_movement AS idx_dly ON hub.id = idx_dly.hub_id
JOIN
forex_daily_movement AS fx_dly ON hub.id = fx_dly.hub_id
...
在应用层现在可以做类似
的事情field_name = ... e.g. 'idx_dly__ror_sd'
table_abr, base_field_name = field_name.split('__')