引用分區

引用分區:處理父子表對等分區的問題,以這種方式處理分區,父表與子表的分區是一對一關係。在維護主子表。如果不是按照引用分區,即使父子表分區一致,也會因爲引用的約束,導致主表數據無法維護:例如下面的表,如果子表中將父表的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行。

記住一定要將父子表設置爲允許行移動

注意:間隔分區不能使用到引用分區中。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章