# oracle表 分区

Oracle表分区分为四种：范围分区，散列分区，列表分区和复合分区。

Sql代码
2. (
3. sno varchar2(10),
4. sname varchar2(20),
5. dormitory varchar2(3),
7. )
9. (
10. partition bujige values less than(60), --不及格
11. partition jige values less than(85), --及格
12. partition youxiu values less than(maxvalue) --优秀
13. )
create table graderecord
(
sno varchar2(10),
sname varchar2(20),
dormitory varchar2(3),
)
(
partition bujige values less than(60), --不及格
partition jige values less than(85), --及格
partition youxiu values less than(maxvalue) --优秀
)

Sql代码
insert into graderecord values('511601','魁','229',92);


Java代码
2. select * from graderecord partition(bujige);
3. select * from graderecord partition(jige);
4. select * from graderecord partition(youxiu);
select * from graderecord;
select * from graderecord partition(youxiu);

Sql代码
2. (
3. sno varchar2(10),
4. sname varchar2(20),
5. dormitory varchar2(3),
7. )
8. partition by hash(sno)
9. (
10. partition p1,
11. partition p2,
12. partition p3
13. );
create table graderecord
(
sno varchar2(10),
sname varchar2(20),
dormitory varchar2(3),
)
partition by hash(sno)
(
partition p1,
partition p2,
partition p3
);

Sql代码
1. select * from graderecord partition(p1);
2. select * from graderecord partition(p2);
3. select * from graderecord partition(p3);
select * from graderecord partition(p1);
select * from graderecord partition(p3);

p1分区的数据：

p2分区的数据：

p3分区的数据：

Sql代码
2. (
3. sno varchar2(10),
4. sname varchar2(20),
5. dormitory varchar2(3),
7. )
8. partition by list(dormitory)
9. (
10. partition d229 values('229'),
11. partition d228 values('228'),
12. partition d240 values('240')
13. )
create table graderecord
(
sno varchar2(10),
sname varchar2(20),
dormitory varchar2(3),
)
partition by list(dormitory)
(
partition d229 values('229'),
partition d228 values('228'),
partition d240 values('240')
)

Sql代码
1. select * from graderecord partition(d229);
2. select * from graderecord partition(d228);
3. select * from graderecord partition(d240);
select * from graderecord partition(d229);
select * from graderecord partition(d240);

d229分区所得数据如下：

d228分区所得数据如下：

d240分区所得数据如下：

Sql代码
2. (
3. sno varchar2(10),
4. sname varchar2(20),
5. dormitory varchar2(3),
7. )
9. subpartition by hash(sno,sname)
10. (
11. partition p1 values less than(75)
12. (
13. subpartition sp1,subpartition sp2
14. ),
15. partition p2 values less than(maxvalue)
16. (
17. subpartition sp3,subpartition sp4
18. )
19. );
create table graderecord
(
sno varchar2(10),
sname varchar2(20),
dormitory varchar2(3),
)
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
)
);

Sql代码
insert into graderecord values('511601','魁','229',92);
insert into graderecord values('511617','靳','244',95);

Sql代码
1. select * from graderecord partition(p1);
2. select * from graderecord partition(p2);
3. select * from graderecord subpartition(sp1);
4. select * from graderecord subpartition(sp2);
5. select * from graderecord subpartition(sp3);
6. select * from graderecord subpartition(sp4);
select * from graderecord partition(p1);
select * from graderecord subpartition(sp4);

