How do I create a partitioned table without initial partitions? - oracle

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.

Related

add interval monthly partition

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'))
);

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:

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.

Oracle Batch Database

I'm trying to run my databases but STUDENTS AND BATCHES tables are giving the error: 'ORA-00942: table or view does not exist'. I've tried dropping them in order, but I can't tell what exactly the problem is.
Forgive me if the answer is obvious, I'm new to batches
DROP TABLE students;
DROP TABLE batches;
DROP TABLE courses;
DROP TABLE faculty;
CREATE TABLE batches (
bcode varchar2(5) CONSTRAINT batches_PK PRIMARY KEY,
ccode varchar2(5) CONSTRAINT batches_ccode_FK REFERENCES COURSES(ccode),
fcode varchar2(5) CONSTRAINT batches_fcode_FK REFERENCES FACULTY(fcode),
stdate date CONSTRAINT batches_stdate_nn not null,
enddate date,
timing number(1) CONSTRAINT batches_timing_chk check( timing in (1,2,3) ),
CONSTRAINT batches_date_chk check ( stdate <= enddate)
);
CREATE TABLE students (
rollno number(5) CONSTRAINT students_PK PRIMARY KEY,
bcode varchar2(5) CONSTRAINT students_bcode_FK REFERENCES batches(bcode),
name varchar2(30),
gender char(1) CONSTRAINT students_gender_chk check( upper(gender) in ('M','F')),
dj date,
phone varchar2(10),
email varchar2(30)
);
CREATE TABLE courses (
ccode VARCHAR2(10) CONSTRAINT courses_PK PRIMARY KEY,
cname VARCHAR2(50),
coursefee NUMBER(6),
prereq VARCHAR2(100)
);
CREATE TABLE faculty (
fcode VARCHAR2(5) CONSTRAINT faculty_PK PRIMARY KEY,
name VARCHAR2(50)
);
Create your faculty and courses tables first. Since batches depends on one of those tables, you would want to have those tables created first.
CREATE TABLE courses (
ccode VARCHAR2(10) CONSTRAINT courses_PK PRIMARY KEY,
cname VARCHAR2(50),
coursefee NUMBER(6),
prereq VARCHAR2(100)
);
CREATE TABLE faculty (
fcode VARCHAR2(5) CONSTRAINT faculty_PK PRIMARY KEY,
name VARCHAR2(50)
);
CREATE TABLE batches (
bcode varchar2(5) CONSTRAINT batches_PK PRIMARY KEY,
ccode varchar2(5) CONSTRAINT batches_ccode_FK REFERENCES COURSES(ccode),
fcode varchar2(5) CONSTRAINT batches_fcode_FK REFERENCES FACULTY(fcode),
stdate date CONSTRAINT batches_stdate_nn not null,
enddate date,
timing number(1) CONSTRAINT batches_timing_chk check( timing in (1,2,3) ),
CONSTRAINT batches_date_chk check ( stdate <= enddate)
);
CREATE TABLE students (
rollno number(5) CONSTRAINT students_PK PRIMARY KEY,
bcode varchar2(5) CONSTRAINT students_bcode_FK REFERENCES batches(bcode),
name varchar2(30),
gender char(1) CONSTRAINT students_gender_chk check( upper(gender) in ('M','F')),
dj date,
phone varchar2(10),
email varchar2(30)
);
Example: http://sqlfiddle.com/#!4/91909c/1 shows the sequence of table creation

oracle partition by group_id and subpartition monthly

I want to create a table like this.
create table some_data (
id number(19,0),
group_id number(19,0),
value float,
timestamp timestamp
);
For this table i would like to have the data stored like
group_id=1
jan-2015
feb-2015
...
group_id=2
jan-2015
feb-2015
...
and so on. So I assume i have to create a partition by range for the group_id and then a subpartition also by range with the timestamp column, right?
So it should look like this:
create table some_data (
id number(19,0),
group_id number(19,0),
value float,
timestamp timestamp
)
PARTITION BY RANGE (group_id)
SUBPARTITION BY RANGE ("TIMESTAMP")
INTERVAL (NUMTOYMINTERVAL(1,'MONTH'))
(
PARTITION part_1 values LESS THAN (TO_DATE('01.02.2015','DD.MM.YYYY'))
);
Is this right? And also the question: With this partition, if a new group_id is added, will oracle create automatically a new partition for the new group_id and the new suppartitions for new data with new months?
Interval partitioning is not supported on subpartition level:
http://docs.oracle.com/cd/E11882_01/server.112/e41084/statements_7002.htm#SQLRF54559
You can define it like this:
create table some_data (
id number(19,0),
group_id number(19,0),
value float,
timestamp timestamp -- not good naming
)
PARTITION BY RANGE ("TIMESTAMP")
INTERVAL (NUMTOYMINTERVAL(1,'MONTH'))
SUBPARTITION BY RANGE (group_id) -- it could be hash or list as well
subpartition template(
...
)
(
PARTITION part_1 values LESS THAN (TO_DATE('01.02.2015','DD.MM.YYYY'))
);

Resources