subpartition_ALTER TABLE…TRUNCATE SUBPARTITION

本文档展示了如何在Oracle数据库中删除分区表的子分区内容。通过创建一个名为sales的分区表,并填充数据,然后使用ALTER TABLE TRUNCATE SUBPARTITION命令清空特定子分区'americas_2012',以此说明分区表的管理操作。在执行清除操作后,查询结果显示子分区的内容已删除,但其结构仍然保留。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

示例 – 清空子分区

下列示例从表sales的子分区中删除了数据。使用下列命令来创建表sales:

CREATE TABLE sales

(

dept_no number,

part_no varchar2,

country varchar2(20),

date date,

amount number

)

PARTITION BY RANGE(date) SUBPARTITION BY LIST (country)

(

PARTITION "2011" VALUES LESS THAN('01-JAN-2012')

(

SUBPARTITION europe_2011 VALUES ('ITALY', 'FRANCE'),

SUBPARTITION asia_2011 VALUES ('PAKISTAN', 'INDIA'),

SUBPARTITION americas_2011 VALUES ('US', 'CANADA')

),

PARTITION "2012" VALUES LESS THAN('01-JAN-2013')

(

SUBPARTITION europe_2012 VALUES ('ITALY', 'FRANCE'),

SUBPARTITION asia_2012 VALUES ('PAKISTAN', 'INDIA'),

SUBPARTITION americas_2012 VALUES ('US', 'CANADA')

),

PARTITION "2013" VALUES LESS THAN('01-JAN-2015')

(

SUBPARTITION europe_2013 VALUES ('ITALY', 'FRANCE'),

SUBPARTITION asia_2013 VALUES ('PAKISTAN', 'INDIA'),

SUBPARTITION americas_2013 VALUES ('US', 'CANADA')

)

);

使用下列命令填充表sales:

INSERT INTO sales VALUES

(10, '4519b', 'FRANCE', '17-Jan-2011', '45000'),

(20, '3788a', 'INDIA', '01-Mar-2012', '75000'),

(40, '9519b', 'US', '12-Apr-2012', '145000'),

(20, '3788a', 'PAKISTAN', '04-Jun-2012', '37500'),

(40, '4577b', 'US', '11-Nov-2012', '25000'),

(30, '7588b', 'CANADA', '14-Dec-2011', '50000'),

(30, '4519b', 'CANADA', '08-Apr-2012', '120000'),

(40, '3788a', 'US', '12-May-2011', '4950'),

(20, '3788a', 'US', '04-Apr-2012', '37500'),

(40, '4577b', 'INDIA', '11-Jun-2011', '25000'),

(10, '9519b', 'ITALY', '07-Jul-2012', '15000'),

(20, '4519b', 'INDIA', '2-Dec-2012', '5090');

查询表sales显示了记录已被分配在子分区之中:

acctg=# SELECT tableoid::regclass, * FROM sales;

tableoid | dept_no| part_no| country | date |amount

--------------------+--------+--------+----------+-------------------+-------

sales_2011_europe | 10| 4519b | FRANCE | 17-JAN-11 00:00:00| 45000

sales_2011_asia | 40| 4577b | INDIA | 11-JUN-11 00:00:00| 25000

sales_2011_americas| 30| 7588b | CANADA | 14-DEC-11 00:00:00| 50000

sales_2011_americas| 40| 3788a | US | 12-MAY-11 00:00:00| 4950

sales_2012_europe | 10| 9519b | ITALY | 07-JUL-12 00:00:00| 15000

sales_2012_asia | 20| 3788a | INDIA | 01-MAR-12 00:00:00| 75000

sales_2012_asia | 20| 3788a | PAKISTAN | 04-JUN-12 00:00:00| 37500

sales_2012_asia | 20| 4519b | INDIA | 02-DEC-12 00:00:00| 5090

sales_2012_americas| 40| 9519b | US | 12-APR-12 00:00:00| 145000

