数据库
首页 > 数据库> > mysql分区(partion)之range(范围)-----------01

mysql分区(partion)之range(范围)-----------01

作者:互联网

查看数据库是否支持分区 show variables like "%partition%"  
5.6版本更改了为 show plugins   (plugin  为插件,列出来以后最后一个插件就是)

1.根据列分区,如果要设置这个表的索引,只能是指定分区的这个列
create table t_prange(
id int not null,
fname varchar(30),
lname varchar(30),
hired date not null default '2019-01-01',
separated date not null default '9999-12-31',
job_code int not null,
store_id int not null
)

partition by range(store_id)(
partition p0 values less than (6),
partition p1 values less than (11),
partition p2 values less than (16),
partition p3 values less than (21)
);
查看创建表的信息,show create table t_prange; 可以看到和不分区的表的区别就是还可以看到分区的信息。
在根据以上创建表的信息是不能插入store_id大于20的,不然找不到分区。
  insert into t_prange(id,fname,lname,job_code,store_id) value(1,'aa','tt',100,10);
  insert into t_prange(id,fname,lname,job_code,store_id) value(1,'aa','tt',100,21);
以上两条sql,第二条就不能插入,原因是store_id 大于20.

当我们改一下表的分区结构
create table t_prange2(
id int not null,
fname varchar(30),
lname varchar(30),
hired date not null default '2019-01-01',
separated date not null default '9999-12-31',
job_code int not null,
store_id int not null
)

partition by range(store_id)(
partition p0 values less than (6),
partition p1 values less than (11),
partition p2 values less than (16),
partition p3 values less than maxvalue
);
同样的两条sql
  insert into t_prange2(id,fname,lname,job_code,store_id) value(1,'aa','tt',100,10);
  insert into t_prange2(id,fname,lname,job_code,store_id) value(1,'aa','tt',100,21);
当store_id大于16的时候,默认放到p3分区

标签:01,partion,partition,than,range,values,null,id,store
来源: https://blog.csdn.net/qq_37778018/article/details/98666164