add interval monthly partition - oracle

I want to create this table, with a monthly partition on the endTime column.
I mean each month a partition added automatically by oracle.
create table T_CALLSESSION() PARTITON BY RANGE (C_ENDTIME )
INTERVAL(NUMTOYMINTERVAL(1,'month'); (
C_ID NUMBER(34, 0) not null,
C_ENDTIME timestamp not null,
C_STARTTIME timestamp not null,
C_TYPE number(10,0) not null,
F_CREATOR NUMBER(34, 0),
F_MESSAGE_THREAD NUMBER(34, 0),
primary key (C_ID)
);
is that works?

There are a few mistakes in your code.
Table name should not contain the parenthesis ()
The PARTITION clause must be after the declaration of columns and constraints.
You must use the INTERVAL partition so that new partitions are automatically created.
One partition must be created with some constant values and then after other partitions will be automatically created.
Use the following code:
create table T_CALLSESSION (
C_ID NUMBER(34, 0) not null,
C_ENDTIME timestamp not null,
C_STARTTIME timestamp not null,
C_TYPE number(10,0) not null,
F_CREATOR NUMBER(34, 0),
F_MESSAGE_THREAD NUMBER(34, 0),
primary key (C_ID)
) PARTITION BY RANGE (C_ENDTIME)
INTERVAL(NUMTOYMINTERVAL(1, 'MONTH'))
(
PARTITION T_CALLSESSION_P1 VALUES LESS THAN (TO_DATE('01-06-2020', 'DD-MM-YYYY'))
);

Related

Adding reference partitioning to oracle table

on the 12c version of oracle I have created T_App table like this:
create table T_APP
(
C_ID NUMBER(34, 0) not null,
C_ACTIVE number(1, 0) not null,
C_APPID varchar2(255 char) not null,
C_CONTACTTYPE varchar2(255 char) not null,
primary key (C_ID)
)
partition by list(C_CONTACTTYPE)
(
partition p1 values ('default')
);
and T_APPUSER table like this:
create table T_APPUSER
(
C_ID NUMBER(34, 0) primary key,
C_ACTIVE number(1, 0) not null,
C_DEVICETOKEN varchar2(255 char),
C_SSOID varchar2(255 char),
F_APP NUMBER(34, 0) not null,
constraint FK_APP
foreign key (F_APP)
references T_APP
)
PARTITION BY REFERENCE (FK_APP);
but when I run the query of creating T_APPUSER I get ERROR!
is there anyThing wrong with my query for creating the T_APPUSER table?

How to create subpartition for Reference-Partitioned Tables

Consider the following scenario of Reference-Partitioned Tables. The table T_PARTNER_CUSTOMER borrows partition part1 and part2 from table T_PARTNER_CONFIG.
CREATE TABLE T_PARTNER_CONFIG (
ID NUMBER(38) NOT NULL,
SECURITY_CONTEXT_ID NUMBER(38) NOT NULL
)
PARTITION BY LIST(SECURITY_CONTEXT_ID)
(PARTITION part1 VALUES (0, 1, 2),
PARTITION part2 VALUES (4, 5, 6));
CREATE TABLE T_PARTNER_CUSTOMER (
ID NUMBER(38) NOT NULL,
PARTNER_CONFIG_ID NUMBER(38) NOT NULL
CUSTOMER_NUMBER VARCHAR2(10) NOT NULL,
CONSTRAINT FK_PC_PARTNER_CONFIG
FOREIGN KEY(PARTNER_CONFIG_ID) REFERENCES T_PARTNER_CONFIG (id)
)
PARTITION BY REFERENCE(FK_PC_PARTNER_CONFIG);
Now if we want to create further subpartition for table T_PARTNER_CUSTOMER, so that the partition part1 and part2 are further subdivided for table T_PARTNER_CUSTOMER. I tried the approach below, but it doesn't work. I am using Oracle 11g Enterprise edition.
CREATE TABLE T_PARTNER_CUSTOMER (
ID NUMBER(38) NOT NULL,
PARTNER_CONFIG_ID NUMBER(38) NOT NULL
CUSTOMER_NUMBER VARCHAR2(10) NOT NULL,
CONSTRAINT FK_PC_PARTNER_CONFIG
FOREIGN KEY(PARTNER_CONFIG_ID) REFERENCES T_PARTNER_CONFIG (id)
)
PARTITION BY REFERENCE(FK_PC_PARTNER_CONFIG)
SUBPARTITION BY HASH (CUSTOMER_NUMBER) SUBPARTITIONS 8;
Seems to be impossible, see documentation:
There is no "SUBPARTITON" clause available, unlike for other partition type, e.g. LIST:

Oracle Trigger to loop and update field based on sysdate

I would like to make a trigger that can update a field (‘STATUS’) to Inactive when a vaccine is past its expiration date (‘EXPIRATION_DATE’).
Here is the current table structure:
**CREATE TABLE WAREHOUSE.VACCINE_INVENTORY (
VACCINE VARCHAR2(200 BYTE) NOT NULL,
RECEIPT_DATE DATE NOT NULL,
CONTAINER_SIZE VARCHAR2(200 BYTE),
QUANTITY NUMBER(6, 0),
REQUISITION NUMBER(6, 0),
FISCAL_YEAR NUMBER(4, 0),
RECEIVED_BY VARCHAR2(50 BYTE),
EXPIRATION_DATE DATE,
LOT_NUMBER VARCHAR2(30 BYTE) NOT NULL,
VENDOR VARCHAR2(200 BYTE),
STATUS VARCHAR2(10 BYTE),
CATALOG_NUMBER NUMBER(5, 0),
CONSTRAINT PK_VACC PRIMARY KEY (VACCINE, RECEIPT_DATE, LOT_NUMBER) USING INDEX TABLESPACE WAREHOUSE STORAGE**
So the idea is that if SYSDATE > EXPIRATION_DATE the expiration date should be changed from Active to Inactive.
I would assume I would need to create a Loop so that when the trigger is run, it will loop through all the records in the table and set each expired vaccine to Inactive.
I would greatly appreciate any help…
Thanks,
Matthew
Create an additional view to provide fields that depend on table columns and sysdate, e.g.
CREATE TABLE VACCINE_INVENTORY(VACCINE VARCHAR2(200 BYTE) NOT NULL,
-- ---
EXPIRATION_DATE DATE NOT NULL
-- ...
);
CREATE view VACCINE_INVENTORY_VW AS
select v.*,
case
when v.expiration_date < sysdate then
'I'
else
'A'
end as status
from VACCINE_INVENTORY v;

Tablespaces with interval partitioning

Is there any way to specify different tablespaces for each partition if I use interval partitioning?
My table is (it is just example, I have more columns in the table):
create table MY_TABLE
(
id NUMBER(20) not null,
type VARCHAR2(1 char) not null,
session_id NUMBER(12) not null,
date_of_beginning DATE not null,
account_number NUMBER not null
)
PCTFREE 1
PARTITION BY RANGE (date_of_beginning)
INTERVAL(NUMTOYMINTERVAL(1, ''MONTH''))
SUBPARTITION BY HASH(account_number) subpartitions 50
(
PARTITION p_1 VALUES LESS THAN (TO_DATE(''01.06.2015'', ''dd.mm.yyyy''))
)';
I want each partition to be stored in separate tablespase. Any ideas how can I achieve it? Maybe some trigger?
For other tables like this one, we have a job, which creates tablespace and partition in the beginning of the month. But this one creates partitions automatically because of intervals.

How do I create a partitioned table without initial partitions?

Say, I have the following DDL:
create table partition_test_table
(
id number(38,0) not null,
value varchar(255),
country_code varchar(2) not null,
creation_date date not null,
constraint pk_partition_test_table primary key (id)
)
partition by range ( creation_date )
subpartition by list ( country_code );
This doesn't work because it doesn't declare any initial partitions. How do I create a partitioned table without initial partitions? Is that even possible?
Thanks.

Resources