-- 分区表
-- 创建主表
CREATE TABLE measurement (
city_id int not null,
logdate date not null,
peaktemp int,
unitsales int
)
PARTITION BY RANGE (logdate);
-- 创建子表
CREATE TABLE measurement_2019_07
PARTITION OF measurement
FOR VALUES FROM ('2019-07-01') TO ('2019-07-31');
-- insert data
insert into measurement
values (1, '2019-07-01', 1, 2);
-- error 没有对应的子表
insert into measurement
values (1, '2019-08-01', 1, 2);
CREATE TABLE measurement_2019_08
PARTITION OF measurement
FOR VALUES FROM ('2019-08-01') TO ('2019-08-30');
-- ok 找到了对应的子表
insert into measurement
values (1, '2019-08-01', 1, 2);
-- detach 子表
ALTER TABLE measurement
DETACH PARTITION measurement_2019_08;
select *
from measurement; -- 只能看到一条内容
-- attach 子表
ALTER TABLE measurement
attach PARTITION measurement_2019_08 for VALUES FROM ('2019-08-01') TO ('2019-08-30');
select *
from measurement; -- 可以看到两条内容了
-- detach
ALTER TABLE measurement
DETACH PARTITION measurement_2019_08;
select *
from measurement;
-- 更新表字段
alter table measurement
add test char(200) default '';
select *
from measurement;
select *
from measurement_2019_07; -- attach 的表是 alert 成功的
select *
from measurement_2019_08; -- detach 的表示没有被 alert 的
-- 将没有被 alert 的表 attach , 结果炸掉了 提示: child tables is missing column "test"
ALTER TABLE measurement
attach PARTITION measurement_2019_08 for VALUES FROM ('2019-08-01') TO ('2019-08-30');