jiaju5213344 2018-05-03 03:20 采纳率: 0%
浏览 5617
已结题

PostgreSQL 10 如何在分区表上创建主键(唯一约束)

想要在主表上创建一个自增的唯一建,保证这个字段在全局唯一(包括所有子表)。
而又想用 PostgreSQL 10版本的分区表的功能。
但在创建表的时候,出现如下问题。看起来是 PostgreSQL 10版本不支持在分区表上创建 primary key 或者 unique。想请教一下,什么方法能实现这个需求呢?

这是在分区表上创建 unique

 create table test (id bigserial not null,
                    name varchar(32),
                    time timestamp(3) not null,
                    constraint test_unique_id unique(id))
                    partition by range (time);
ERROR:  unique constraints are not supported on partitioned tables
LINE 4: constraint test_unique_id unique(id))

这是在分区表上创建 primary key

 create table test (id bigserial primary key,
                    name varchar(32),
                    time timestamp(3) not null)
                    partition by range (time);
ERROR:  primary key constraints are not supported on partitioned tables
LINE 1: create table test (id bigserial primary key
  • 写回答

2条回答 默认 最新

  • 笨笨的程序员啊 2018-05-03 05:23
    关注

    实际上数据是按时间存储到各个子表中的,主表没有数据,在创建主表时指定主键 然后子表继承主表
    create table parent__table(
    id bigserial primary key ,
    name varchar(32),
    time timestamp(3) not null);

    建立子表,继承主表
    create table parent_table_2018_05(
    check (time>=date '2018-05-01' and time<date '2018-06-01'))
    inherits(parent_table);

    评论

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