引用分区

10 篇文章 0 订阅
8 篇文章 0 订阅

引用分区:处理父子表对等分区的问题,以这种方式处理分区,父表与子表的分区是一对一关系。在维护主子表。如果不是按照引用分区,即使父子表分区一致,也会因为引用的约束,导致主表数据无法维护:例如下面的表,如果子表中将父表的startDate时间复制过去,这样会导致在删除主表2013-12-30之前的数据时,oracle报违反外键约束条件,即使子表中这个月份对应的数据删除了,也会的。

使用引用分区后,子表会继承其父表的分区机制,而不需要根据分区间逆向规范,更重要的是让数据库了解子表和父表的分区特点。也就是说截取或者删除子表时,也能删除父表

create table reference_Parent_example1

(
id number ,
startDate date,
constraint con_reference_example_pk1 primary key(id)
)
partition by range(startdate)
(
  partition reference_example_part1 values less than(to_date('2013-12-30','YYYY-MM-DD')) tablespace example,
  partition reference_example_part2 values less than(to_date('2014-1-30','YYYY-MM-DD')) tablespace learn
)
;
alter table reference_Parent_example1 enable row movement;


create table reference_Child_example1
(
id number primary key,
data varchar2(20),
ParentID number not null,
constraint con_fg1 foreign key (ParentID)references reference_Parent_example(id)

enable row movement  partition by reference(con_fg1);

SQL> select t.table_name,t.partition_name  from user_tab_partitions t where t.table_name=upper('reference_Child_example1') ;
TABLE_NAME                     PARTITION_NAME
------------------------------ ------------------------------
REFERENCE_CHILD_EXAMPLE1       REFERENCE_EXAMPLE_PART1
REFERENCE_CHILD_EXAMPLE1       REFERENCE_EXAMPLE_PART2

因为创建时,会根据父表分区个数,创建相同的子分区,子分区与父分区名字、存储表空间一致


插入数据

insert into reference_Parent_example1(id,Startdate) select level,to_date('2013-12-1','YYYY-MM-DD')+level from dual connect by level<60;

SQL> select count(*) from reference_Parent_example1 partition( reference_example_part2 );
  COUNT(*)
----------
        31

SQL> select * from reference_Child_example1 partition (REFERENCE_EXAMPLE_PART1);
未选定行
SQL> select * from reference_Child_example1 partition (REFERENCE_EXAMPLE_PART2);
未选定行
SQL>
alter table reference_Parent_example1 add partition reference_example_part3 values less than(to_date('2014-3-30','YYYY-MM-DD')) tablespace learn;

SQL> select t.table_name,t.partition_name  from user_tab_partitions t where t.table_name in(upper('reference_Child_example1'),upper('reference_Parent_example1') ) order by table_name;
TABLE_NAME                     PARTITION_NAME
------------------------------ ------------------------------
REFERENCE_CHILD_EXAMPLE1       REFERENCE_EXAMPLE_PART1
REFERENCE_CHILD_EXAMPLE1       REFERENCE_EXAMPLE_PART3
REFERENCE_CHILD_EXAMPLE1       REFERENCE_EXAMPLE_PART2
REFERENCE_PARENT_EXAMPLE1      REFERENCE_EXAMPLE_PART2
REFERENCE_PARENT_EXAMPLE1      REFERENCE_EXAMPLE_PART3
REFERENCE_PARENT_EXAMPLE1      REFERENCE_EXAMPLE_PART1

子表分区会根据父表分区增加


数据修改:
SQL> alter table reference_Child_example1 disable row movement;
表已更改。
SQL> alter table reference_Parent_example1 disable row movement;
表已更改。
SQL> update reference_Parent_example1 t set t.startdate=t.startdate+30 where t.startdate<to_date('2013-12-30','YYYY-MM-DD');
update reference_Parent_example1 t set t.startdate=t.startdate+30 where t.startdate<to_date('2013-12-30','YYYY-MM-DD')
       *第 1 行出现错误: ORA-14402: 更新分区关键字列将导致分区的更改
SQL> alter table reference_Child_example1 enable row movement;
表已更改。
SQL> alter table reference_Parent_example1 enable row movement;
表已更改。
SQL> update reference_Parent_example1 t set t.startdate=t.startdate+30 where t.startdate<to_date('2013-12-30','YYYY-MM-DD');
已更新28行。

记住一定要将父子表设置为允许行移动

注意:间隔分区不能使用到引用分区中。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值