Quantcast
Channel: Oracle
Viewing all articles
Browse latest Browse all 1814

Wiki Page: Oracle Database 12c – Partition Table Enhancements (Interval reference, Cascading Truncate and Exchange Operations) for Pluggable database (PDB) in Container Database (CDB)

$
0
0
By Y V Ravi Kumar , Oracle ACE and Oracle Certified Master Introduction: Oracle Database 12c addressed key issues in supporting large tables and indexes by divided into smaller and manageable parts called partitions. Here we can observe Interval reference partitioning, Exchanging partitions between Non-Partition Table to Partition table in pluggable database (cdb2_pdb1) in Container database (cdb2) Environment Setup: Database version : Oracle 12c (12.1.0.2.0) Operating System : Oracle Enterprise Linux 7 Container database : cdb2 with pluggable database (cdb2_pdb1) Login into container database (cdb2) and open pluggable database (cdb2_pdb1) [oracle@oralinux7 ~]$ . oraenv ORACLE_SID = [cdb1] ? cdb2 The Oracle base remains unchanged with value /u01/app/oracle [oracle@oralinux7 ~]$ sqlplus /nolog SQL*Plus: Release 12.1.0.2.0 Production on Mon May 16 09:00:40 2016 Copyright (c) 1982, 2014, Oracle. All rights reserved. SQL> connect sys/oracle@cdb2 as sysdba Connected. SQL> alter pluggable database all open; Pluggable database altered. Login into pluggable database (cdb2_pdb1) and create user and login as ‘scott’ user and create partition objects with reference functionality SQL> create user scott identified by oracle; User created. SQL> grant connect,resource to scott; Grant succeeded. SQL> connect scott/oracle@192.168.2.25:1521/cdb2_pdb1 Connected. SQL> Create table master_table ( No number not null, Name varchar2(200), constraint No_Pk primary key (No)) partition by range (No) interval (10) ( partition Part_p1 values less than (10) ); Table created. SQL> create table Child_table ( No number not null, Name varchar2(200), SecNo number not null, constraint No_Pk2 primary key (No), constraint SecNo_Fk foreign key (secNo) references master_table (No) on delete cascade) partition by reference (SecNo_Fk); Table created. SQL> select table_name, partition_name from user_tab_partitions; TABLE_NAME PARTITION_NAME ------------------- --------------------------- CHILD_TABLE PART_P1 MASTER_TABLE PART_P1 Insert data into above created partition table called master_table and child_table with range partition with reference relation and verify partitions SQL> insert into MASTER_TABLE values (1,'ORACLE'); 1 row created. SQL> insert into MASTER_TABLE values (15,'MYSQL'); 1 row created. SQL> COMMIT; Commit complete. SQL> select table_name, partition_name from user_tab_partitions where table_name like '%TABLE%' TABLE_NAME PARTITION_NAME --------------------- ------------------------- CHILD_TABLE PART_P1 MASTER_TABLE PART_P1 MASTER_TABLE SYS_P201 Note: Additional partition with the name ‘SYS_P201’ has been automatically created for the master_table SQL> insert into MASTER_TABLE values (100,'EXADATA'); 1 row created. SQL> commit; Commit complete. SQL> select table_name, partition_name from user_tab_partitions where table_name like '%TABLE%' TABLE_NAME PARTITION_NAME ---------------------- ------------------------- CHILD_TABLE PART_P1 MASTER_TABLE PART_P1 MASTER_TABLE SYS_P201 MASTER_TABLE SYS_P202 Note: One more additional partition with the name ‘SYS_P202’ has been automatically created for the master_table Insert data into child_table with reference key and check the partitions SQL> insert into CHILD_TABLE values (1,'EXADATA',100); 1 row created. SQL> COMMIT; Commit complete. SQL> select table_name, partition_name from user_tab_partitions where table_name like '%TABLE%' TABLE_NAME PARTITION_NAME ---------------------- ------------------------- CHILD_TABLE PART_P1 CHILD_TABLE SYS_P202 MASTER_TABLE PART_P1 MASTER_TABLE SYS_P201 MASTER_TABLE SYS_P202 SQL> insert into CHILD_TABLE values (2,'MYSQL',15); 1 row created. SQL> COMMIT; Commit complete. SQL> select table_name, partition_name from user_tab_partitions where table_name like '%TABLE%' TABLE_NAME PARTITION_NAME --------------------- ------------------------- CHILD_TABLE PART_P1 CHILD_TABLE SYS_P201 CHILD_TABLE SYS_P202 MASTER_TABLE PART_P1 MASTER_TABLE SYS_P201 MASTER_TABLE SYS_P202 6 rows selected. Note: Additional partitions with the names ‘SYS_P201’ and ‘SYS_P202’ has been automatically created for the child_table SQL> select * from master_table; NO NAME ---------- ------------ 1 ORACLE 15 MYSQL 100 EXADATA SQL> select * from child_table; NO NAME SECNO ---------- -------------------- ---------- 2 MYSQL 15 1 EXADATA 100 Rename the partition for the table master_table and check the effects and behavior of the table child_table SQL> alter table MASTER_TABLE truncate partition for (100) cascade update indexes; Table truncated. SQL> select table_name, partition_name from user_tab_partitions where table_name like '%TABLE%' TABLE_NAME PARTITION_NAME ------------------ ------------------------- CHILD_TABLE PART_P1 CHILD_TABLE SYS_P201 CHILD_TABLE SYS_P202 MASTER_TABLE PART_100 MASTER_TABLE PART_P1 MASTER_TABLE SYS_P201 6 rows selected. Note: If you change the partition name for master_table it won’t effect for child_table and you can change partition for child_table manually SQL> alter table CHILD_TABLE rename partition for (100) to PART_CHILD_100; Table altered. SQL> select table_name, partition_name from user_tab_partitions where table_name like '%TABLE%' TABLE_NAME PARTITION_NAME ---------------------- -------------------------- CHILD_TABLE PART_CHILD_100 CHILD_TABLE PART_P1 CHILD_TABLE SYS_P201 MASTER_TABLE PART_100 MASTER_TABLE PART_P1 MASTER_TABLE SYS_P201 6 rows selected. Check the above partitions after renaming for the tables master_table and child_table SQL> alter table MASTER_TABLE truncate partition for (100) cascade update indexes; Table truncated. SQL> select * from master_table; NO NAME ---------- ------------- 1 ORACLE 15 MYSQL SQL> select * from child_table; NO NAME SECNO ---------- ----------- ---------- 2 MYSQL 15 If you truncate the partition with data ‘100’ in master_table it automatically truncates in child_table for the partition data ‘100’. Exchange Partitions between Non-Partition Table to Partition Table with Child Tables: Check the data in master_table and child_table before executing exchange partition functionality SQL> select * from master_table; NO NAME ---------- ---------------------- 1 Part_1 - Partition 2 Part_2 - Partition SQL> select * from child_table; NO NAME SECNO ---------- -------------------- ---------- 100 Part_100 - Partition 1 200 Part_200 - Partition 2 Create Non-Partitioned tables master_table_ref and child_table_ref with data referenced data SQL> create table master_table_ref ( No number not null, Name varchar2(200), constraint No_ref_Pk primary key (No)); create table child_table_ref ( No number not null, Name varchar2(200), SecNo number not null, constraint No_pk3 primary key (No), constraint secNo_Fk1 foreign key (secNo) references master_table_ref (No) on delete cascade); Insert the data for both the tables with reference SQL>insert into master_table_ref values (3, 'Part_3 - Non Partitioned data'); 1 row created. SQL>insert into child_table_ref values (3, 'Part_3 - Non Partitioned data',3); 1 row created. SQL>commit; Commit complete. Check all the data for the tables before exchanging the partitions: master_table, child_table, master_table_ref and child_table_ref SQL> select * from master_table; NO NAME ---------- ---------------------- 1 Part_1 - Partition 2 Part_2 - Partition SQL> select * from child_table; NO NAME SECNO ---------- -------------------------- ---------- 100 Part_100 - Partition 1 200 Part_200 - Partition 2 SQL> select * from master_table_ref; NO NAME ---------- --------------------------------------- 3 Part_3 - Non Partitioned data SQL> select * from child_table_ref; NO NAME SECNO ---------- ---------------------------------------- ---------- 3 Part_3 - Non Partitioned data 3 Now exchange the partitions and check the data SQL> alter table master_table exchange partition for (3) with table master_table_ref cascade update indexes; Table altered. SQL> select * from master_table; NO NAME ---------- --------------------------- 3 Part_3 - Non Partitioned data SQL> select * from child_table; NO NAME SECNO ---------- --------------------------------------- ---------- 3 Part_3 - Non Partitioned data 3 SQL> select * from master_table_ref; NO NAME ---------- ----------------------- 1 Part_1 - Partition 2 Part_2 - Partition SQL> select * from child_table_ref; NO NAME SECNO ---------- -------------------- ---------- 100 Part_100 - Partition 1 200 Part_200 - Partition 2 Summary : If you exchange the partition with Non-Partition table (master_table_ref) to Partition table (master_table) it will exchange dependent child tables (child_table & child_table_ref) also.

Viewing all articles
Browse latest Browse all 1814

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>