每日一练:openGauss数据库在线实训课程 ## 第八天作业

0.分区表

分区表是把逻辑上的一张表根据某种方案分成几张物理块进行存储,这张逻辑上的表称之为分区表,物理块称之为分区。
分区表是一张逻辑表,不存储数据,数据实际是存储在分区上的。

范围分区表:将数据基于范围映射到每一个分区,这个范围是由创建分区表时指定的分区键决定的。这种分区方式是
最为常用的。
范围分区功能,即根据表的一列或者多列,将要插入表的记录分为若干个范围(这些范围在不同的分区里没有重叠),然后为每个范围创建一个分区,用来存储相应的数据。用户在CREATE TABLE时增加PARTITION参数,即表示针对此表应用数据分区功能。

1.创建一个含有5个分区的范围分区表store,在每个分区中插入记录

create table store
(
    c1 int,
    c2 varchar(2)
)
partition by range (c1)
(
    partition store_p0 values less than (50),
    partition store_p1 values less than (100),
    partition store_p2 values less than (150),
    partition store_p3 values less than (200),
    partition store_p4 values less than (1000)
);
\d+ store
select * from pg_partition;

insert into store values (1, 'a'), (50, 'b'), (100, 'c'), (180, 'd'), (300, 'e');
insert into store values (1001, 'f'); -- ERROR:  inserted partition key does not map to any table partition

2.查看分区1上的数据

select * from store partition(store_p0);

3.重命名分区2

\d+ store
alter table store rename partition store_p1 to store_p1_1;
\d+ store

4.删除分区5

alter table store drop partition store_p4;

5.增加分区6

alter table store add partition store_p5 values more than (2000); -- 错误
alter table store add partition store_p5 values less than (2000);
insert into store values (1500, 'z');

6.在系统表pg_partition中查看分区信息

select relname, parttype, partkey from pg_partition where relname='store';

select user from current_user;

# 查看表 分区信息
SELECT pg_get_partition_def('schema.tbname'::regclass,true);
SELECT pg_get_partition_def('omm.store'::regclass,true); -- 不好使
SELECT pg_get_partition_def('gaussdb.store'::regclass,true); --不好使

7.删除分区表

drop table store;

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/hezuijiudexiaobai/article/details/121801872