sales_2012_americas| 40| 4577b | US | 11-NOV-12 00:00:00| 25000

sales_2012_americas| 30| 4519b | CANADA | 08-APR-12 00:00:00| 120000

sales_2012_americas| 20| 3788a | US | 04-APR-12 00:00:00| 37500

(12 rows)

要删除分区2012_americas的内容,先要调用下列命令:

ALTER TABLE sales TRUNCATE SUBPARTITION "americas_2 012";

现在,查询表sales显示了分区americas_2012的内容已被删除:

acctg=# SELECT tableoid::regclass, * FROM sales;

tableoid | dept_no|part_no| country | date | amount

--------------------+--------+-------+----------+--------------------+-------

sales_2011_europe | 10| 4519b | FRANCE | 17-JAN-11 00:00:00 | 45000

sales_2011_asia | 40| 4577b | INDIA | 11-JUN-11 00:00:00 | 25000

sales_2011_americas| 30| 7588b | CANADA | 14-DEC-11 00:00:00 | 50000

sales_2011_americas| 40| 3788a | US | 12-MAY-11 00:00:00 | 4950

sales_2012_europe | 10| 9519b | ITALY | 07-JUL-12 00:00:00 | 15000

sales_2012_asia | 20| 3788a | INDIA | 01-MAR-12 00:00:00 | 75000

sales_2012_asia | 20| 3788a | PAKISTAN | 04-JUN-12 00:00:00 | 37500

sales_2012_asia | 20| 4519b | INDIA | 02-DEC-12 00:00:00 | 5090

(8 rows)

当删除记录时,分区2012_americas的结构仍然是完整的:

acctg=# SELECT subpartition_name, high_value FROM ALL_TAB_SUBPARTITIONS;

subpartition_name | high_value

-------------------+---------------------

2013_europe | 'ITALY', 'FRANCE'

2012_europe | 'ITALY', 'FRANCE'

2011_europe | 'ITALY', 'FRANCE'

2013_asia | 'PAKISTAN', 'INDIA'

2012_asia | 'PAKISTAN', 'INDIA'

2011_asia | 'PAKISTAN', 'INDIA'

2013_americas | 'US', 'CANADA'

2012_americas | 'US', 'CANADA'

2011_americas | 'US', 'CANADA'

(9

rows)

Oracle subpartition template is a feature that allows you to create subpartitions with a specific pattern or template. This can be useful when you have a large number of subpartitions to create, and you want to automate the process. To use subpartition template, you need to first create the partitioned table with the desired partitioning scheme. Then, you can use the ALTER TABLE statement with the ADD SUBPARTITION clause to create subpartitions using the template. Here is an example: ``` CREATE TABLE sales ( sale_id NUMBER, sale_date DATE, amount NUMBER ) PARTITION BY RANGE (sale_date) SUBPARTITION BY HASH (sale_id) SUBPARTITIONS 4 ( PARTITION sales_q1 VALUES LESS THAN (TO_DATE('01-APR-2020', 'DD-MON-YYYY')), PARTITION sales_q2 VALUES LESS THAN (TO_DATE('01-JUL-2020', 'DD-MON-YYYY')), PARTITION sales_q3 VALUES LESS THAN (TO_DATE('01-OCT-2020', 'DD-MON-YYYY')), PARTITION sales_q4 VALUES LESS THAN (MAXVALUE) ); ALTER TABLE sales ADD SUBPARTITION sales_q1_01 TEMPLATE ( SUBPARTITION sales_q1_01 VALUES LESS THAN (TO_DATE('02-APR-2020', 'DD-MON-YYYY')) ); ``` In this example, we first create a partitioned table with quarterly partitions and 4 hash subpartitions per partition. Then, we use the ALTER TABLE statement to add a subpartition to the "sales_q1" partition using the subpartition template. The template specifies that the subpartition should have a name of "sales_q1_01" and should store rows with a "sale_date" value less than April 2, 2020. This will create a subpartition for every quarter of the year with the same naming convention and range of values.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值