--用列做分区,分区原则是列的内容
create table custaddr
(
id varchar2(15 byte) not null,
areacode varchar2(4 byte)
)
partition by list (areacode)
(
partition t_list025 values ('025'),
partition t_list372 values ('372') ,
partition t_list510 values ('510'),
partition p_other values (default)
);
commit;
select * from custaddr;
select * from custaddr partition(t_list025) ;
select * from custaddr partition(t_list372) ;
select * from custaddr partition(t_list510) ;
select * from custaddr partition(p_other) ;
--范围分区
create table graderecord
(
sno varchar2(10),
sname varchar2(20),
dormitory varchar2(3),
grade int
)
partition by range(grade)
(
partition bujige values less than(60), --不及格
partition jige values less than(85), --及格
partition youxiu values less than(maxvalue) --优秀
);
commit;
select * from graderecord;
select * from graderecord partition(bujige);
select * from graderecord partition(jige);
select * from graderecord partition(youxiu);
---散列分区,计算字段值的hashcode进行分区,可以是单独一个字段hash,也可以是多个字段
create table graderecord1
(
sno varchar2(10),
sname varchar2(20),
dormitory varchar2(3),
grade int
)
partition by hash(sno)
(
partition p1,
partition p2,
partition p3
);
commit;
select * from graderecord1 partition(p1);
select * from graderecord1 partition(p2);
select * from graderecord1 partition(p3);
--复合分区(范围-散列分区,范围-列表分区)
--子分区如果是通过hashcode分区,可以是单个字段,也可以多个字段
create table graderecord4
--先通过范围分区,然后再联合sname和sno进行复合分区,其实就是子分区
(
sno varchar2(10),
sname varchar2(20),
dormitory varchar2(3),
grade int
)
partition by range(grade)
subpartition by hash(sno) --subpartition by hash(sno,sname)
(
partition p1 values less than(75)
(
subpartition sp1 --, subpartition sp2
),
partition p2 values less than(maxvalue)
(
subpartition sp3 --, subpartition sp4
)
);
select * from graderecord4 partition(p1);
select * from graderecord4 partition(p2);
select * from graderecord4 subpartition(sp1);
--select * from graderecord subpartition(sp2);
select * from graderecord4 subpartition(sp3);
--select * from graderecord subpartition(sp4);
--复合分区之范围-列表分区(无模板)
create table MobileMessage
(
ACCT_MONTH VARCHAR2(6), -- 帐期 格式:年月 YYYYMM
AREA_NO VARCHAR2(10), -- 地域号码
DAY_ID VARCHAR2(2), -- 本月中的第几天 格式 DD
SUBSCRBID VARCHAR2(20), -- 用户标识
SVCNUM VARCHAR2(30) -- 手机号码
)
partition by range(ACCT_MONTH,AREA_NO) subpartition by list(DAY_ID)
(
partition p1 values less than('200705','012')
(
subpartition shangxun1 values('01','02','03','04','05','06','07','08','09','10'),
subpartition zhongxun1 values('11','12','13','14','15','16','17','18','19','20'),
subpartition xiaxun1 values('21','22','23','24','25','26','27','28','29','30','31')
),
partition p2 values less than('200709','014')
(
subpartition shangxun2 values('01','02','03','04','05','06','07','08','09','10'),
subpartition zhongxun2 values('11','12','13','14','15','16','17','18','19','20'),
subpartition xiaxun2 values('21','22','23','24','25','26','27','28','29','30','31')
),
partition p3 values less than('200801','016')
(
subpartition shangxun3 values('01','02','03','04','05','06','07','08','09','10'),
subpartition zhongxun3 values('11','12','13','14','15','16','17','18','19','20'),
subpartition xiaxun3 values('21','22','23','24','25','26','27','28','29','30','31')
)
) ;
--range(ACCT_MONTH,AREA_NO) subpartition by list(DAY_ID)
select * from MobileMessage;
select * from MobileMessage partition(p1);
select * from MobileMessage partition(p2);
select * from MobileMessage partition(p3);
select * from MobileMessage subpartition(shangxun1);
select * from MobileMessage subpartition(zhongxun1);
select * from MobileMessage subpartition(xiaxun1);
select * from MobileMessage subpartition(shangxun2);
select * from MobileMessage subpartition(zhongxun2);
select * from MobileMessage subpartition(xiaxun2);
select * from MobileMessage subpartition(shangxun3);
select * from MobileMessage subpartition(zhongxun3);
select * from MobileMessage subpartition(xiaxun3);
---含有模板的一种复合分区,依据范围-列表进行分区,适合海量数据
create table UnionMessage
(
ACCT_MONTH VARCHAR2(6), -- 帐期 格式:年月 YYYYMM
AREA_NO VARCHAR2(10), -- 地域号码
DAY_ID VARCHAR2(2), -- 本月中的第几天 格式 DD
SUBSCRBID VARCHAR2(20), -- 用户标识
SVCNUM VARCHAR2(30) -- 手机号码
)
partition by range(ACCT_MONTH,AREA_NO) subpartition by list(DAY_ID)
subpartition template
(
subpartition sub1 values('01'),subpartition sub2 values('02'),
subpartition sub3 values('03'),subpartition sub4 values('04'),
subpartition sub5 values('05'),subpartition sub6 values('06'),
subpartition sub7 values('07'),subpartition sub8 values('08'),
subpartition sub9 values('09'),subpartition sub10 values('10'),
subpartition sub11 values('11'),subpartition sub12 values('12'),
subpartition sub13 values('13'),subpartition sub14 values('14'),
subpartition sub15 values('15'),subpartition sub16 values('16'),
subpartition sub17 values('17'),subpartition sub18 values('18'),
subpartition sub19 values('19'),subpartition sub20 values('20'),
subpartition sub21 values('21'),subpartition sub22 values('22'),
subpartition sub23 values('23'),subpartition sub24 values('24'),
subpartition sub25 values('25'),subpartition sub26 values('26'),
subpartition sub27 values('27'),subpartition sub28 values('28'),
subpartition sub29 values('29'),subpartition sub30 values('30'),
subpartition sub31 values('31')
)
(
partition p_0701_010 values less than('200701','011'),
partition p_0701_011 values less than('200701','012'),
partition p_0701_012 values less than('200701','013'),
partition p_0701_013 values less than('200701','014'),
partition p_0701_014 values less than('200701','015'),
partition p_0701_015 values less than('200701','016'),
partition p_0702_010 values less than('200702','011'),
partition p_0702_011 values less than('200702','012'),
partition p_0702_012 values less than('200702','013'),
partition p_0702_013 values less than('200702','014'),
partition p_0702_014 values less than('200702','015'),
partition p_0702_015 values less than('200702','016'),
partition p_0703_010 values less than('200703','011'),
partition p_0703_011 values less than('200703','012'),
partition p_0703_012 values less than('200703','013'),
partition p_0703_013 values less than('200703','014'),
partition p_0703_014 values less than('200703','015'),
partition p_0703_015 values less than('200703','016'),
partition p_0704_010 values less than('200704','011'),
partition p_0704_011 values less than('200704','012'),
partition p_0704_012 values less than('200704','013'),
partition p_0704_013 values less than('200704','014'),
partition p_0704_014 values less than('200704','015'),
partition p_0704_015 values less than('200704','016'),
partition p_0705_010 values less than('200705','011'),
partition p_0705_011 values less than('200705','012'),
partition p_0705_012 values less than('200705','013'),
partition p_0705_013 values less than('200705','014'),
partition p_0705_014 values less than('200705','015'),
partition p_0705_015 values less than('200705','016'),
partition p_0706_010 values less than('200706','011'),
partition p_0706_011 values less than('200706','012'),
partition p_0706_012 values less than('200706','013'),
partition p_0706_013 values less than('200706','014'),
partition p_0706_014 values less than('200706','015'),
partition p_0706_015 values less than('200706','016'),
partition p_0707_010 values less than('200707','011'),
partition p_0707_011 values less than('200707','012'),
partition p_0707_012 values less than('200707','013'),
partition p_0707_013 values less than('200707','014'),
partition p_0707_014 values less than('200707','015'),
partition p_0707_015 values less than('200707','016'),
partition p_0708_010 values less than('200708','011'),
partition p_0708_011 values less than('200708','012'),
partition p_0708_012 values less than('200708','013'),
partition p_0708_013 values less than('200708','014'),
partition p_0708_014 values less than('200708','015'),
partition p_0708_015 values less than('200708','016'),
partition p_0709_010 values less than('200709','011'),
partition p_0709_011 values less than('200709','012'),
partition p_0709_012 values less than('200709','013'),
partition p_0709_013 values less than('200709','014'),
partition p_0709_014 values less than('200709','015'),
partition p_0709_015 values less than('200709','016'),
partition p_0710_010 values less than('200710','011'),
partition p_0710_011 values less than('200710','012'),
partition p_0710_012 values less than('200710','013'),
partition p_0710_013 values less than('200710','014'),
partition p_0710_014 values less than('200710','015'),
partition p_0710_015 values less than('200710','016'),
partition p_0711_010 values less than('200711','011'),
partition p_0711_011 values less than('200711','012'),
partition p_0711_012 values less than('200711','013'),
partition p_0711_013 values less than('200711','014'),
partition p_0711_014 values less than('200711','015'),
partition p_0711_015 values less than('200711','016'),
partition p_0712_010 values less than('200712','011'),
partition p_0712_011 values less than('200712','012'),
partition p_0712_012 values less than('200712','013'),
partition p_0712_013 values less than('200712','014'),
partition p_0712_014 values less than('200712','015'),
partition p_0712_015 values less than('200712','016'),
partition p_0801_010 values less than('200801','011'),
partition p_0801_011 values less than('200801','012'),
partition p_0801_012 values less than('200801','013'),
partition p_0801_013 values less than('200801','014'),
partition p_0801_014 values less than('200801','015'),
partition p_0801_015 values less than('200801','016'),
partition p_other values less than(maxvalue, maxvalue)
);
--四、分区表操作
--1、插入记录:
insert into AAA values(1 ,sysdate);
--2、查询分区表记录:
select * from AAA partition(part_01);
--3、更新分区表的记录:
update AAA partition(part_01) t set indate=’’where id=1; --但是当更新的时候指定了分区,而根据查询的记录不在该分区中时,将不会更新数据
--4、删除分区表记录:
delete from AAA partition(part_02) t where id=4; -- 如果指定了分区,而条件中的数据又不在该分区中时,将不会删除任何数据。
--5、增加一个分区:
alter table AAA add partition part_04 values less than(to_date(’2012-01-01’,’yyyy-mm-dd’)) tablespace dinya_spa ce03; --增加一个分区的时候,增加的分区的条件必须大于现有分区的最大值,否则系统将提示ORA-14074 partition bound must collate higher than that of the last partition 错误。
--6、合并一个分区:
alter table AAA merge partitions part_01,part_02 into partition part_02; ,--如果在合并的时候把合并后的分区定为part_01的时候,系统将提示ORA-14275 cannot reuse lower-bound partition as resulting partition 错误。
--7、删除分区:
alter table AAA drop partition part_01; --删除分区表的一个分区后,查询该表的数据时显示,该分区中的数据已全部丢失,所以执行删除分区动作时要慎重,确保先备份数据后再执行,或将分区合并。