Oracle对表进行备份
2024-09-06 20:40:06
前言:
在实际开发中,我们常常需要对单张或多张表进行备份,以下博主就从这两个方面进行总结。如需转载,请标明来处,谢谢!
在备份前我们先创建表盒相关测试的数据
-- Create table
create table TBLUSER
(
id NUMBER(8) not null,
username VARCHAR2(100) not null,
sex VARCHAR2(2) default '男',
age NUMBER(3),
companyaddress VARCHAR2(1000),
homeaddress VARCHAR2(1000),
phone VARCHAR2(40),
tel VARCHAR2(40),
remark VARCHAR2(4000),
usercode VARCHAR2(40)
)
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
-- Add comments to the table
comment on table TBLUSER
is '用户表';
-- Add comments to the columns
comment on column TBLUSER.id
is '自动增长列';
comment on column TBLUSER.username
is '用户名';
comment on column TBLUSER.sex
is '性别';
comment on column TBLUSER.age
is '年龄';
comment on column TBLUSER.companyaddress
is '公司地址';
comment on column TBLUSER.homeaddress
is '家庭地址';
comment on column TBLUSER.phone
is '电话';
comment on column TBLUSER.tel
is '座机';
comment on column TBLUSER.remark
is '备注';
comment on column TBLUSER.usercode
is '用户代码';
-- Create/Recreate primary, unique and foreign key constraints
alter table TBLUSER
add primary key (ID)
using index
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
); --插入数据
insert into tbluser (ID, USERNAME, SEX, AGE, COMPANYADDRESS, HOMEADDRESS, PHONE, TEL, REMARK, USERCODE)
values (10003, '小明', '男', 24, '武汉xxx有限责任公司', '湖北省武汉市', '176xxxxxxx5', '027-001', null, 'XM001'); insert into tbluser (ID, USERNAME, SEX, AGE, COMPANYADDRESS, HOMEADDRESS, PHONE, TEL, REMARK, USERCODE)
values (10004, '小王', '男', 19, '武汉xxx有限责任公司', '湖北省武汉市', '176xxxxxxx5', '027-001', null, 'XW001'); insert into tbluser (ID, USERNAME, SEX, AGE, COMPANYADDRESS, HOMEADDRESS, PHONE, TEL, REMARK, USERCODE)
values (10005, '小红', '女', 20, '深圳xxx有限责任公司', '深圳市宝安区', '176xxxxxxx5', '075-001', null, 'XH001'); insert into tbluser (ID, USERNAME, SEX, AGE, COMPANYADDRESS, HOMEADDRESS, PHONE, TEL, REMARK, USERCODE)
values (10007, '小蓝', '男', 24, '武汉xxx有限责任公司', '湖北省武汉市', '176xxxxxxx5', '027-001', null, 'XL001'); insert into tbluser (ID, USERNAME, SEX, AGE, COMPANYADDRESS, HOMEADDRESS, PHONE, TEL, REMARK, USERCODE)
values (10009, '小芳', '女', 30, '长沙xxx有限责任公司', '湖南省长沙市', '123xxxxxxx5', '027-001', null, 'XF001'); insert into tbluser (ID, USERNAME, SEX, AGE, COMPANYADDRESS, HOMEADDRESS, PHONE, TEL, REMARK, USERCODE)
values (10011, '小黄', '男', 20, '武汉xxx有限责任公司', '湖北省武汉市', '176xxxxxxx5', '027-001', null, 'XH001'); insert into tbluser (ID, USERNAME, SEX, AGE, COMPANYADDRESS, HOMEADDRESS, PHONE, TEL, REMARK, USERCODE)
values (10013, '小琴', '男', 18, '北京xxx有限责任公司', '北京市西旗xxx', '176xxxxxxx5', '027-001', null, 'XQ001'); insert into tbluser (ID, USERNAME, SEX, AGE, COMPANYADDRESS, HOMEADDRESS, PHONE, TEL, REMARK, USERCODE)
values (10014, '小陈', '男', 19, '武汉xxx有限责任公司', '上海市xxx', '176xxxxxxx5', '027-001', null, 'XC001');
单张表的备份
--备份tbluser的数据到tbluser_bak中
create table tbluser_bak as select * from tbluser; --此处不加as也行
此处需要注意的是,此备份表通过对比SQL语句可以看出只备份了表的结构和数据,另外备份前,tbluser_bak不能在表中存在
--备份完后的tbluser_bakSQL语句
-- Create table
create table TBLUSER_BAK
(
id NUMBER(8),
username VARCHAR2(100) not null,
sex VARCHAR2(2),
age NUMBER(3),
companyaddress VARCHAR2(1000),
homeaddress VARCHAR2(1000),
phone VARCHAR2(40),
tel VARCHAR2(40),
remark VARCHAR2(4000),
usercode VARCHAR2(40)
)
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
单张表数据恢复
方法一:此种方式是当原表结构存在,且新旧表的表结构一致的情况下,仅仅备份数据【同字段仅数据】
--删除备份表的数据
truncate table tbluser_bak; --还原表数据
insert into tbluser_bak select * from tbluser ; --查看数据是否恢复
select * from tbluser_bak;
查询结果如下
方法二:此种方式是当原表结构存在,但新旧表的字段不一致的情况下,仅仅备份指定字段的数据【不同的字段仅数据】
--删除备份表的数据
truncate table tbluser_bak; --修改表结构(删除表tbluser_bak中所有字段,把username,usercode改成uname,ucode) --还原表数据
insert into tbluser_bak(uname,ucode) select t.username,t.usercode from tbluser t ; --查询数据是否恢复
select * from tbluser_bak;
查询结果如下
多张表的备份
创建测试表及数据:
-- Create table
create table TBLSTUDENT
(
sno VARCHAR2(3) not null,
sname VARCHAR2(4) not null,
ssex VARCHAR2(2) not null,
sbirthday DATE,
class VARCHAR2(5)
)
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
); --插入数据
insert into tblstudent (SNO, SNAME, SSEX, SBIRTHDAY, CLASS)
values ('', '曾华', '男', to_date('24-02-2011', 'dd-mm-yyyy'), ''); insert into tblstudent (SNO, SNAME, SSEX, SBIRTHDAY, CLASS)
values ('', '匡明', '男', to_date('02-10-1975', 'dd-mm-yyyy'), ''); insert into tblstudent (SNO, SNAME, SSEX, SBIRTHDAY, CLASS)
values ('', '王丽', '女', to_date('23-01-1976', 'dd-mm-yyyy'), ''); insert into tblstudent (SNO, SNAME, SSEX, SBIRTHDAY, CLASS)
values ('', '李军', '男', to_date('20-02-1976', 'dd-mm-yyyy'), ''); insert into tblstudent (SNO, SNAME, SSEX, SBIRTHDAY, CLASS)
values ('', '王芳', '女', to_date('20-02-1976', 'dd-mm-yyyy'), ''); insert into tblstudent (SNO, SNAME, SSEX, SBIRTHDAY, CLASS)
values ('', '陆君', '男', to_date('03-06-1974', 'dd-mm-yyyy'), '');
备份:多张表的备份可以用命令导出表(备份)到dmp文件中
操作方法:windows+r 在出现的运行窗口中直接输入以下命令进行备份
--exp [用户名]/[密码] tables=[表一],[表二] file=xxx\table.dmp
exp test/111111 tables=tbluser,tblstudent file=C:\Users\HUANG\Desktop\test\20200221.dmp --或者 exp test/test@orat file=C:\Users\HUANG\Desktop\test\20200221.dmp tables=(tb1,tb2,tb3,...)
exp test/111111@orat file=C:\Users\HUANG\Desktop\test\20200221.dmp tables=(tbluser,tblstudent)
其中:
“test/111111”是用户名和密码;“orat”是“网络服务名”。
file=c:\xx.dmp:是保存到本地的全文件路径。
tables=(tb1,tb2,tb3,...):“(tb1,tb2,tb3,...)”是你选择需要备份的那些表的名称。
因为,备份的时候一般都写一个日志备份文档,以方便以后查找,所以,原语句可以改写成
exp test/111111@orat file=C:\Users\HUANG\Desktop\test\20200221.dmp tables=(tbluser,tblstudent) log=C:\Users\HUANG\Desktop\test\20200221.text
导出结果如图所示
还原:windows+r 在出现的运行窗口中直接输入以下命令进行还原
imp test/111111@orat grants=y commit=y full=y ignore=y file=C:\Users\HUANG\Desktop\test\20200221.dmp log=C:\Users\HUANG\Desktop\test\20200221.text
--log后面表示记录日志,也可以只导数据不记录
还原前数据:
还原后数据
20200221.txt日志文件如下
连接到: Oracle Database 11g Enterprise Edition Release 11.2.0.1. - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options 经由常规路径由 EXPORT:V11.02.00 创建的导出文件
已经完成 ZHS16GBK 字符集和 AL16UTF16 NCHAR 字符集中的导入
. 正在将 TEST 的对象导入到 TEST
. 正在将 TEST 的对象导入到 TEST
. . 正在导入表 "TBLUSER"导入了 行
. . 正在导入表 "TBLSTUDENT"导入了 行
成功终止导入, 没有出现警告。
最新文章
- ASP.NET免费服务器~支持MVC和Net4.5
- 《大道至简》第一章读后感(java语言伪代码)
- 保证唯一的一种js提交数据方式,还不错
- rewrite规则中参数多于9个的处理方式 apache nginx
- Ioc正解
- SQL Server--用户自定义函数
- Spring资源访问
- telnet登录路由器启动服务的shell脚本
- BaseAdapter优化深入分析
- RabbitMq install on Centos6.3
- java程序使用memcached
- 学习笔记TF031:实现VGGNet
- PackageManager整理
- 前端----css的继承性和层叠性
- Confluence 6 修改站点的标识图片
- Redis缓存的使用
- SaaS教父:我眼中最糟糕的9条SaaS建议(转)
- c run-time library 和 standard c++ library
- sgu 183. Painting the balls 动态规划 难度:3
- BUAA软工个人作业Week2-代码复审
热门文章
- weblogic高级进阶之ssl配置证书
- 02.Hamcrest和TestSuite介绍
- java scoket Blocking 阻塞IO socket通信三
- Python干货整理之数据结构篇
- 只需几行 JavaScript 代码,网页瞬间有气质了!
- 【错误】上传新的项目出错 error: failed to push some refs to 'https://github.com/...
- django admin 添加用户出现外键约束错误
- 关于位图数据位和系统管理区大小-P6
- 记一次解密wireshark抓取的冰蝎通信流量
- Web前端年后跳槽面试复习指南