一:范围分区
就是根据数据库表中某一字段的值的范围来划分分区,例如:
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) --优秀
) ;
插入实验数据:
insert into graderecord values('511601','魁','229',92);
insert into graderecord values('511602','凯','229',62);
insert into graderecord values('511603','东','229',26);
insert into graderecord values('511604','亮','228',77);
insert into graderecord values('511605','敬','228',47);
insert into graderecord(sno,sname,dormitory) values('511606','峰','228');
下面查询一下全部数据,然后查询各个分区数据,代码一起写:
select * from graderecord;
select * from graderecord partition(bujige);
select * from graderecord partition(jige);
select * from graderecord partition(youxiu);
说明:数据中有空值,Oracle机制会自动将其规划到maxvalue的分区中。
二,间隔分区
–创建间隔分区表
CREATE TABLE SALES2
(
SALES_ID NUMBER,
PRODUCT_ID VARCHAR2(5),
SALES_DATE DATE NOT NULL
)
PARTITION BY RANGE(SALES_DATE)
INTERVAL(NUMTOYMINTERVAL(3,'MONTH'))
(PARTITION P1 VALUES LESS THAN (to_date('2013-04-1','yyyy/mm/dd')));
–插入数据
INSERT INTO sales2 VALUES (1,'a',to_date('2013-08-1','yyyy/mm/dd'));
–获得分区情况
SELECT table_name,partition_name
FROM user_tab_partitions
WHERE table_name=UPPER('sales2');
–查询输出结果,系统自动根据输入数据情况创建新分区“SYS_P82”
TABLE_NAME PARTITION_NAME
----------------------------
SALES2 P1
SALES2 SYS_P21
–查询分区数据
SELECT * FROM sales2 PARTITION(sys_P21);