Sql代码
1. create table MobileMessage
2. (
3. ACCT_MONTH VARCHAR2(6), -- 帐期 格式：年月 YYYYMM
4. AREA_NO VARCHAR2(10), -- 地域号码
5. DAY_ID VARCHAR2(2), -- 本月中的第几天 格式 DD
6. SUBSCRBID VARCHAR2(20), -- 用户标识
7. SVCNUM VARCHAR2(30) -- 手机号码
8. )
9. partition by range(ACCT_MONTH,AREA_NO) subpartition by list(DAY_ID)
10. (
11. partition p1 values less than('200705','012')
12. (
13. subpartition shangxun1 values('01','02','03','04','05','06','07','08','09','10'),
14. subpartition zhongxun1 values('11','12','13','14','15','16','17','18','19','20'),
15. subpartition xiaxun1 values('21','22','23','24','25','26','27','28','29','30','31')
16. ),
17. partition p2 values less than('200709','014')
18. (
19. subpartition shangxun2 values('01','02','03','04','05','06','07','08','09','10'),
20. subpartition zhongxun2 values('11','12','13','14','15','16','17','18','19','20'),
21. subpartition xiaxun2 values('21','22','23','24','25','26','27','28','29','30','31')
22. ),
23. partition p3 values less than('200801','016')
24. (
25. subpartition shangxun3 values('01','02','03','04','05','06','07','08','09','10'),
26. subpartition zhongxun3 values('11','12','13','14','15','16','17','18','19','20'),
27. subpartition xiaxun3 values('21','22','23','24','25','26','27','28','29','30','31')
28. )
29. )
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')
)
)

Sql代码
1. insert into MobileMessage values('200701','010','04','ghk001','13800000000');
2. insert into MobileMessage values('200702','015','12','myx001','13633330000');
3. insert into MobileMessage values('200703','015','24','hjd001','13300000000');
4. insert into MobileMessage values('200704','010','04','ghk001','13800000000');
5. insert into MobileMessage values('200705','010','04','ghk001','13800000000');
6. insert into MobileMessage values('200705','011','18','sxl001','13222000000');
7. insert into MobileMessage values('200706','011','21','sxl001','13222000000');
8. insert into MobileMessage values('200706','012','11','tgg001','13800044400');
9. insert into MobileMessage values('200707','010','04','ghk001','13800000000');
10. insert into MobileMessage values('200708','012','24','tgg001','13800044400');
11. insert into MobileMessage values('200709','014','29','zjj001','13100000000');
12. insert into MobileMessage values('200710','014','29','zjj001','13100000000');
13. insert into MobileMessage values('200711','014','29','zjj001','13100000000');
14. insert into MobileMessage values('200711','013','30','wgc001','13444000000');
15. insert into MobileMessage values('200712','013','30','wgc001','13444000000');
16. insert into MobileMessage values('200712','010','30','ghk001','13800000000');
17. insert into MobileMessage values('200801','015','22','myx001','13633330000');
insert into MobileMessage values('200701','010','04','ghk001','13800000000');
insert into MobileMessage values('200702','015','12','myx001','13633330000');
insert into MobileMessage values('200703','015','24','hjd001','13300000000');
insert into MobileMessage values('200704','010','04','ghk001','13800000000');
insert into MobileMessage values('200705','010','04','ghk001','13800000000');
insert into MobileMessage values('200705','011','18','sxl001','13222000000');
insert into MobileMessage values('200706','011','21','sxl001','13222000000');
insert into MobileMessage values('200706','012','11','tgg001','13800044400');
insert into MobileMessage values('200707','010','04','ghk001','13800000000');
insert into MobileMessage values('200708','012','24','tgg001','13800044400');
insert into MobileMessage values('200709','014','29','zjj001','13100000000');
insert into MobileMessage values('200710','014','29','zjj001','13100000000');
insert into MobileMessage values('200711','014','29','zjj001','13100000000');
insert into MobileMessage values('200711','013','30','wgc001','13444000000');
insert into MobileMessage values('200712','013','30','wgc001','13444000000');
insert into MobileMessage values('200712','010','30','ghk001','13800000000');
insert into MobileMessage values('200801','015','22','myx001','13633330000');

Sql代码
1. select * from MobileMessage;
select * from MobileMessage;

