Oracle使用range分区并根据时间列自动创建分区
oracle使用range分区并根据时间列自动创建分区
oracle使用range分区,根据时间列自动创建分区
-- create table
create table my_test
(
id number (12) not null,
name varchar2 (12) not null,
today timestamp (6) default sysdate
)
partition by range (today) interval(numtodsinterval(1,'day')) --月分区用month,年分区用year
(
partition p_20230411 values less than (to_date('2023-04-12 00:00:00,"syyyy-mm-dd hh24:mi:ss"))
tablespace tb_sams
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 1m
next 1m
minextents 1
maxextents unlimited
)
);
- - add comments to the table
comment on table my_test
is ‘测试表';
add comments to the columns
comment on column my_test.id
is ‘主键id';
comment on column my_test.name
is ‘名称';
comment on column my_test.today
is ‘时间';
- - create/recreate indexes
create index my_test_index on my_test (id)
tablespace tb_sams
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64k
next 1m
minextents 1
maxextents unlimited
);
- -grant/revoke object privileges
grant select on my_test to dbview;
insert into my_test values (1,'xxc1',sysdate);
insert into my_test values (2,'xxc2'‚sysdate+1);
insert into my_test vaiues (3,'xxc3',sysdate+2) ;
测试效果

附录:oracle 根据日期自动生成分区表
oracle 根据日期自动生成分区表
create table my_table (
id number,
name varchar2(50),
created_date date
)
partition by range (created_date) interval (numtodsinterval(1, 'day'))
(
partition p1 values less than (to_date('2022-01-01', 'yyyy-mm-dd'))
);
其中:
numtodsinterval(,) ,x是一个数字,c是一个字符串,
表明x的单位,这个函数把x转为interval day to second数据类型
常用的单位有 (‘day’,‘hour’,‘minute’,‘second’)
numtoyminterval 与numtodsinterval函数类似,将x转为interval year to month数据类型
常用的单位有(‘year’,‘month’)
格式:numtoyminterval(n, interval_unit);
n: 数值类型
interval_unit: ‘year’, ‘month’ ,或其他可以转换成这两个值之一的表达式
numtoyminterval(1, ‘year’) :一年后的间隔
numtoyminterval(-1, ‘month’): 一个月前
小数会被计算成整数后,再做计算:
select sysdate + numtoyminterval(0.1, 'month') from dual; 2023-03-21 09:54:37
如果执行含有函数的sql时报错:”interval year to month literal“。不能与数值做运算。
常用用途:
做日期运算时,这个函数非常有用。例如:取一个月后的日期:
select sysdate + numtoyminterval(1, 'month') from dual;
关于oracle使用range分区并根据时间列自动创建分区的文章就介绍至此,更多相关oracle自动创建分区内容请搜索硕编程以前的文章,希望以后支持硕编程!
相关文章


