例:创建范围分区
create table range_orders
(order_id varchar2(10) constraint OR_PK primary key
,order_date date default sysdate
,qty integer
,payterms varchar2(10)
,book_id number(6)
)
partition by range (order_date)
(partition p1 values less than (to_date('20140331','yyyymmdd')) tablespace user01,
partition p2 values less than (to_date('20140430','yyyymmdd')) tablespace user02,
partition p3 values less than (to_date('20140531','yyyymmdd')) tablespace user03
)
;
SQL> insert into range_orders values ('10001',to_date('20140321','yyyymmdd'),1,'payterm_1',110345);
1 row inserted
SQL> insert into range_orders values ('10002',to_date('20140421','yyyymmdd'),1,'payterm_2',110745);
1 row inserted
SQL> insert into range_orders values ('10003',to_date('20140521','yyyymmdd'),1,'payterm_3',110945);
1 row inserted
SQL> commit;
Commit complete
SQL> select rowid,r.* from range_orders r;
ROWID ORDER_ID ORDER_DATE QTY PAYTERMS BOOK_ID
------------------ ---------- ----------- ---- ---------- -------
AAADwpAAGAAAACFAAA 10001 2014/3/21 1 payterm_1 110345
AAADwqAAHAAAACFAAA 10002 2014/4/21 1 payterm_2 110745
AAADwrAAIAAAACFAAA 10003 2014/5/21 1 payterm_3 110945
可以看到AAG,AAH,AAI 分别代表了三条数据的文件号是6,7,8
SQL> select x.FILE#,x.NAME from v$datafile x;
FILE# NAME
---------- ----------------------------------------
1 D:\ORACLE\ORADATA\CRISS_DB\SYSTEM01.DBF
2 D:\ORACLE\ORADATA\CRISS_DB\SYSAUX01.DBF
3 D:\ORACLE\ORADATA\CRISS_DB\UNDOTBS01.DBF
4 D:\ORACLE\ORADATA\CRISS_DB\USERS01.DBF
5 D:\ORACLE\ORADATA\CRISS_DB\TEST01.DBF
6 D:\ORACLE\ORADATA\CRISS_DB\USER01.DBF
7 D:\ORACLE\ORADATA\CRISS_DB\USER02.DBF
8 D:\ORACLE\ORADATA\CRISS_DB\USER03.DBF
8 rows selected
<散列分区 partition by hash>
SQL> create table hash_orders
(order_id varchar2(10) constraint HOR_PK primary key
,order_date date default sysdate
,qty integer
,payterms varchar2(10)
,book_id number(6)
)
partition by hash(order_id)
( partition hash_p1 tablespace user01
,partition hash_p2 tablespace user02
);
Table created
SQL> insert into hash_orders select * from range_orders;
3 rows inserted
SQL> select rowid,h.* from hash_orders h;
ROWID ORDER_ID ORDER_DATE QTY PAYTERMS BOOK_ID
------------------ ---------- ----------------- ---------- -------
AAADyIAAGAAAACNAAA 10002 2014/4/21 1 payterm_2 110745
AAADyIAAGAAAACNAAB 10003 2014/5/21 1 payterm_3 110945
AAADyJAAHAAAACNAAA 10001 2014/3/21 1 payterm_1 110345
order_id为 10002,10003的被存在AAG 6号文件上,第141个块上的 第1行和第2行
SQL> create table list_orders
(order_id varchar2(10) constraint LOR_PK primary key
,order_date date default sysdate
,qty integer
,payterms varchar2(10)
,book_id number(6)
)
partition by list(payterms)
( partition list_p1 values('payterm_1') tablespace user01
,partition list_p2 values('payterm_2')tablespace user02
,partition list_p3 values('payterm_3') tablespace user03
);
Table created
SQL> select rowid,l.* from list_orders l;
ROWID ORDER_ID ORDER_DATE QTY PAYTERMS BOOK_ID
------------------ ---------- ------------------ ---------- -------
AAADyRAAGAAAACdAAA 10001 2014/3/21 1 payterm_1 110345
AAADySAAHAAAACdAAA 10002 2014/4/21 1 payterm_2 110745
AAADyTAAIAAAACVAAA 10003 2014/5/21 1 payterm_3 110945
<复合分区>
SQL> create table comp_orders
(order_id varchar2(10) constraint COR_PK primary key
,order_date date default sysdate
,qty integer
,payterms varchar2(10)
,book_id number(6)
)
partition by range (order_date)
subpartition by list(payterms)
(partition p1 values less than (to_date('20140331','yyyymmdd'))
( subpartition p1_sub1 values('payterm_1') tablespace user01
,subpartition p1_sub2 values('payterm_2') tablespace user02
,subpartition p1_sub3 values('payterm_3') tablespace user03
)
,partition p2 values less than (to_date('20140430','yyyymmdd'))
( subpartition p2_sub1 values('payterm_1') tablespace user04
,subpartition p2_sub2 values('payterm_2') tablespace user05
,subpartition p2_sub3 values('payterm_3') tablespace user06
)
,partition p3 values less than (maxvalue) tablespace user07
)
;
Table created
SQL> select rowid,c.* from comp_orders c;
ROWID ORDER_ID ORDER_DATE QTY PAYTERMS BOOK_ID
------------------ ---------- ----------- ----- ---------- -------
AAADyZAAGAAAAClAAA 10004 2014/3/21 1 payterm_1 110345
AAADyZAAGAAAAClAAB 10001 2014/3/21 1 payterm_1 110345
AAADydAAKAAAACFAAA 10005 2014/4/21 1 payterm_2 110745
AAADydAAKAAAACFAAB 10002 2014/4/21 1 payterm_2 110745
AAADyfAAMAAAACFAAA 10006 2014/5/21 1 payterm_3 110945
AAADyfAAMAAAACFAAB 10003 2014/5/21 1 payterm_3 110945
6 rows selected
PAYTERMS字段值相同,且ORDER_DATE时间区间相同的数据被放到同一个表空间下面.
SQL> insert into comp_orders values('10007',to_date('20140421','yyyymmdd'),1,'payterm_1','113888');
1 row inserted
SQL> select rowid,c.* from comp_orders c;
ROWID ORDER_ID ORDER_DATE QTY PAYTERMS BOOK_ID
------------------ ---------- ---------------- ---------- -------
AAADyZAAGAAAAClAAA 10004 2014/3/21 1 payterm_1 110345
AAADyZAAGAAAAClAAB 10001 2014/3/21 1 payterm_1 110345
AAADycAAJAAAACFAAA 10007 2014/4/21 1 payterm_1 113888
AAADydAAKAAAACFAAA 10005 2014/4/21 1 payterm_2 110745
AAADydAAKAAAACFAAB 10002 2014/4/21 1 payterm_2 110745
AAADyfAAMAAAACFAAA 10006 2014/5/21 1 payterm_3 110945
AAADyfAAMAAAACFAAB 10003 2014/5/21 1 payterm_3 110945
7 rows selected
ORDER_ID为10007的数据存放在了另一个表空间内(AAJ AAADyc[AAJ]AAAACFAAA)
<间隔分区>
间隔分区是oracle 11g release 1 以后版本中新增的特性,它是对范围分区的扩展,
可以自动进行等距离范围的分区.
实验对比一下,间隔分区与范围分区有什么区别即可.
首先创建一个间隔分区,间隔分区以一个范围分区为'起点',并定义一个间隔,
当有数据插入,依据该间隔为附加的数据创建新的分区.
SQL> create table intvl_orders
(order_id varchar2(10) constraint INO_PK primary key
,order_date date default sysdate
,qty integer
,payterms varchar2(10)
,book_id number(6)
)
partition by range (order_date)
interval(numtoyminterval(1,'MONTH'))
store in (user01,user02,user03)
(
partition p1 values less than (to_date('20140101','yyyymmdd'))
)
;
Table created
注意:partition p1 values less than (to_date('20140101','yyyymmdd')) 起始是月初,
SQL> drop table range_orders purge;
Table dropped
SQL>
SQL> create table range_orders
(order_id varchar2(10) constraint OR_PK primary key
,order_date date default sysdate
,qty integer
,payterms varchar2(10)
,book_id number(6)
)
partition by range (order_date)
(partition p1 values less than (to_date('20140331','yyyymmdd')) tablespace user01,
partition p2 values less than (to_date('20140430','yyyymmdd')) tablespace user02,
partition p3 values less than (to_date('20140531','yyyymmdd')) tablespace user03
)
;
Table created
SQL> insert into range_orders values ('10003',to_date('20140621','yyyymmdd'),1,'payterm_3',110945);
insert into range_orders values ('10003',to_date('20140621','yyyymmdd'),1,'payterm_3',110945)
ORA-14400: 插入的分区关键字未映射到任何分区
SQL> insert into range_orders values ('10003',to_date('20140521','yyyymmdd'),1,'payterm_3',110945);
1 row inserted
可以看到,新创建的范围分区表最大范围是20140531,当出入的数据超出分区范围就会报错,
SQL> insert into intvl_orders values ('10003',to_date('20140621','yyyymmdd'),1,'payterm_3',110945);
1 row inserted
我们插入同样超出分区范围的数据,数据插入成功了!
-- Create table
create table INTVL_ORDERS
(
ORDER_ID VARCHAR2(10) not null,
ORDER_DATE DATE default sysdate,
QTY INTEGER,
PAYTERMS VARCHAR2(10),
BOOK_ID NUMBER(6)
)
partition by range (ORDER_DATE)
(
partition P1 values less than (TO_DATE(' 2014-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
)
);
-- Create/Recreate primary, unique and foreign key constraints
alter table INTVL_ORDERS
add constraint INO_PK primary key (ORDER_ID)
using index
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
插入数据后间隔分区表时候oracle内的建表语句:
-- Create table
create table INTVL_ORDERS
(
ORDER_ID VARCHAR2(10) not null,
ORDER_DATE DATE default sysdate,
QTY INTEGER,
PAYTERMS VARCHAR2(10),
BOOK_ID NUMBER(6)
)
partition by range (ORDER_DATE)
(
partition P1 values less than (TO_DATE(' 2014-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
),
partition SYS_P22 values less than (TO_DATE(' 2014-07-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
tablespace USER01
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
)
);
-- Create/Recreate primary, unique and foreign key constraints
alter table INTVL_ORDERS
add constraint INO_PK primary key (ORDER_ID)
using index
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
结果自动扩展了一个分区!这也就是间隔分区的特点.
<引用分区>
引用分区通过父表继承分区键,可以在逻辑上均分具有分子关系的表.
分区键通过现有的父子关系解析,由现行的主键或外键约束实施.
SQL> create table range_books
(bookid number(6) primary key
,bookname varchar2(40) not null
,booktime date default sysdate
)
partition by range(booktime)
(
partition p1 values less than (to_date('20140331','yyyymmdd'))
,partition p2 values less than (to_date('20140430','yyyymmdd'))
);
Table created
SQL> create table ref_order
(order_id varchar2(20) constraint RFO_PK primary key
,order_date date default sysdate
,book_id number(6) not null--not null !!!
,constraint RFO_FK foreign key (book_id) references range_books(bookid)
)
partition by reference (RFO_FK)
;
Table created
上面的实验,通过使用外键发现分区机制,外键[RFO_FK]指向父表'range_books',
删除分区:
alter table range_orders drop partition p2;
增加分区:
alter table range_orders add partition p2 values
less than (to_date('20140430','yyyymmdd')) tablespace user02;
合并分区:
alter table range_orders merge partition p1,p2 into partition p2;
移动分区:
alter table range_orders move partition p2 tablespace user05;
重命名:
alter table range_orders rename partition p2 to p1;
截断分区:
alter table range_orders truncate partition p1;