Sql代码
1. create table MobileMessage
2. (
3. ACCT_MONTH VARCHAR2(6), -- 帐期 格式：年月 YYYYMM
4. AREA_NO VARCHAR2(10), -- 地域号码
5. DAY_ID VARCHAR2(2), -- 本月中的第几天 格式 DD
6. SUBSCRBID VARCHAR2(20), -- 用户标识
7. SVCNUM VARCHAR2(30) -- 手机号码
8. )
9. partition by range(ACCT_MONTH,AREA_NO) subpartition by list(DAY_ID)
10. subpartition template
11. (
12. subpartition sub1 values('01'),subpartition sub2 values('02'),
13. subpartition sub3 values('03'),subpartition sub4 values('04'),
14. subpartition sub5 values('05'),subpartition sub6 values('06'),
15. subpartition sub7 values('07'),subpartition sub8 values('08'),
16. subpartition sub9 values('09'),subpartition sub10 values('10'),
17. subpartition sub11 values('11'),subpartition sub12 values('12'),
18. subpartition sub13 values('13'),subpartition sub14 values('14'),
19. subpartition sub15 values('15'),subpartition sub16 values('16'),
20. subpartition sub17 values('17'),subpartition sub18 values('18'),
21. subpartition sub19 values('19'),subpartition sub20 values('20'),
22. subpartition sub21 values('21'),subpartition sub22 values('22'),
23. subpartition sub23 values('23'),subpartition sub24 values('24'),
24. subpartition sub25 values('25'),subpartition sub26 values('26'),
25. subpartition sub27 values('27'),subpartition sub28 values('28'),
26. subpartition sub29 values('29'),subpartition sub30 values('30'),
27. subpartition sub31 values('31')
28. )
29. (
30. partition p_0701_010 values less than('200701','011'),
31. partition p_0701_011 values less than('200701','012'),
32. partition p_0701_012 values less than('200701','013'),
33. partition p_0701_013 values less than('200701','014'),
34. partition p_0701_014 values less than('200701','015'),
35. partition p_0701_015 values less than('200701','016'),
36. partition p_0702_010 values less than('200702','011'),
37. partition p_0702_011 values less than('200702','012'),
38. partition p_0702_012 values less than('200702','013'),
39. partition p_0702_013 values less than('200702','014'),
40. partition p_0702_014 values less than('200702','015'),
41. partition p_0702_015 values less than('200702','016'),
42. partition p_0703_010 values less than('200703','011'),
43. partition p_0703_011 values less than('200703','012'),
44. partition p_0703_012 values less than('200703','013'),
45. partition p_0703_013 values less than('200703','014'),
46. partition p_0703_014 values less than('200703','015'),
47. partition p_0703_015 values less than('200703','016'),
48. partition p_0704_010 values less than('200704','011'),
49. partition p_0704_011 values less than('200704','012'),
50. partition p_0704_012 values less than('200704','013'),
51. partition p_0704_013 values less than('200704','014'),
52. partition p_0704_014 values less than('200704','015'),
53. partition p_0704_015 values less than('200704','016'),
54. partition p_0705_010 values less than('200705','011'),
55. partition p_0705_011 values less than('200705','012'),
56. partition p_0705_012 values less than('200705','013'),
57. partition p_0705_013 values less than('200705','014'),
58. partition p_0705_014 values less than('200705','015'),
59. partition p_0705_015 values less than('200705','016'),
60. partition p_0706_010 values less than('200706','011'),
61. partition p_0706_011 values less than('200706','012'),
62. partition p_0706_012 values less than('200706','013'),
63. partition p_0706_013 values less than('200706','014'),
64. partition p_0706_014 values less than('200706','015'),
65. partition p_0706_015 values less than('200706','016'),
66. partition p_0707_010 values less than('200707','011'),
67. partition p_0707_011 values less than('200707','012'),
68. partition p_0707_012 values less than('200707','013'),
69. partition p_0707_013 values less than('200707','014'),
70. partition p_0707_014 values less than('200707','015'),
71. partition p_0707_015 values less than('200707','016'),
72. partition p_0708_010 values less than('200708','011'),
73. partition p_0708_011 values less than('200708','012'),
74. partition p_0708_012 values less than('200708','013'),
75. partition p_0708_013 values less than('200708','014'),
76. partition p_0708_014 values less than('200708','015'),
77. partition p_0708_015 values less than('200708','016'),
78. partition p_0709_010 values less than('200709','011'),
79. partition p_0709_011 values less than('200709','012'),
80. partition p_0709_012 values less than('200709','013'),
81. partition p_0709_013 values less than('200709','014'),
82. partition p_0709_014 values less than('200709','015'),
83. partition p_0709_015 values less than('200709','016'),
84. partition p_0710_010 values less than('200710','011'),
85. partition p_0710_011 values less than('200710','012'),
86. partition p_0710_012 values less than('200710','013'),
87. partition p_0710_013 values less than('200710','014'),
88. partition p_0710_014 values less than('200710','015'),
89. partition p_0710_015 values less than('200710','016'),
90. partition p_0711_010 values less than('200711','011'),
91. partition p_0711_011 values less than('200711','012'),
92. partition p_0711_012 values less than('200711','013'),
93. partition p_0711_013 values less than('200711','014'),
94. partition p_0711_014 values less than('200711','015'),
95. partition p_0711_015 values less than('200711','016'),
96. partition p_0712_010 values less than('200712','011'),
97. partition p_0712_011 values less than('200712','012'),
98. partition p_0712_012 values less than('200712','013'),
99. partition p_0712_013 values less than('200712','014'),
100. partition p_0712_014 values less than('200712','015'),
101. partition p_0712_015 values less than('200712','016'),
102. partition p_0801_010 values less than('200801','011'),
103. partition p_0801_011 values less than('200801','012'),
104. partition p_0801_012 values less than('200801','013'),
105. partition p_0801_013 values less than('200801','014'),
106. partition p_0801_014 values less than('200801','015'),
107. partition p_0801_015 values less than('200801','016'),
108. partition p_other values less than(maxvalue, maxvalue)
109. );
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)
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)
);

