--①系统变更号
--获取数据时间插入时间 ora_rowscn返回行最近一次修改的scn.
select scn_to_timestamp(ora_rowscn),tt.* from tt;
--scn与timestamp转换
select timestamp_to_scn(sysdate) from dual ;
select to_char(scn_to_timestamp(timestamp_to_scn(sysdate)),'yyyy-mm-dd hh24:mi:ss') from dual ;
--②wm_concat 与listagg
/*wm_concat就存在10g 11g 之前用自定义函数 strcat
11g 及之后存在listagg
wm_concat 可以去重 listagg 可以排序*/
select tt.id,
wm_concat(distinct tt.name) name1,
listagg(tt.name, ';') within group(order by name) name2
from tt
group by tt.id;
--③计算表达式
select dbms_aw.eval_number('20*20') from dual ;
例子计算1-10 合为10的组合 跟connect_by_path() 结合使用
with t as
(select 1 a from dual union all
select 2 a from dual union all
select 3 a from dual union all
select 4 a from dual union all
select 5 a from dual union all
select 6 a from dual union all
select 7 a from dual union all
select 8 a from dual union all
select 9 a from dual),
t1 as (
select t.*,
dbms_aw.eval_number(substr(sys_connect_by_path(a, '+'), 2)) dd,
substr(sys_connect_by_path(a, '+'), 2) cc
from t
connect by nocycle a > prior a
and level <=4
)
select * from t1 where dd =10
;
--④ 迁移数据是 禁用/恢复 约束
ALTER TABLE test_phone_tab disable constraint test_phone_pk;
ALTER TABLE test_phone_tab enable constraint test_phone_pk;
--⑤分区可以有默认分区 (自己思维定式 未仔细想过)
列表可存多个值
create table t_partition_list (id number,name varchar2(50))
partition by list(id)
( partition t_list_p1 values (1,2,3,4,5,6,7,8,9) , --可以多个值
partition t_list_p2 values (10,11,12,13,14,15,16,17,18,19) ,
partition t_list_p3 values (20,21,22,23,24,25,26,27,28,29),
partition t_list_pd values (default) ); --可以有默认分区 需要制定
/*****范围分区 < maxvalue 默认分区 ***/
/*****哈希分区 本来就是随机分配分区 ***/
insert into t_partition_list
select level+15,'name'||level
from dual connect by level <26
select * from t_partition_list partition (t_list_p2) ;
select * from t_partition_list partition (t_list_p3) ;
select * from t_partition_list partition (t_list_pd);
--oracle 子分区模板 subpartition template add at 2016.08.25
create table csdn_sub_part
(
id number,
grade varchar2(10),
sal number
)
partition by list (grade) subpartition by range (sal)
subpartition template --此处关键字
(
subpartition subpart_20 values less than (20) ,
subpartition subpart_40 values less than (40) ,
subpartition subpart_60 values less than (60) ,
subpartition subpart_80 values less than (80) ,
subpartition subpart_100 values less than (100) ,
subpartition subpart_other values less than (maxvalue) --range 默认分区
)
(
partition p_01 values ('A','B'),
partition p_02 values ('C','D','E'),
partition p_03 values (default) --list 默认分区
)
--增加住分区 利用子分区模板
alter table csdn_sub_part add partition p_04 values ('F');
------------- ora-14233:在default分区存在时无法添加分区---
/*
先删除default分区 (备份)
创建新的子分区 默认分区
重新insert进表 分区存储
*/
create table zz_dl as
select * from csdn_sub_part where 1=0;
alter table csdn_sub_part exchange partition p_03 with table zz_dl;
--丫的组合分区 无法exchange
insert into zz_dl
select * from csdn_sub_part partition (p_03);
alter table csdn_sub_part drop partition p_03;
alter table csdn_sub_part add partition p_03 values ('F');
--ora-14020:不能指定表分区的物理属性 values 写成 value
alter table csdn_sub_part add partition p_default values (default);
insert into csdn_sub_part
select * from zz_dl;
commit;
--⑥JOB执行多个任务
what => 'sp_emp_01;sp_emp_02;'
或者(貌似一个意思)
what => '
begin
sp_emp_01;
sp_emp_02;
end;'
⑦ sqlplus c 和 edit 这两个功能
c/t/t1 记录上次执行的命令 具体 c/t/t1啥意思位置
edit 编辑上次执行的命令 修改后可 c/t/t1查询
⑧、子查询 结果显示为cursor
select d.deptno,
d.dname,
cursor (select e.empno, e.ename, e.sal
from emp e
where d.deptno = e.deptno) as details
from dept d
⑨、普通表转变成分区表
① 创建一个新的分区表 insert into 分区表
② 穿件一个新的分区表 创建分区对应的几个基础表 exchange 语句
③ 在线重新定义 DBMS_REDEFINITION
--在线重新定义(普通表边分区表)
-- ① 创建一个新的分区表 insert into 分区表
-- ② 穿件一个新的分区表 创建分区对应的几个基础表 exchange 语句
-- ③ 在线重新定义 DBMS_REDEFINITION
--① 创建一个新的分区表 insert into 分区表
create table comm_table
(id number,
name varchar2(20),
data_date varchar2(8));
select * from comm_table;
insert /*+append*/
into comm_table nologging
select level, level || 'name', '20161201'
from dual
connect by level <= 100000;
commit;
select * from comm_table;
create table part_table
(id number,
name varchar2(20),
data_date varchar2(8))
partition by list (data_date)
(partition part_20161201 values ('20161201') tablespace users,
partition part_20161202 values ('20161202') tablespace users,
partition part_20161203 values ('20161203') tablespace users
)
;
insert/*+append*/ into part_table nologging
select /*+parallel(a,4)*/ * from comm_table a;
select * from part_table partition(part_20161203);
--② 穿件一个新的分区表 创建分区对应的几个基础表 exchange 语句
create table part_table2
(id number,
name varchar2(20),
data_date varchar2(8))
partition by list (data_date)
(partition part_20161201 values ('20161201') tablespace users,
partition part_20161202 values ('20161202') tablespace users,
partition part_20161203 values ('20161203') tablespace users
);
alter table part_table2 exchange partition part_20161201 with table comm_table;
--ora-14099 未指定分区限定表中的所有行
create table comm_table_1201
as
select /*+parallel(a,4)*/ * from comm_table a where a.data_date='20161201';
create table comm_table_1202
as
select /*+parallel(a,4)*/ * from comm_table a where a.data_date='20161202';
create table comm_table_1203
as
select /*+parallel(a,4)*/ * from comm_table a where a.data_date='20161203';
select * from comm_table_1202;
alter table part_table2 exchange partition part_20161201 with table comm_table_1201;
alter table part_table2 exchange partition part_20161202 with table comm_table_1202;
alter table part_table2 exchange partition part_20161203 with table comm_table_1203;
select * from part_table2;
select * from comm_table_1201;
--③ 在线重新定义 DBMS_REDEFINITION
--源表
create table demo as select empno,ename,sal,deptno from scott.emp;
--没主键不行啊
alter table demo add constraint demo_pk primary key(empno);
create index demo_idx on demo(ename);
--检测重定义合理性
begin
DBMS_REDEFINITION.CAN_REDEF_TABLE('logminer', 'demo');
end;
--中间表
create table demo_tmp
partition by range(deptno)
(
partition p1 values less than (11),
partition p2 values less than (21),
partition p3 values less than (31)
)
as
select * from demo where 1=2;
--执行在线重定义
BEGIN
DBMS_REDEFINITION.START_REDEF_TABLE('LOGMINER', 'DEMO', 'DEMO_TMP');
END;
select object_id,object_name,object_type,status from user_objects;
我们注意到Oracle新建了两张表RUPD$_DEMO和MLOG$_DEMO,
其实Oracle在线重定义是通过物化视图的LOG来实现的。做完这一步后,在中间表中也有了相同的数据
select * from DEMO;
select * from DEMO_TMP;
--结束在线重定义
begin
DBMS_REDEFINITION.FINISH_REDEF_TABLE('LOGMINER', 'DEMO', 'DEMO_TMP');
end;
select * from DEMO;
--成功
/**************结语************/
/*
需要创建主键,(索引可无),创建中间表,实用性不太好,记住这个功能
*/
⑩、pl/sql的data genertor
选择 用户,表之后,在data中选择想要的数据,生成随机数不错
[图片]
⑩①、归档日志满了
sqlplus /nolog
connect /as sysdba
select * from V$FLASH_RECOVERY_AREA_USAGE;
select sum(percent_space_used)*3/100 from v$flash_recovery_area_usage;
ALTER SYSTEM SET DB_RECOVERY_FILE_DEST_SIZE=8g;
show parameter recover;
rman
connect target sys/sys_passwd
DELETE ARCHIVELOG ALL COMPLETED BEFORE 'SYSDATE-7';