Before You Begin
This 15-minute tutorial shows you how to create a hybrid partitioned table with both internal and external partitions and manage the internal and external partitions.
Background
Oracle Database 12c allows partitions of a partitioned table to be created either as internal partitions in the database in Oracle data files or in external sources as external partitions.
Oracle Database 19c allows a partitioned table to hold both internal partitions and external partitions. These partitioned tables are called hybrid partitioned tables.
What Do You Need?
- Oracle Database 19c installed
- A database, either non-CDB or CDB with a PDB
- Tablespaces created in the non-CDB or PDB for internal partitions of hybrid partitioned tables
- Source data files for external partitions of hybrid
partitioned tables:
cent17.dat, cent19.dat,andcent20.dat. Download the .dat files to the labs directory created on your server in their respective subdirectories,/home/oracle/labs/CENT17,/home/oracle/labs/CENT19, and/home/oracle/labs/CENT20 - The
create_hybrid_table.sqlSQL script. Download the SQL script to the labs directory created on your server/home/oracle/labs - The
insert.sqlSQL script. Download the SQL script to the labs directory created on your server/home/oracle/labs - The
insert2.sqlSQL script. Download the SQL script to the labs directory created on your server/home/oracle/labs
Create
the Tablespaces for the Internal Partitions
In this section, you create two tablespaces to store data of the internal partitions, one of the two tablespaces to be the default tablespace for internal partitions.
- Log in to the PDB as
SYSTEM.sqlplus system@PDB1 Enter password: password
- Create the tablespaces
TS1andTS2to store internal partitions of the hybrid partitioned table.CREATE TABLESPACE ts1 DATAFILE '/u02/app/oracle/oradata/ORCL/pdb1/ts1.dbf' SIZE 100M;
CREATE TABLESPACE ts2 DATAFILE '/u02/app/oracle/oradata/ORCL/pdb1/ts2.dbf' SIZE 100M;
Create
the Logical Directories for the External Partitions
In this section, you create the logical directories to store the source data files for external partitions.
- Create the logical directory
CENT18to store the source data filecent18.datfor theCENT18external partition.CREATE DIRECTORY cent18 AS '/home/oracle/labs/CENT18';
- Create the logical directory
CENT19to store the source data filecent19.datfor theCENT19external partition.CREATE DIRECTORY cent19 AS '/home/oracle/labs/CENT19';
- Create the logical directory
CENT20to store the source data filecent20.datfor theCENT20external partition.CREATE DIRECTORY cent20 AS '/home/oracle/labs/CENT20';
Create
the Hybrid Partitioned Table
- Create the user that owns the hybrid partitioned table.
CREATE USER hypt IDENTIFIED BY password;
- Grant the read and write privileges on the directories that
store the source data files to the table owner.
GRANT read, write ON DIRECTORY cent18 TO hypt;
GRANT read, write ON DIRECTORY cent19 TO hypt;
GRANT read, write ON DIRECTORY cent20 TO hypt;
- Grant the
CREATE SESSION,CREATE TABLEandUNLIMITED TABLESPACEprivileges to the table owner.GRANT create session, create table, unlimited tablespace TO hypt;
- Execute the
create_hybrid_table.sqlSQL script to create theHYPT_TABhybrid partitioned table with the following attributes:- The table is partitioned by range on the
TIME_IDcolumn. - The default tablespace for internal partitions is
TS1. - The default tablespace for external partitions is
CENT20. - The fields in the records of the external files are separated by ','.
- The table is partitioned into five parts:
- Three external partitions:
CENT18is empty for the moment;CENT19has thecent19.datfile stored in another directory than the default,CENT19;CENT20has thecent20.datfile stored in the default directory. - Two internal partitions:
Y2000is stored in tablespaceTS2andPMAXis stored in the default tablespaceTS1.
- Three external partitions:
@/home/oracle/labs/create_hybrid_table.sql
- The table is partitioned by range on the
Insert
Data Into the Partitions
- Insert rows into the internal partitions of the table.
Execute the
insert.sqlSQL script.@/home/oracle/labs/insert.sql
- Insert a row for the date of 12 August 1997.
INSERT INTO hypt.hypt_tab VALUES (41, to_date('12.08.1997', 'dd.mm.yyyy')); INSERT INTO hypt.hypt_tab * ERROR at line 1: ORA-14466: Data in a read-only partition or subpartition cannot be modified.The data can be inserted into the external partition only via the external source data file. - Insert the data for the date of 12 August 1997 into the
appropriate external source data file.
host echo "41,12-Aug-1997" >> /home/oracle/labs/CENT20/cent20.dat
- Verify that the row is readable from the appropriate
external partition
CENT20.SELECT history_event, TO_CHAR(time_id, 'dd-MON-yyyy') FROM hypt.hypt_tab PARTITION (cent20) ORDER BY 1;
Read the result from theresult1text file. - Append another record into the external source data file
CENT19.host echo "42,12-Aug-1997" >> /home/oracle/labs/CENT19/cent19.dat
SELECT history_event, TO_CHAR(time_id, 'dd-MON-yyyy') FROM hypt.hypt_tab PARTITION (cent19) ORDER BY 1;
Read the result from theresult2text file. Observe that the row is readable from the external partitionCENT19although the row should be stored in another partition. There is no control on theTIME_IDof the records inserted as rows into the external partitions, as it is the case for rows inserted into internal partitions. - Use an text editor to remove the record inserted into
/home/oracle/labs/CENT19/cent19.dat. - Query the rows of the five partitions.
SELECT history_event, TO_CHAR(time_id, 'dd-MON-yyyy') FROM hypt.hypt_tab PARTITION (cent18) ORDER BY 1; no rows selected
SELECT history_event, TO_CHAR(time_id, 'dd-MON-yyyy') FROM hypt.hypt_tab PARTITION (cent19) ORDER BY 1;
Read the result from theresult3text file.SELECT history_event, TO_CHAR(time_id, 'dd-MON-yyyy') FROM hypt.hypt_tab PARTITION (cent20) ORDER BY 1;
Read the result from theresult1text file.SELECT history_event, TO_CHAR(time_id, 'dd-MON-yyyy') FROM hypt.hypt_tab PARTITION (y2000) ORDER BY 1;
Read the result from theresult4text file.SELECT history_event, TO_CHAR(time_id, 'dd-MON-yyyy') FROM hypt.hypt_tab PARTITION (pmax) ORDER BY 1; HISTORY_EVENT TO_CHAR(TIME_ID,'DD- ------------- -------------------- 29 12-AUG-2018 30 15-SEP-2017
List
the Partitions of the Hybrid Partitioned Table
- Distinguish the partitioned tables from the hybrid
partitioned tables. Verify the existence of the hybrid
partitioned table in the
DBA_EXTERNAL_TABLESview and its associated partitions from theDBA_TAB_PARTITIONSview.SELECT * FROM dba_external_tables WHERE owner = 'HYPT';
Read the result from thehypt_tabletext file.SELECT partition_name, high_value FROM dba_tab_partitions WHERE table_name = 'HYPT_TAB' ORDER BY 1;
Read the result from thepartitionstext file.
Add
and Remove External Partitions
In this section, you add an external partition to the internal
partitioned table HYPT.PART_TAB for the 17th
century. The external file cent17.dat source data file
storing historic events of the 17th century is stored in the
directory /home/oracle/labs/CENT17.
- Drop the hybrid partitioned table.
DROP TABLE hypt.hypt_tab;
- Create another partitioned table with internal partitions
only.
CREATE TABLE hypt.part_tab (history_event NUMBER , time_id DATE) TABLESPACE ts1 PARTITION BY RANGE (time_id) (PARTITION cent18 VALUES LESS THAN (TO_DATE('01-Jan-1800','dd-MON-yyyy')) , PARTITION cent19 VALUES LESS THAN (TO_DATE('01-Jan-1900','dd-MON-yyyy')) , PARTITION cent20 VALUES LESS THAN (TO_DATE('01-Jan-2000','dd-MON-yyyy')) , PARTITION y2000 VALUES LESS THAN (TO_DATE('01-Jan-2001','dd-MON-yyyy'))
TABLESPACE ts2, PARTITION pmax VALUES LESS THAN (MAXVALUE)); - Execute the
insert2.sqlSQL script to insert rows into the internal partitions of thePART_TABtable.@/home/oracle/labs/insert2.sql
- Display the rows in the table.
SELECT * FROM hypt.part_tab ORDER BY 1;
Read the result from theresult5text file. - Create the logical directory for the
/home/oracle/lab/CENT17directory.CREATE DIRECTORY cent17 AS '/home/oracle/labs/CENT17';
- Grant the read and write privileges on the directory to
HYPT.
GRANT read, write ON DIRECTORY cent17 TO hypt;
- Define the external parameters for all external partitions
that might be added to the
HYPT.PART_TABtable.ALTER TABLE hypt.part_tab ADD EXTERNAL PARTITION ATTRIBUTES (TYPE ORACLE_LOADER DEFAULT DIRECTORY cent17 ACCESS PARAMETERS (FIELDS TERMINATED BY ',' (history_event , time_id DATE 'dd-MON-yyyy')) REJECT LIMIT UNLIMITED ); - Split the first partition to a partition limit that will be
the high limit of the partition added.
ALTER TABLE hypt.part_tab SPLIT PARTITION cent18 AT (TO_DATE('01-Jan-1700','dd-MON-yyyy')) INTO (PARTITION cent17 EXTERNAL LOCATION ('cent17.dat'), PARTITION cent18); - Read the rows of the external partition.
SELECT history_event, TO_CHAR(time_id, 'dd-MON-yyyy') FROM hypt.part_tab PARTITION (cent17) ORDER BY 1;
Read the result from theresult6text file. - The partition storing 17th century historic events is no
longer required. Remove the external parameters for the hybrid
partitioned that was added to the
HYPT.PART_TABtable.ALTER TABLE hypt.part_tab DROP EXTERNAL PARTITION ATTRIBUTES(); ALTER TABLE hypt.part_tab DROP EXTERNAL PARTITION ATTRIBUTES() * ERROR at line 1: ORA-14354: operation not supported for a hybrid-partitioned table
External partitions must be dropped first before you can remove external attributes at the table level. - Drop the external partition from the
HYPT.PART_TABtable. Because there is one external partition left, the attributes for the external partitions cannot be removed from the hybrid partitioned table.ALTER TABLE hypt.part_tab DROP PARTITION cent17;
- Remove the external parameters for the hybrid partitioned
HYPT.PART_TABtable.ALTER TABLE hypt.part_tab DROP EXTERNAL PARTITION ATTRIBUTES();
Clean
Up the Environment
- Drop the hybrid partitioned
HYPT.PART_TABtable.DROP TABLE hypt.part_tab PURGE;
- Quit the session.
EXIT
Manage
Partitions in Hybrid Partitioned Tables