Sql代码
1. select * from MobileMessage partition(p_0701_010);
select * from MobileMessage partition(p_0701_010);

Sql代码
1. select * from MobileMessage subpartition(p_0701_010_sub4);
select * from MobileMessage subpartition(p_0701_010_sub4);

Sql代码
1. select * from MobileMessage partition(p_0706_011);
select * from MobileMessage partition(p_0706_011);

Sql代码
1. select * from MobileMessage subpartition(p_0706_011_sub21);
select * from MobileMessage subpartition(p_0706_011_sub21);

(1)分裂分区，以第一个范围分区为例：

Sql代码
1. alter table graderecord split partition jige at(75)
2. into(partition keyi,partition lianghao);
alter table graderecord split partition jige at(75)
into(partition keyi,partition lianghao); 

(2)合并分区，以第一个范围分区为例：

Sql代码
1. alter table graderecord merge partitions keyi,lianghao
2. into partition jige;
alter table graderecord merge partitions keyi,lianghao
into partition jige;

(3)添加分区，由于在范围分区上添加分区要求添加的分区范围大于原有分区最大值，但原有分区最大值已经为maxvalue，故本处以第二个散列分区为例：

Sql代码
alter table graderecord add partition p4;

(4)删除分区，语法：

Sql代码
1. alter table table_name drop partition partition_name;
alter table table_name drop partition partition_name;

(5)截断分区，清空分区中的数据

Sql代码
1. alter table table_name truncate partition partition_name;
alter table table_name truncate partition partition_name;

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

泥盖木屋

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
07-01 1447

04-02 8876
05-05 2万+
08-22
07-06 7591
10-16 1582
04-15 3948
08-19 799