【YashanDB数据库】Yashandb表闪回业务表实践
数据误删除
DELETE 操作闪回
示例(HEAP表)
基于闪回查询(建议):
select * from sales.branches1;
BRANCH_NO BRANCH_NAME AREA_NO ADDRESS EMPLOYEE_COUNT
--------- ---------------------------------------------------------------- ------- ---------------------------------------------------------------- --------------
0001 深圳 40
0101 上海 01 上海市静安区
0102 南京 01 City of Nanjing 70
0103 福州 01
0104 厦门 01 Xiamen
0401 北京 04
0402 天津 04 10
0403 大连 04 大连市 30
0404 沈阳 04
0201 成都 02
0501 武汉 30
0405 贵州 04 Guizhou
12 rows fetched.
delete sales.branches1 where BRANCH_NO = '0405' and BRANCH_NAME ='贵州';
commit;
SQL> SQL> select systimestamp from dual;
SYSTIMESTAMP
----------------------------------------------------------------
2024-05-11 14:03:11.193397
1 row fetched.
insert into sales.branches1 values('0406','江西',4,'JiangXi',50);
commit;
SQL> select systimestamp from dual;
SYSTIMESTAMP
----------------------------------------------------------------
2024-05-11 14:04:46.152775
SQL> select * from sales.branches1 as of TIMESTAMP TIMESTAMP('2024-05-11 14:03:00.00000');
BRANCH_NO BRANCH_NAME AREA_NO ADDRESS EMPLOYEE_COUNT
--------- ---------------------------------------------------------------- ------- ---------------------------------------------------------------- --------------
0001 深圳 40
0101 上海 01 上海市静安区
0102 南京 01 City of Nanjing 70
0103 福州 01
0104 厦门 01 Xiamen
0401 北京 04
0402 天津 04 10
0403 大连 04 大连市 30
0404 沈阳 04
0201 成都 02
0501 武汉 30
0405 贵州 04 Guizhou
12 rows fetched.
create table sales.branches1_1403 as select * from sales.branches1 as of TIMESTAMP TIMESTAMP('2024-05-11 14:03:00.00000');
Succeed.
SQL> select * from sales.branches1_1403;
BRANCH_NO BRANCH_NAME AREA_NO ADDRESS EMPLOYEE_COUNT
--------- ---------------------------------------------------------------- ------- ---------------------------------------------------------------- --------------
0001 深圳 40
0101 上海 01 上海市静安区
0102 南京 01 City of Nanjing 70
0103 福州 01
0104 厦门 01 Xiamen
0401 北京 04
0402 天津 04 10
0403 大连 04 大连市 30
0404 沈阳 04
0201 成都 02
0501 武汉 30
0405 贵州 04 Guizhou
12 rows fetched.
create table sales.branches1_1405 as select * from sales.branches1 as of TIMESTAMP TIMESTAMP('2024-05-11 14:05:00.00000');
SQL> select * from sales.branches1_1405 ;
BRANCH_NO BRANCH_NAME AREA_NO ADDRESS EMPLOYEE_COUNT
--------- ---------------------------------------------------------------- ------- ---------------------------------------------------------------- --------------
0001 深圳 40
0101 上海 01 上海市静安区
0102 南京 01 City of Nanjing 70
0103 福州 01
0104 厦门 01 Xiamen
0401 北京 04
0402 天津 04 10
0403 大连 04 大连市 30
0404 沈阳 04
0201 成都 02
0501 武汉 30
0406 江西 4 JiangXi 50
12 rows fetched.
--将需要的数据整合到业务表中
create table sales.branches_bak as select * from sales.branches1_1403;
insert into sales.branches_bak select * from sales.branches1_1405 where BRANCH_NO ='0406';
SQL> ALTER TABLE SALES.BRANCHES RENAME TO BRANCHES_ORG;
Succeed.
SQL> ALTER TABLE SALES.BRANCHES_BAK RENAME TO BRANCHES;
Succeed.
SQL> SELECT * FROM SALES.BRANCHES;
BRANCH_NO BRANCH_NAME AREA_NO ADDRESS EMPLOYEE_COUNT
--------- ---------------------------------------------------------------- ------- ---------------------------------------------------------------- --------------
0001 深圳 40
0101 上海 01 上海市静安区
0102 南京 01 City of Nanjing 70
0103 福州 01
0104 厦门 01 Xiamen
0401 北京 04
0402 天津 04 10
0403 大连 04 大连市 30
0404 沈阳 04
0201 成都 02
0501 武汉 30
0405 贵州 04 Guizhou
0406 江西 4 JiangXi 50
13 rows fetched.
基于闪回+闪回查询
SQL> SELECT * FROM SALES.BRANCHES;
BRANCH_NO BRANCH_NAME AREA_NO ADDRESS EMPLOYEE_COUNT
--------- ---------------------------------------------------------------- ------- ---------------------------------------------------------------- --------------
0001 深圳 40
0101 上海 01 上海市静安区
0102 南京 01 City of Nanjing 70
0103 福州 01
0104 厦门 01 Xiamen
0401 北京 04
0402 天津 04 10
0403 大连 04 大连市 30
0404 沈阳 04
0201 成都 02
0501 武汉 30
0405 贵州 04 Guizhou
12 rows fetched.
delete sales.branches where BRANCH_NO = '0405' and BRANCH_NAME ='贵州';
commit;
delete sales.branches where BRANCH_NO = '0405' and BRANCH_NAME ='贵州';
commit;
SQL>
1 row affected.
SQL>
Succeed.
SQL>
SQL> SELECT * FROM SALES.BRANCHES;
BRANCH_NO BRANCH_NAME AREA_NO ADDRESS EMPLOYEE_COUNT
--------- ---------------------------------------------------------------- ------- ---------------------------------------------------------------- --------------
0001 深圳 40
0101 上海 01 上海市静安区
0102 南京 01 City of Nanjing 70
0103 福州 01
0104 厦门 01 Xiamen
0401 北京 04
0402 天津 04 10
0403 大连 04 大连市 30
0404 沈阳 04
0201 成都 02
0501 武汉 30
11 rows fetched.
SQL> select systimestamp from dual;
SYSTIMESTAMP
----------------------------------------------------------------
2024-05-11 14:23:32.963164
1 row fetched.
insert into sales.branches values('0406','江西',4,'JiangXi',50);
select * from sales.branches ;
BRANCH_NO BRANCH_NAME AREA_NO ADDRESS EMPLOYEE_COUNT
--------- ---------------------------------------------------------------- ------- ---------------------------------------------------------------- --------------
0001 深圳 40
0101 上海 01 上海市静安区
0102 南京 01 City of Nanjing 70
0103 福州 01
0104 厦门 01 Xiamen
0401 北京 04
0402 天津 04 10
0403 大连 04 大连市 30
0404 沈阳 04
0201 成都 02
0501 武汉 30
0406 江西 4 JiangXi 50
12 rows fetched.
SQL> select * from sales.branches as of TIMESTAMP TIMESTAMP('2024-05-11 14:22:32.00000');
BRANCH_NO BRANCH_NAME AREA_NO ADDRESS EMPLOYEE_COUNT
--------- ---------------------------------------------------------------- ------- ---------------------------------------------------------------- --------------
0001 深圳 40
0101 上海 01 上海市静安区
0102 南京 01 City of Nanjing 70
0103 福州 01
0104 厦门 01 Xiamen
0401 北京 04
0402 天津 04 10
0403 大连 04 大连市 30
0404 沈阳 04
0201 成都 02
0501 武汉 30
0405 贵州 04 Guizhou
12 rows fetched.
FLASHBACK TABLE sales.branches TO TIMESTAMP TIMESTAMP('2024-05-11 14:22:32.00000');
CREATE TABLE SALES.branches_1430 as select * from sales.branches as of TIMESTAMP TIMESTAMP('2024-05-11 14:30:11.00000');
SQL> insert into sales.branches select * from SALES.branches_1430 where branch_no='0406';
1 row affected.
SQL> commit;
Succeed.
SQL> select * from sales.branches;
BRANCH_NO BRANCH_NAME AREA_NO ADDRESS EMPLOYEE_COUNT
--------- ---------------------------------------------------------------- ------- ---------------------------------------------------------------- --------------
0001 深圳 40
0101 上海 01 上海市静安区
0102 南京 01 City of Nanjing 70
0103 福州 01
0104 厦门 01 Xiamen
0401 北京 04
0402 天津 04 10
0403 大连 04 大连市 30
0404 沈阳 04
0201 成都 02
0501 武汉 30
0406 江西 4 JiangXi 50
0405 贵州 04 Guizhou
13 rows fetched.
DROP 操作闪回
基于闪回查询:
ALTER SYSTEM SET RECYCLEBIN_ENABLED=ON;DROP TABLE finance_info;DROP TABLE orders_info;DROP TABLE sales_info;DROP TABLE employees;-- 1.查询回收站是否存在finance_info表SELECT original_name,object_name FROM DBA_RECYCLEBIN
WHERE original_name IN ('FINANCE_INFO','ORDERS_INFO','SALES_INFO','EMPLOYEES');
ORIGINAL_NAME OBJECT_NAME
----------------------------- -----------------------
SALES_INFO BIN$2393
ORDERS_INFO BIN$2389
EMPLOYEES BIN$2385
FINANCE_INFO BIN$2408-- 2.表结构及表数据闪回
FLASHBACK TABLE "BIN$2393" TO BEFORE DROP;-- 或使用表的原始名称进行闪回
FLASHBACK TABLE finance_info TO BEFORE DROP;-- 或通过RENAME TO命令指定表的新名称
FLASHBACK TABLE employees TO BEFORE DROP RENAME TO employees_recycle;-- 3.验证相关对象是否保留系统生成的回收站名称,若名称未恢复,请使用ALTER INDEX语句手动修改SELECT INDEX_NAME
FROM USER_INDEXES
WHERE TABLE_NAME = 'FINANCE_INFO';
INDEX_NAME
---------------------------------
IDX_FINANCE_INFO_1
TRUNCATE 操作闪回
ALTER SYSTEM SET RECYCLEBIN_ENABLED=ON;TRUNCATE TABLE product;-- 1.查询回收站是否存在product表SELECT original_name,object_name FROM DBA_RECYCLEBIN WHERE original_name = 'PRODUCT';
ORIGINAL_NAME OBJECT_NAME
----------------------------- -----------------------
PRODUCT PRODUCT
-- 2.表数据闪回
FLASHBACK TABLE product TO BEFORE TRUNCATE;-- 3.验证相关对象是否保留系统生成的回收站名称,若名称未恢复,请使用ALTER INDEX语句手动修改SELECT INDEX_NAME
FROM USER_INDEXES
WHERE TABLE_NAME = 'PRODUCT';
INDEX_NAME
--------------------------
SYS_C_133