暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

openGauss每日一练第8天 | 分区表基本操作

原创 2021-12-08
913

1、创建分区表

----范围分区表

create table update_table(c1 int, c2 CHAR(2)) partition by range (c1) ( partition update_table_p0 values less than (50), partition update_table_p1 values less than (100), partition update_table_p2 values less than (150) );

----查看分区表信息

\d+ update_table; select relname,parttype,parentid,rangenum,intervalnum,partstrategy from pg_partition;

Image.png
----插入数据

insert into update_table values (1, 'a'), (50, 'b'), (100, 'c'); --超出范围的数据会报错 insert into update_table values (150, 'd');

Image.png
----查看各区上的数据

select * from update_table partition(update_table_p0);

Image.png

2、创建分区

alter table update_table add partition update_table_p3 values less than (200); insert into update_table values (150, 'd'); select * from update_table partition(update_table_p3);

Image.png

3、修改分区属性

alter table update_table rename partition update_table_p1 to update_table_p1_1; select relname,parttype,parentid,rangenum,intervalnum,partstrategy from pg_partition;

Image.png

4、删除分区

alter table update_table drop partition update_table_p0; select relname,parttype,parentid,rangenum,intervalnum,partstrategy from pg_partition;

Image.png

5、删除分区表

drop table update_table; \d+ update_table

Image.png

课后作业

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

create table store(c1 int, c2 CHAR(2)) partition by range (c1) ( partition store_p1 values less than (10), partition store_p2 values less than (20), partition store_p3 values less than (30), partition store_p4 values less than (40), partition store_p5 values less than (50) ); insert into store values (5, 'a'), (15, 'b'), (25, 'c'), (35, 'd'), (45, 'e');

Image.png

2、查看分区1上的数据

select * from store partition(store_p1);

Image.png

3、重命名分区2

alter table store rename partition store_p2 to store_p2_1; select relname,parttype,parentid,rangenum,intervalnum,partstrategy from pg_partition;

Image.png

4、删除分区5

alter table store drop partition store_p5; select relname,parttype,parentid,rangenum,intervalnum,partstrategy from pg_partition;

Image.png

5、增加分区6

alter table store add partition store_p6 values less than (60); insert into store values (55, 'f');

Image.png

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

\d+ store; select relname,parttype,parentid,rangenum,intervalnum,partstrategy from pg_partition;

Image.png

7、删除分区表

drop table store;

Image.png

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论