一。基本增删改语句
alter table table_a add (dept_name varchar2(6));
alter table table_a modify (dept_name varchar2(60));
alter table table_a drop (dept_name);
comment on column table_a.dept_name is '部门名称';
二。oracle中修改table_a.dept_no字段类型number为varchar
--1.新增一个字段dept_no2
alter table table_a add (dept_no2 varchar2(60));
--2.将数据从dept_no赋值到dept_no2上
update table_a set dept_no2 = dept_no where id >0 and SOURCE_ID is not null
update table_a set dept_no = null where id > 0
--3.修改dept_no类型从number到varchar
alter table table_a modify (dept_no varchar2(60));
comment on column table_a.dept_no is '部门名称';
--4.将数据从dept_no2赋值到dept_no上
update table_a set dept_no = dept_no2 where id >0 and dept_no2 is not null
--5.删除dept_no2
alter table table_a drop (dept_no2);
三。恢复删除的数据,分为两种方法:scn和时间戳两种方法恢复。
3.1.通过scn恢复删除且已提交的数据
--1.获得当前数据库的scn号
select current_scn from v$database; --(切换到sys用户或system用户查询)
--查询到的scn号为:1499223
--2.查询当前scn号之前的scn
select * from 表名 as of scn 1499220; (确定删除的数据是否存在,如果存在,则恢复数据;如果不是,则继续缩小scn号)
select * from 表名 as of scn 1499220 where 还可以加条件;
--3、恢复删除且已提交的数据
flashback table 表名 to scn 1499220;
3.2、通过时间恢复删除且已提交的数据,闪回
--1.查询oracle中最近执行的语句。找到误删数据的时间(sys用户登录)
select sql_text,last_load_time from v$sql
where sql_text like '%update%' order by last_load_time desc;
--2.切换到删除库中
begin;
alter table 表名 enable row movement;--开启闪回
flashback table 表名 to timestamp to_timestamp('2018-06-08/18:20:25','yyyy-mm-dd hh24:mi:ss');
alter table 表名 disable row movement;--关闭闪回
commit;