您的位置:首页 > 其它

Orcl表分区和表空间的简单介绍,及其简单的创建方法

2016-09-02 20:01 369 查看
一、表分区

1、范围分区

实例:
1、创建表分区
create table sales(
pid number(10) primary key,
sale number(8,2)
)partition by range(sale)(
partition p1 values less than(5000),
partition p21 values less than(10000),
partition p3 values less than(maxvalue)
);
2、添加数据到表分区
create sequence seq_sales_pid start with 1;(随机产生ID)
insert into sales values(seq_sales_pid.nextval,4000);
insert into sales values(seq_sales_pid.nextval,8000);
insert into sales values(seq_sales_pid.nextval,14000);
insert into sales values(seq_sales_pid.nextval,24000);
insert into sales values(seq_sales_pid.nextval,6000);


2、指定表分区查询

select * from sales partition(p3);
select * from sales partition(p3) where sale>10000 and sale<20000;


二、表空间

1、创建表空间

create tablespace yc_space01 datafile 'D:\yc01.dbf' size 5M;
create tablespace yc_space02 datafile 'D:\yc02.dbf' size 5M;
create tablespace yc_space03 datafile 'D:\yc03.dbf' size 5M;
create tablespace yc_space04 datafile 'D:\yc04.dbf' size 5M;
create tablespace yc_space05 datafile 'D:\yc05.dbf' size 5M;


2、列表分区(适合列的基数比较低的情况 就是列的取值比较少)

实例
1、创建表分区
create table employee(
eid number(10) primary key,
ename varchar2(100),
addr varchar2(100)
)partition by list(addr)(
partition north values('吉林','辽宁') tablespace yc_space01,
partition west values('吉林','辽宁') tablespace yc_space02,
partition south values('吉林','辽宁') tablespace yc_space03,
partition east values('吉林','辽宁') tablespace yc_space04,
partition center values(default) tablespace yc_space05
);
2、添加数据到表分区
insert into employee values(1,'战三','辽宁');
insert into employee values(2,'战三','辽宁');
insert into employee values(3,'战三','辽宁');
insert into employee values(4,'战三','辽宁');
insert into employee values(5,'战三','辽宁');
insert into employee values(6,'战三','辽宁');
insert into employee values(7,'战三','辽宁');
insert into employee values(8,'战三','辽宁');
3、查询指定列表分区
select * from empioyee partition(south);
select * from empioyee partition(center);


三、删除表空间

drop tablespace <tablesapce_name> including conten
4000
ts and datafile;


四、复合分区

实例1:
1、创建表分区
create table sale(
sid number(10) primary  key,
s_date date not null,
cost_num number(10)
)partition by range(s_date)
subpartition by hash (cost_num)
subpartition 5(
partition s1 values less than(to_date('2016-02','yyyy-mm'),
partition s2 values less than(to_date('2016-03','yyyy-mm'),
partition s3 values less than(to_date('2016-04','yyyy-mm'),
partition s4 values less than(to_date('2016-05','yyyy-mm'),
partition s5 values less than(maxvalue)
)
2、为表空间添加数据
insert into sale values(1,to_date('2016-02','yyyy-mm'),4000);
insert into sale values(2,to_date('2016-02','yyyy-mm'),4000);
insert into sale values(3,to_date('2016-02','yyyy-mm'),4000);
insert into sale values(4,to_date('2016-02','yyyy-mm'),4000);
insert into sale values(5,to_date('2016-02','yyyy-mm'),4000);
insert into sale values(6,to_date('2016-02','yyyy-mm'),4000);
insert into sale values(7,to_date('2016-02','yyyy-mm'),4000);
3、查询对应复合分区
select * from sale partition(s2);


实例2:
1、创建复合分区
create table sales(
pid number(10) primary key,
sale number(8,2)
)partition by range(sale)(
partition p1 values less than(5000),
partition p2 values less than(10000),
partition p3 values less than(maxvalue)
);
2、为复合分区添加数据
create sequence seq_sales_pid start with 1(产生PID);
insert into sales values(seq_sales_pid.nextval,4000);
insert into sales values(seq_sales_pid.nextval,8000);
insert into sales values(seq_sales_pid.nextval,14000);
insert into sales values(seq_sales_pid.nextval,24000);
insert into sales values(seq_sales_pid.nextval,6000);
alert table sales add partition p4 values less than(15000);


五、在原有的分区里添加新分区的步骤

1、先备份原有分区中最后一个分区即上面实例二中p3的数据

create table temp as select * from sales partition(p3);


2、删除最高的那个分区

alter table sales drop partition(p3);


3、查看现有分区

select * from user_tab_partitions where table_name="SALES";


4、创建新分区

alter table sales add partition p3 values less than(15000);
alter table sales add partition p4 values less than(maxvalue);


5、查看索引

select * from user_indexes;


6、重建索引

alter index SYS_COO11609 rebuild;


7、将原有的数据还原

insert into sales select * from temp;


8、删除备份表

drop table temp;


六、截断分区、删除指定分区中的数据

alter table sales truncate partition p4;


七、合并与拆分分区

1、合并分区

alter table sales merge partition p2,p3 into partition p3;


2、拆分分区

alter table sales split partition p3 at(8000) into (partition p2,partition p3);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  实例