As with other partitioned tables, you can specify object-level
default attributes, and you can optionally specify partition descriptors
that override the object-level defaults on a per-partition basis.Reference partitioning is a new
partitioning scheme in Oracle Database 11g that lets you partition a
table on the basis of the partitioning scheme of the table that its
reference constraint refers to. Reference partitioning is probably the
hardest new partitioning scheme to grasp. The
partitioning key is determined through the parent- child relationship
between the tables, as enforced by the active primary key or foreign key
constraints. Reference partitioning thus lets you logically
equipartition a table inheriting the partitioning the key from its
parent table. You thus don’t have to duplicate the key columns.
Partition maintenance operations are no problem because the database
automatically maintains the logical dependency between the two tables
during those operations.You can’t use interval partitioning with reference partitioning.Unlike
in Oracle Database 10g, where partition-wise joins would work only if
the partitioning and predicates were identical, reference partitioning
has no such limitation. That is, a partition-wise join will work even
when query predicates are different.
SQL> CREATE TABLE new_orders
2 ( order_id NUMBER(12),
3 order_date TIMESTAMP(6),
4 order_mode VARCHAR2(8),
5 customer_id NUMBER(6),
6 order_status NUMBER(2),
7 order_total NUMBER(8,2),
8 sales_rep_id NUMBER(6),
9 promotion_id NUMBER(6),
10 CONSTRAINT new_orders_pk PRIMARY KEY(order_id)
11 )
12 PARTITION BY RANGE(order_date)
13 ( PARTITION Q1_2005 VALUES LESS THAN (TO_DATE('01-Feb-2005','DD-MON-YYYY')),
14 PARTITION Q2_2005 VALUES LESS THAN (TO_DATE('01-May-2005','DD-MON-YYYY')),
15 PARTITION Q3_2005 VALUES LESS THAN (TO_DATE('01-Sep-2005','DD-MON-YYYY')),
16 PARTITION Q4_2005 VALUES LESS THAN (TO_DATE('01-Dec-2005','DD-MON-YYYY'))
17 );
Table created.
SQL> CREATE TABLE new_orders_items
2 ( order_id NUMBER(12) NOT NULL,
3 line_item_id NUMBER(3) NOT NULL,
4 product_id NUMBER(6) NOT NULL,
5 unit_price NUMBER(8,2) NOT NULL,
6 quantity NUMBER(8) NOT NULL,
7 CONSTRAINT new_orders_items_fk
8 FOREIGN KEY(order_id) REFERENCES new_orders(order_id)
9 )
10 PARTITION BY REFERENCE(new_orders_items_fk);
Table created.
child table:
SQL> SELECT table_name, partition_name, high_value, num_rows
2 FROM user_tab_partitions
3 where table_name='NEW_ORDERS_ITEMS'
4 ORDER BY table_name, partition_name;
TABLE_NAME PARTITION_NAME HIGH_VALUE NUM_ROWS
------------------------- -------------------- ---------------------------------------- ----------
NEW_ORDERS_ITEMS Q1_2005
NEW_ORDERS_ITEMS Q2_2005
NEW_ORDERS_ITEMS Q3_2005
NEW_ORDERS_ITEMS Q4_2005
SQL> select count(*) from NEW_ORDERs_ITEMS;
COUNT(*)
----------
3
the parent table:
SQL> SELECT table_name, partition_name, high_value, num_rows
2 FROM user_tab_partitions
3 where table_name='NEW_ORDERS'
4 ORDER BY table_name, partition_name;
TABLE_NAME PARTITION_NAME HIGH_VALUE NUM_ROWS
------------------------- -------------------- ---------------------------------------- ----------
NEW_ORDERS Q1_2005 TIMESTAMP' 2005-02-01 00:00:00'
NEW_ORDERS Q2_2005 TIMESTAMP' 2005-05-01 00:00:00' 1
NEW_ORDERS Q3_2005 TIMESTAMP' 2005-09-01 00:00:00' 1
NEW_ORDERS Q4_2005 TIMESTAMP' 2005-12-01 00:00:00' 1
SQL> select table_name, partitioning_type, ref_ptn_constraint_name
2 from user_part_tables
3 where table_name in ('NEW_ORDERS','NEW_ORDERS_ITEMS');
TABLE_NAME PARTITION REF_PTN_CONSTRAINT_NAME
------------------------- --------- ------------------------------
NEW_ORDERS RANGE
NEW_ORDERS_ITEMS REFERENCE NEW_ORDERS_ITEMS_FK
SQL>
SQL> select table_name, partition_name, high_value
2 from user_tab_partitions
3 where table_name='NEW_ORDERS'
4 or
5 table_name='NEW_ORDERS_ITEMS'
6 /
TABLE_NAME PARTITION_NAME HIGH_VALUE
------------------------- -------------------- ----------------------------------------
NEW_ORDERS Q1_2005 TIMESTAMP' 2005-02-01 00:00:00'
NEW_ORDERS Q2_2005 TIMESTAMP' 2005-05-01 00:00:00'
NEW_ORDERS Q3_2005 TIMESTAMP' 2005-09-01 00:00:00'
NEW_ORDERS Q4_2005 TIMESTAMP' 2005-12-01 00:00:00'
NEW_ORDERS_ITEMS Q1_2005
NEW_ORDERS_ITEMS Q2_2005
NEW_ORDERS_ITEMS Q3_2005
NEW_ORDERS_ITEMS Q4_2005
8 rows selected.
Conditions and Restrictions
The following conditions and restrictions apply to reference partitioning:
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/13750068/viewspace-2144143/ ,如需转载,请注明出处,否则将追究法律责任。