postgresql - Postgresql 按列分区,没有主键
问题描述
我希望能够通过worksite_uuid
. 但worksite_uuid
需要可以为空,并且我需要uuid
成为主键。
我能够创建分区的唯一方法是 if worksite_uuid
is not nullable 像这样:
CREATE TABLE test (
uuid uuid DEFAULT uuid_generate_v1mc(),
worksite_uuid text,
notes text
)
PARTITION BY LIST(worksite_uuid)
//then add worksite_uuid and uuid as a primary key
create table test_worksite1 partition of test for values in ('1');
create table test_worksite2 partition of test for values in ('2');
有谁知道我如何创建一个仅uuid
作为主键的分区并使其可以为worksite_uuid
空?
--
示例:我不能这样做
CREATE TABLE test (
uuid uuid DEFAULT uuid_generate_v1mc() PRIMARY KEY,
worksite_uuid text,
notes text
)
PARTITION BY LIST(worksite_uuid)
我收到以下错误:
查询 1 错误:错误:分区表上的唯一约束必须包括所有分区列详细信息:表“test”上的 PRIMARY KEY 约束缺少作为分区键一部分的列“worksite_uuid”。
解决方案
为 NULL 值创建分区是没有问题的:
create table test_worksite_null partition of test for values in (NULL);
但是不可能有一个可以为空的主键列。你就是做不到。
我看到了两种方法:
你生活没有主键。相反,在每个单独的分区上创建主键。这不能保证全球唯一性,但几乎可以。
您使用另一个值而不是 NULL,例如 -1。
推荐阅读
- node.js - Node.js api - 我们可以根据谷歌地图获得两个纬度之间的准确距离,给出距离结果吗?
- r - 如何使用日期 yy/mm/dd hh:mm:ss 制作绘图数据?
- android - 使用 kotlin 在 MPAndroidChart 中将 xAsis 毫秒转换为 HH:mm 格式
- firebase - Crashlytics 在中国有效吗?
- java - 如何将数组输出格式化为下一行的 5 个块空间和另一半?
- android - 通过android在firebase中进行电话号码身份验证的问题
- javascript - NodeJS 承诺:我有竞争条件问题吗?
- pyspark - 错误:AttributeError:“DataFrame”对象没有属性“_jdf”
- java - 确切地说,按位运算符在 Java 中是如何工作的?
- c - 如何使用简单的指针算法访问结构的属性?