12.2的分区新特性中引入了只读分区的特性,可以帮助我们将某些分区的数据进行静态化保护。
这一特性通过将表或者分区设置为READ ONLY或者READ WRITE来进行控制,以下是一个测试Demo(可以在 https://livesql.oracle.com )网站验证体验。
以下测试首先将数据表置为READ ONLY模式,对部分分区设置为READ WRITE的读写模式:
drop table YHEM_ODS;
CREATE TABLE YHEM_ODS
(oid number,
odate DATE,
omount number
) read only
PARTITION BY RANGE(odate)
(partition q1_2016 values less than (to_date('2016-04-01','yyyy-mm-dd')),
partition q2_2016 values less than (to_date('2016-07-01','yyyy-mm-dd')),
partition q3_2016 values less than (to_date('2016-10-01','yyyy-mm-dd')) read write,
partition q4_2016 values less than (to_date('2017-01-01','yyyy-mm-dd')) read write)
/
insert into YHEM_ODS values(1,to_date('2016-01-20','yyyy-mm-dd'),100);
insert into YHEM_ODS values(1,to_date('2016-10-20','yyyy-mm-dd'),100);
insert into YHEM_ODS values(1,to_date('2016-12-20','yyyy-mm-dd'),100);
select * from yhem_ods;
select * from user_tab_partitions;
alter table YHEM_ODS modify partition q2_2016 read write;
如果向着只读分区插入数据,则会收到ORA-14466的错误提示:
读写分区可以自有的进行数据变更:
最后可以查询这些分区的数据:
分区的属性可以通过modify语句进行在线修改。
alter table YHEM_ODS modify partition q2_2016 read write;
SELECT PARTITION_NAME, READ_ONLY FROM USER_TAB_PARTITIONS;
通过分区表增加的『Read_Only』字段可以查看哪些分区被设置为只读状态:
在官方手册上有如下的范例,对表、分区和子分区都做出了设置,初始状态全表设置为读写,部分分区设置为只读状态:
CREATE TABLE orders_read_write_only (
order_id NUMBER (12),
order_date DATE CONSTRAINT order_date_nn NOT NULL,
state VARCHAR2(2)
) READ WRITE
PARTITION BY RANGE (order_date)
SUBPARTITION BY LIST (state)
( PARTITION order_p1 VALUES LESS THAN (TO_DATE ('01-DEC-2015','DD-MON-YYYY')) READ ONLY
( SUBPARTITION order_p1_northwest VALUES ('OR', 'WA'),
SUBPARTITION order_p1_southwest VALUES ('AZ', 'UT', 'NM') ),
PARTITION order_p2 VALUES LESS THAN (TO_DATE ('01-MAR-2016','DD-MON-YYYY'))
( SUBPARTITION order_p2_northwest VALUES ('OR', 'WA'),
SUBPARTITION order_p2_southwest VALUES ('AZ', 'UT', 'NM') READ ONLY),
PARTITION order_p3 VALUES LESS THAN (TO_DATE ('01-JUL-2016','DD-MON-YYYY'))
( SUBPARTITION order_p3_northwest VALUES ('OR', 'WA') READ ONLY,
SUBPARTITION order_p3_southwest VALUES ('AZ', 'UT', 'NM') ) );
SELECT PARTITION_NAME, READ_ONLY FROM USER_TAB_PARTITIONS WHERE TABLE_NAME ='ORDERS_READ_WRITE_ONLY';
SELECT PARTITION_NAME, SUBPARTITION_NAME, READ_ONLY FROM USER_TAB_SUBPARTITIONS WHERE TABLE_NAME ='ORDERS_READ_WRITE_ONLY';
了解了Oracle这些新的变化,将有助于我们深入和灵活的去使用Oracle数据库。
文章转自数据和云公众号,原文链接