首页 / ORACLE / Oracle对表进行备份
Oracle对表进行备份
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Oracle对表进行备份,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6321字,纯文字阅读大概需要10分钟。
内容图文
![Oracle对表进行备份](/upload/InfoBanner/zyjiaocheng/1321/38a52080d332477e8e1117a4c988d2d7.jpg)
前言:
在实际开发中,我们常常需要对单张或多张表进行备份,以下博主就从这两个方面进行总结。如需转载,请标明来处,谢谢!
在备份前我们先创建表盒相关测试的数据
-- Create table create table TBLUSER ( id NUMBER(8) notnull, username VARCHAR2(100) notnull, 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 ontable TBLUSER is‘用户表‘; -- Add comments to the columns comment oncolumn TBLUSER.id is‘自动增长列‘; comment oncolumn TBLUSER.username is‘用户名‘; comment oncolumn TBLUSER.sex is‘性别‘; comment oncolumn TBLUSER.age is‘年龄‘; comment oncolumn TBLUSER.companyaddress is‘公司地址‘; comment oncolumn TBLUSER.homeaddress is‘家庭地址‘; comment oncolumn TBLUSER.phone is‘电话‘; comment oncolumn TBLUSER.tel is‘座机‘; comment oncolumn TBLUSER.remark is‘备注‘; comment oncolumn TBLUSER.usercode is‘用户代码‘; -- Create/Recreate primary, unique and foreign key constraints altertable TBLUSER addprimarykey (ID) using index tablespace USERS pctfree 10 initrans 2 maxtrans 255 storage ( initial 64K next 1M minextents 1 maxextents unlimited ); --插入数据insertinto tbluser (ID, USERNAME, SEX, AGE, COMPANYADDRESS, HOMEADDRESS, PHONE, TEL, REMARK, USERCODE) values (10003, ‘小明‘, ‘男‘, 24, ‘武汉xxx有限责任公司‘, ‘湖北省武汉市‘, ‘176xxxxxxx5‘, ‘027-001‘, null, ‘XM001‘); insertinto tbluser (ID, USERNAME, SEX, AGE, COMPANYADDRESS, HOMEADDRESS, PHONE, TEL, REMARK, USERCODE) values (10004, ‘小王‘, ‘男‘, 19, ‘武汉xxx有限责任公司‘, ‘湖北省武汉市‘, ‘176xxxxxxx5‘, ‘027-001‘, null, ‘XW001‘); insertinto tbluser (ID, USERNAME, SEX, AGE, COMPANYADDRESS, HOMEADDRESS, PHONE, TEL, REMARK, USERCODE) values (10005, ‘小红‘, ‘女‘, 20, ‘深圳xxx有限责任公司‘, ‘深圳市宝安区‘, ‘176xxxxxxx5‘, ‘075-001‘, null, ‘XH001‘); insertinto tbluser (ID, USERNAME, SEX, AGE, COMPANYADDRESS, HOMEADDRESS, PHONE, TEL, REMARK, USERCODE) values (10007, ‘小蓝‘, ‘男‘, 24, ‘武汉xxx有限责任公司‘, ‘湖北省武汉市‘, ‘176xxxxxxx5‘, ‘027-001‘, null, ‘XL001‘); insertinto tbluser (ID, USERNAME, SEX, AGE, COMPANYADDRESS, HOMEADDRESS, PHONE, TEL, REMARK, USERCODE) values (10009, ‘小芳‘, ‘女‘, 30, ‘长沙xxx有限责任公司‘, ‘湖南省长沙市‘, ‘123xxxxxxx5‘, ‘027-001‘, null, ‘XF001‘); insertinto tbluser (ID, USERNAME, SEX, AGE, COMPANYADDRESS, HOMEADDRESS, PHONE, TEL, REMARK, USERCODE) values (10011, ‘小黄‘, ‘男‘, 20, ‘武汉xxx有限责任公司‘, ‘湖北省武汉市‘, ‘176xxxxxxx5‘, ‘027-001‘, null, ‘XH001‘); insertinto tbluser (ID, USERNAME, SEX, AGE, COMPANYADDRESS, HOMEADDRESS, PHONE, TEL, REMARK, USERCODE) values (10013, ‘小琴‘, ‘男‘, 18, ‘北京xxx有限责任公司‘, ‘北京市西旗xxx‘, ‘176xxxxxxx5‘, ‘027-001‘, null, ‘XQ001‘); insertinto 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 asselect*from tbluser;
此处需要注意的是,此备份表通过对比SQL语句可以看出只备份了表的结构和数据,另外备份前,tbluser_bak不能在表中存在
-- 备份完后的tbluser_bakSQL语句 -- Create table create table TBLUSER_BAK ( id NUMBER(8), username VARCHAR2(100) notnull, 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) notnull, sname VARCHAR2(4) notnull, ssex VARCHAR2(2) notnull, sbirthday DATE, class VARCHAR2(5) ) tablespace USERS pctfree 10 initrans 1 maxtrans 255 storage ( initial 64K next 1M minextents 1 maxextents unlimited ); --插入数据insertinto tblstudent (SNO, SNAME, SSEX, SBIRTHDAY, CLASS) values (‘108‘, ‘曾华‘, ‘男‘, to_date(‘24-02-2011‘, ‘dd-mm-yyyy‘), ‘95033‘); insertinto tblstudent (SNO, SNAME, SSEX, SBIRTHDAY, CLASS) values (‘105‘, ‘匡明‘, ‘男‘, to_date(‘02-10-1975‘, ‘dd-mm-yyyy‘), ‘95031‘); insertinto tblstudent (SNO, SNAME, SSEX, SBIRTHDAY, CLASS) values (‘107‘, ‘王丽‘, ‘女‘, to_date(‘23-01-1976‘, ‘dd-mm-yyyy‘), ‘95033‘); insertinto tblstudent (SNO, SNAME, SSEX, SBIRTHDAY, CLASS) values (‘101‘, ‘李军‘, ‘男‘, to_date(‘20-02-1976‘, ‘dd-mm-yyyy‘), ‘95033‘); insertinto tblstudent (SNO, SNAME, SSEX, SBIRTHDAY, CLASS) values (‘109‘, ‘王芳‘, ‘女‘, to_date(‘20-02-1976‘, ‘dd-mm-yyyy‘), ‘95031‘); insertinto tblstudent (SNO, SNAME, SSEX, SBIRTHDAY, CLASS) values (‘103‘, ‘陆君‘, ‘男‘, to_date(‘03-06-1974‘, ‘dd-mm-yyyy‘), ‘95031‘);
备份:多张表的备份可以用命令导出表(备份)到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@oratfile=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@oratfile=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.0 - 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"导入了 8 行 . . 正在导入表 "TBLSTUDENT"导入了 6 行 成功终止导入, 没有出现警告。
原文:https://www.cnblogs.com/wuhanjackh/p/12341797.html
内容总结
以上是互联网集市为您收集整理的Oracle对表进行备份全部内容,希望文章能够帮你解决Oracle对表进行备份所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。