单位把程序做成了exe的安装包。
安装包在初始化数据库的时候,会先drop所有的表,然后建表,最后插入数据。
在开发用的数据库,一个同事加了一天班,给所有的表和字段都加了注释,估计是用于PD反向工程。
但是不凑巧,第二天另外一个同事运行安装包直接把他的表覆盖了...
大致过程如下,
同事A:
-
create table a(a int);
-
create table b(b int);
-
create table c(c int);
-
create table d(d int);
-
create table e(e int);
-
comment on column a.a is 'Test a';
-
comment on column b.b is 'Test b';
-
comment on column c.c is 'Test c';
-
comment on column d.d is 'Test d';
-
comment on column e.e is 'Test e';
第二天,同事B:
-
drop table a;
-
drop table b;
-
drop table c;
-
drop table d;
-
drop table e;
-
create table a(a int);
-
create table b(b int);
-
create table c(c int);
-
create table d(d int);
-
create table e(e int);
现在需要恢复同事A的注释。
当时解决的过程如下
1.查看回收站,发现表还在。
-
SQL> select object_name,original_name,createtime from recyclebin;
-
-
OBJECT_NAME ORIGINAL_NAME CREATETIME
-
------------------------------ -------------------------------- -------------------
-
BIN$NmbxAasiR2CPSv0of/GyzA==$0 A 2013-11-19:21:19:28
-
BIN$cyT3N5nJQMO85ho0DkpV9g==$0 B 2013-11-19:21:19:29
-
BIN$fdrhQ5VtS0q7pZ5kT9tceA==$0 C 2013-11-19:21:19:29
-
BIN$o8t+PJl3SpmRTh2gD1mUBQ==$0 D 2013-11-19:21:19:29
-
BIN$RhLkOWtCTo+ls8QSwMxD9g==$0 E 2013-11-19:21:19:29
2.使用闪回删除,恢复表。
在恢复的时候,将表加上前缀。
(之所以没有删除当前的表,再将回收站的表恢复,是因为怕新删除的表,直接将旧表挤出回收站)
-
SQL> select 'flashback table "'||object_name||'" to before drop rename to bak_'||original_name||';' from recyclebin;
-
-
'FLASHBACKTABLE"'||OBJECT_NAME||'"TOBEFOREDROPRENAMETOBAK_'||ORIGINAL_NAME||';'
-
---------------------------------------------------------------------------------------------------------------
-
flashback table "BIN$NmbxAasiR2CPSv0of/GyzA==$0" to before drop rename to bak_A;
-
flashback table "BIN$cyT3N5nJQMO85ho0DkpV9g==$0" to before drop rename to bak_B;
-
flashback table "BIN$fdrhQ5VtS0q7pZ5kT9tceA==$0" to before drop rename to bak_C;
-
flashback table "BIN$o8t+PJl3SpmRTh2gD1mUBQ==$0" to before drop rename to bak_D;
-
flashback table "BIN$RhLkOWtCTo+ls8QSwMxD9g==$0" to before drop rename to bak_E;
3.删除当前存在的表
-
SQL> select 'drop table '||substr(table_name,5)||' purge;' from user_tables where table_name like 'BAK%';
-
-
'DROPTABLE'||SUBSTR(TABLE_NAME,5)||'PURGE;'
-
----------------------------------------------------------------------
-
drop table A purge;
-
drop table B purge;
-
drop table C purge;
-
drop table D purge;
-
drop table E purge;
4.将恢复的表重命名
-
SQL> select 'alter table '|| table_name||' rename to '||substr(table_name,5)||';' from user_tables where table_name like 'BAK%';
-
-
'ALTERTABLE'||TABLE_NAME||'RENAMETO'||SUBSTR(TABLE_NAME,5)||';'
-
----------------------------------------------------------------------------------------------------------
-
alter table BAK_A rename to A;
-
alter table BAK_B rename to B;
-
alter table BAK_C rename to C;
-
alter table BAK_D rename to D;
-
alter table BAK_E rename to E;
运行alter table命令之后,查看注释,已经恢复。
-
SQL> select * from user_col_comments;
-
-
TABLE_NAME COLUMN_NAME COMMENTS
-
------------------------------ ------------------------------ ----------
-
A A Test a
-
B B Test b
-
C C Test c
-
D D Test d
-
E E Test e
后来发现还有一种更简单的方法。
表被Drop之后,放入回收站,他的table_name被改名,
而注释的table_name会和回收站的table_name保持一致。
-
SQL> select * from user_col_comments;
-
-
TABLE_NAME COLUMN_NAME COMMENTS
-
------------------------------ ------------------------------ ----------
-
BIN$sKho5ULVTNOOG2eLJIufhA==$0 A Test a
-
BIN$zxSn8/FpSZyllfe4evv3EQ==$0 B Test b
-
BIN$Fc/XryYETrqEMfv0aMxY1g==$0 C Test c
-
BIN$fVzUmlAeR72hOkHWH85z3g==$0 D Test d
-
BIN$r5cZoCgSSxO4DLf8xrem/A==$0 E Test e
-
-
SQL> select object_name,original_name,createtime from recyclebin;
-
-
OBJECT_NAME ORIGINAL_NAME CREATETIME
-
------------------------------ -------------------------------- -------------------
-
BIN$sKho5ULVTNOOG2eLJIufhA==$0 A 2013-11-19:21:19:28
-
BIN$zxSn8/FpSZyllfe4evv3EQ==$0 B 2013-11-19:21:19:29
-
BIN$Fc/XryYETrqEMfv0aMxY1g==$0 C 2013-11-19:21:19:29
-
BIN$fVzUmlAeR72hOkHWH85z3g==$0 D 2013-11-19:21:19:29
-
BIN$r5cZoCgSSxO4DLf8xrem/A==$0 E 2013-11-19:21:19:29
所以不用恢复回收站中的表,直接用回收站连接user_col_comments,生成注释信息即可。
-
SQL> select 'comment on column '||recyclebin.original_name||'.'||user_col_comments.column_name||' is '''||user_col_comments.comments||''';' create_comments
-
2 from recyclebin
-
3 inner join user_col_comments
-
4 on user_col_comments.table_name=recyclebin.object_name
-
5 where recyclebin.type='TABLE';
-
-
CREATE_COMMENTS
-
------------------------------------------------------------------------------------------------------------------------------------------------------------
-
comment on column A.A is 'Test a';
-
comment on column B.B is 'Test b';
-
comment on column C.C is 'Test c';
-
comment on column D.D is 'Test d';
-
comment on column E.E is 'Test e';
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-777031/
,如需转载,请注明出处,否则将追究法律责任。