第七节——分区表

数据表分区学习

一、范围分区

  1. 用法:创建一个母表,指定一个列进行范围分区,之后再创建子表,对每个子表划定分区范围,对母表进行数据插入时,不仅会插入到母表,还会根据子表分区范围自动将数据插入到相应的子表
  2. 命令:
//创建母表指定列进行范围分区
create table tablename (列名1 数据类型,列名2 数据类型) partition by range(需要分区的列名);
//创建子表指定分区范围
create table sontable partition of mothertable for values from (min) to (max);
  1. 举例:
//创建一个母表testtable,并对id列进行范围分区
create table testtable (id int,name text) partition by range ( id);
//创建子表ttable1,分区范围为0-9
create table ttable1 partition of testtable for values from ( 0) to (9);
//创建子表ttable2,分区范围为10-19
create table ttable2 partition of testtable for values from (10) to (19);
//向母表testtable插入id=1的数据和id=11的数据
insert into testtable values (1,'qwe'),(11,'asd');
//查询母表testtable,子表ttable1和ttable2
数据全部都差入进母表,子表就收取符合范围的数据

在这里插入图片描述

  1. 注意:范围分区可以一次指定多个列进行分区,eg:对id和age进行范围分区
    在这里插入图片描述

二、列表分区

  1. 用法:创建一个母表后,指定一个列进行列表分区,随后创建子表并规定分区的列的关键字。子表只存储与关键字相同的数据
  2. 命令:
//创建母表,并指定列进行列表分区
CREATE TABLE mothertable (列名 数据类型,列名 数据类型) PARTITION BY LIST (列表分区的列名);
//创建子表,指定分区的关键字
CREATE TABLE sontable PARTITION OF 母表名 FOR VALUES IN (关键字);
  1. 举例
//创建母表testtable,并对name列进行列表分区
create table testtable (id int,name text) partition by list ( name);
//创建子表ttable1,并指定关键字qwe
create table ttable1 partition of testtable for values in ('qwe');
//创建子表ttable2,并指定关键字asd
create table ttable2 partition of testtable for values in ('asd');
//向母表testtable插入三条数据,(1,'qwe'),(2,'asd'),(3,'qwe');
insert into testtable values (1,'qwe'),(2,'asd'),(3,'qwe');
查询母表与子表,母表展示所有数据,子表只存储对应关键字的数据

在这里插入图片描述

  1. 注意:列表分区只能指定一个列进行列表分区,不能一次指定多列
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值