--以TABLE_NAME内容创建分区表TABLE_NAME1并将数据写入
CREATE TABLE TABLE_NAME1 PARTITION BY RANGE(CREATEDATE) ( PARTITION TABLE_NAME_P1 VALUES LESS THAN (TO_DATE('2018-1-1 00:00:00', 'yyyy-mm-dd hh24:mi:ss')), PARTITION TABLE_NAME_P2 VALUES LESS THAN (TO_DATE('2019-1-1 00:00:00', 'yyyy-mm-dd hh24:mi:ss')), PARTITION TABLE_NAME_P3 VALUES LESS THAN (TO_DATE('2020-1-1 00:00:00', 'yyyy-mm-dd hh24:mi:ss')), PARTITION TABLE_NAME_P4 VALUES LESS THAN (TO_DATE('2021-1-1 00:00:00', 'yyyy-mm-dd hh24:mi:ss')), PARTITION TABLE_NAME_P5 values less than(maxvalue)) AS SELECT * FROM TABLE_NAME;
--将原表名备份
alter table TABLE_NAME rename to TABLE_NAME_OLD;
--将分区表名改为原表名
alter table TABLE_NAME1 rename to TABLE_NAME;
--查看两表数据是否一致
(SELECT COUNT(*) NUM,'TABLE_NAME' TABLENAME FROM TABLE_NAME T) UNION ALL (SELECT COUNT(*) NUM,'TABLE_NAME_OLD' TABLENAME FROM TABLE_NAME_OLD T);
--刪除备份表中索引(避免新建冲突)
SELECT * FROM USER_INDEXES WHERE TABLE_NAME='TABLE_NAME_OLD'; DROP INDEX INDEX_NAME;
--以PLSQL查看并添加注释及索引(选择table,右键view,右下角View SQL)
comment on table TABLE_NAME is '部门信息表'; comment on column TABLE_NAME.stitic_month is '统计月份'; comment on column TABLE_NAME.cus_no is '部门代码';
--添加索引
create index index_name on TABLE_NAME (column_name);
--删除备份表
drop table TABLE_NAME_OLD;
--查看表的分区
SELECT TABLE_NAME,PARTITION_NAME FROM USER_TAB_PARTITIONS WHERE TABLE_NAME='TABLE_NAME';
--查看某一分区的全部记录
SELECT * FROM TABLE_NAME PARTITION(PARTITION_NAME);
ALTER TABLE TABLE_NAME TRUNCATE PARTITION PARTITION_NAME UPDATE GLOBAL INDEXES; --truncate某一分區 ALTER TABLE TABLE_NAME DROP PARTITION PARTITION_NAME UPDATE GLOBAL INDEXES; --删除表中某一分区(会同时删除分区里的数据)