通常你好备份数据需要放到另一个表中,这样就不会造成混乱.
1.建立Table1_backup2.建立trigger , 当update Table1 时, insert old record to Table1_backup .如果你不这样做,你的数据库会变得一团糟:-)
1.建立Table1_backup2.建立trigger , 当update Table1 时, insert old record to Table1_backup .如果你不这样做,你的数据库会变得一团糟:-)
delete table1 where no='001';
insert into table1 select * from table1_bak where no='001';
select no||'_BACK' as no_back , other_column, ....
from yourtable;insert into yourtable (select * from back_view where no_back='001_BACK');
No Name Age Address
001 张三 25 沈阳市和平区文化路115号
001_BACK 张三 25 沈阳市和平区文化路115号现在我要恢复数据了,先删除“001”的数据,然后用insert into table1 (select '001',Name,Age,Address from table1 where No='001_BACK'),这样就可以把备份的数据恢复了。但是必须写出所有的字段Name,Age,Address,如果一个表里的字段非常非常多的话,就很麻烦。特别是当数据库的结构没有完全定下来的时候。如果没有主键的话,就好办了。insert into table1 (select * from table1 where No='001_BACK'),不需要具体的知道有些什么字段。有没有什么方法,能够修改主键还不需要具体的知道所有字段的,如第二个语句,就很好,偏偏需要修改主键才能插入(只要有一个字段需要改变的话,就需要写出所有其它的字段)。
最后再提醒一句:一定要在同一个表里
Step 1
delete from table1 where no='001';
Step 2
create temp_table as select * from table1 where no='001_BACK';
Step 3
update table1 set no='001' where no='001_BACK';
Step 4
insert into table1 ( select * from temp_table where no='001_BACK');
-- 也可以不用 where 条件
Step 5
drop temp_table;这样就可以不用关心表中除主键外的其他字段了,Step1-5可以封装在匿名块之中执行。
step2 : create table tableaa as select * from tabletmp;
step3 : update tableaa set field=field||'_bak';
step4 : insert into tableaa select * from tabletmp;
step5 : drop table tabletmp;