无法将存储有前导零的数字值的varchar字段分区在PostgreSQL中。

huangapple go评论48阅读模式
英文:

Unable to partition varchar field storing numeric value with leading 0 in postgres

问题

我正在使用Postgres 13.9

以下是表结构:

创建表t5
(
id varchar(3), --使用varchar作为id类型,因为我想保留前导0。
fname varchar
)按id范围分区;

创建表t5_a作为t5的值从(001)到(010)的分区;
创建表t5_b作为t5的值从(010)到(020)的分区;
创建表t5_c作为t5的值从(020)到(030)的分区;

如果我尝试插入以下内容

插入到t5的值('007', 'Tom');

它会抛出以下错误:

错误:找不到与行关联的分区"t5"

英文:

I am using Postgres 13.9

Below is the table structure:-

create table t5
(
id varchar(3), --using id as varchar since I want to retain leading 0. 
fname varchar
)partition by range (id);

create table t5_a partition of t5 for values from (001) to (010);
create table t5_b partition of t5 for values from (010) to (020);
create table t5_c partition of t5 for values from (020) to (030);

If I try to insert below

insert into t5 values('007', 'Tom');

It throws below error:-

Error: no partition of relation "t5" found for row

答案1

得分: 0

我在范围内加了单引号,并且现在能插入数据:

创建表 t5_a 作为 t5 的值为从 ('001') 到 ('010') 的分区;
创建表 t5_b 作为 t5 的值为从 ('010') 到 ('020') 的分区;
创建表 t5_c 作为 t5 的值为从 ('020') 到 ('030') 的分区;

向 t5 插入值('007', 'Tom');

然而,需要注意的一点是,如果你尝试仅传入 7 而不是 '007' 来插入数据,它会抛出一个错误。

英文:

I added single quote in ranges and I am able to insert data now:-

create table t5_a partition of t5 for values from ('001') to ('010');
create table t5_b partition of t5 for values from ('010') to ('020');
create table t5_c partition of t5 for values from ('020') to ('030');

insert into t5 values('007', 'Tom');

However, one thing to note is, if you try to insert data by passing only 7 instead of '007', it will throw an error.

huangapple
  • 本文由 发表于 2023年5月17日 15:48:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269690.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定