Oracle 自动根据年份动态创建范围分区 踩坑填平

知识点

关于分区的各种策略就不说了,直接说关键的INTERVAL (numtoyminterval(1, ‘year’)),本篇是25年以及之后开始自动创建分区。

对于根据年份自动创建分区,您可以使用INTERVAL (numtoyminterval(1, ‘year’))。这是一种在Oracle数据库中动态创建范围分区的方法。
使用INTERVAL关键字和numtoyminterval函数,您可以定义一个自动增长的分区间隔,即每个分区的范围都是前一个分区末尾值加上指定的间隔值。

sql代码

踩坑版

CREATE TABLE "模式名"."表名" (
  "GMSFHMMD5" VARCHAR2(32 BYTE) NOT NULL ,
  "XM" VARCHAR2(64 BYTE) ,
  "MZDM" VARCHAR2(8 BYTE) ,
  "QFJG_GAJGMC" VARCHAR2(100 BYTE) ,
  "RKSJ" DATE ,
  "GXSJ" VARCHAR2(14 BYTE) ,
  "SFYZJ" CHAR(1 BYTE) ,
  "ROWNUM" NUMBER(20) NOT NULL ,
  "YXQQSRQ" DATE ,
  "YXQJZRQ" VARCHAR2(8 BYTE) ,
  "XBDM" VARCHAR2(8 BYTE)
)
PARTITION BY RANGE ( EXTRACT(YEAR FROM RKSJ) ) 
INTERVAL (NUMTOYMINTERVAL(1, 'year'))(
  PARTITION JBXX_RKSJ_1996 VALUES LESS THAN (TO_DATE('1997','YYYY')),
  PARTITION JBXX_RKSJ_2004 VALUES LESS THAN (TO_DATE('2005','YYYY')),
  PARTITION JBXX_RKSJ_2007 VALUES LESS THAN (TO_DATE('2008','YYYY')),
  PARTITION JBXX_RKSJ_2011 VALUES LESS THAN (TO_DATE('2012','YYYY')),
  PARTITION JBXX_RKSJ_2015 VALUES LESS THAN (TO_DATE('2016','YYYY')),
  PARTITION JBXX_RKSJ_2021 VALUES LESS THAN (TO_DATE('2022','YYYY')),
  PARTITION JBXX_RKSJ_2022 VALUES LESS THAN (TO_DATE('2023','YYYY')),
  PARTITION JBXX_RKSJ_2023 VALUES LESS THAN (TO_DATE('2024','YYYY')),
  PARTITION JBXX_RKSJ_AUTO VALUES LESS THAN (TO_DATE('2025','YYYY'))
);

该段代码会直接报 ORA-00907: 缺失右括号,Char AI 也找不到原因

修正版

CREATE TABLE "模式名"."表名" (
  "GMSFHMMD5" VARCHAR2(32 BYTE) NOT NULL ,
  "XM" VARCHAR2(64 BYTE) ,
  "MZDM" VARCHAR2(8 BYTE) ,
  "QFJG_GAJGMC" VARCHAR2(100 BYTE) ,
  "RKSJ" DATE ,
  "GXSJ" VARCHAR2(14 BYTE) ,
  "SFYZJ" CHAR(1 BYTE) ,
  "ROWNUM" NUMBER(20) NOT NULL ,
  "YXQQSRQ" DATE ,
  "YXQJZRQ" VARCHAR2(8 BYTE) ,
  "XBDM" VARCHAR2(8 BYTE)
)
PARTITION BY RANGE ( RKSJ ) 
INTERVAL (NUMTOYMINTERVAL(1, 'year'))(
  PARTITION JBXX_RKSJ_1996 VALUES LESS THAN (TO_DATE('1997-01-01','YYYY-MM-DD')),
  PARTITION JBXX_RKSJ_2004 VALUES LESS THAN (TO_DATE('2005-01-01','YYYY-MM-DD')),
  PARTITION JBXX_RKSJ_2007 VALUES LESS THAN (TO_DATE('2008-01-01','YYYY-MM-DD')),
  PARTITION JBXX_RKSJ_2011 VALUES LESS THAN (TO_DATE('2012-01-01','YYYY-MM-DD')),
  PARTITION JBXX_RKSJ_2015 VALUES LESS THAN (TO_DATE('2016-01-01','YYYY-MM-DD')),
  PARTITION JBXX_RKSJ_2021 VALUES LESS THAN (TO_DATE('2022-01-01','YYYY-MM-DD')),
  PARTITION JBXX_RKSJ_2022 VALUES LESS THAN (TO_DATE('2023-01-01','YYYY-MM-DD')),
  PARTITION JBXX_RKSJ_2023 VALUES LESS THAN (TO_DATE('2024-01-01','YYYY-MM-DD')),
  PARTITION JBXX_RKSJ_AUTO VALUES LESS THAN (TO_DATE('2025-01-01','YYYY-MM-DD'))
);

点个赞吧!

你可能感兴趣的:(oracle,数据库,分区)