elivans 发表于 2011-12-23 03:45

Export 和 Import 是一对读写Oracle数据的工具

<DIV>Export 和 Import 是一对读写Oracle数据的工具. Export 将 Oracle 数据库中的数据输出到操作系统文件中, Import 把这些文件中的数据读到Oracle 数据库中. Export/Import可以用来完成以下工作: 数据归档, 数据库升级, 备份数据库, 把数据从一个数据库移到另一个数据库, 回收数据库存储碎片等等. </DIV>
<DIV>&nbsp;&nbsp; 使用Export除了要保证磁盘或磁带上有足够的空间, 还必须执行expvew.sql和expvew.sql来创建Export使用的示图, 并创建EXP_FULL_DATABASE ROLE. 使用Export的用户应具有CREATE SESSION的权限, 若要Export其他用户的表还要有EXP_FULL_DATABASE ROLE.同样, 使用Import必须用catex.sql来创建IMP_FULL_DATABASE ROLE. 使用Import的用户应具有CREATE SESSION的权限. Import只能读入用Export创建的文件. 如果该文件是全库Export, 使用Import的用户还要有IMP_FULL_DATABASE ROLE. </DIV>
<DIV>&nbsp;&nbsp; Export/Import有三个级别: 表级, 用户级和全数据库级. </DIV>
<DIV>&nbsp;&nbsp; 表级允许Export/Import指定的表而不涉及其他数据库对象. 用户级Export/Import只针对属于指定用户的全部数据库对象. 只有拥有EXP_FULL_DATABASE/IMP_FULL_DATABASE ROLE 的用户才能使用全数据库级的Export/Import. </DIV>
<DIV>&nbsp;&nbsp;&nbsp; </DIV>
<DIV>&nbsp;&nbsp; 有三种方式执行Export/Import: 参数文件方式, 命令行方式和交互式. </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 使用参数文件是一种比较好的方式, 格式为: </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Exp &lt;username/password&gt; PARFILE = &lt;filename&gt; </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Imp &lt;username/password&gt; PARFILE = &lt;filename&gt; </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 命令行方式是指在命令行中指定参数: </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Exp &lt;username/password&gt; TABLES = (emp,dept) GRANTS = y </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Imp &lt;username/password&gt; FROMUSER = scott TOUSER = test TABLES = (emp,dept) </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 交互式只要敲入Exp或Imp然后回答屏幕上的提问即可. </DIV>
<DIV>下面介绍一些EXPORT/IMPORT的使用技巧 </DIV>
<DIV>&nbsp;&nbsp; - 把数据库对象从一个用户移到另一个用户 </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Oracle 不允许直接改变表的拥有者, 利用Export/Import可以达到这一目的. </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 假设要把表 T 的拥有者User1改为User2, 具体步骤是: </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - exp system/manager tables = User1.T </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - imp system/manager fromuser = User1 touser = User2 tables = T </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - drop table User1.T </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </DIV>
<DIV>&nbsp;&nbsp; - 把数据库对象从一个表空间移到另一个表空间 </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 建表时可以指定表空间, 表空间一经确定就部能随意改变. 若要表 T 从表空间 tbs1移到表空间 tbs2, 就要采用以下方法: </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - exp &lt;user/passwd&gt; tables = T </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - imp &lt;user/passwd&gt; tables = T indexfile = temp.sql </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - drop table T </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - 编辑 temp.sql 只保留所需的建表命令并指定表空间为tbs2 </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - 以表的所有者执行temp.sql </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - imp &lt;user/passwd&gt; tables = T ignore = Y </DIV>
<DIV>&nbsp;&nbsp; - 只输出一个的表空间 </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 通常数据库设计成用户若属于某个表空间, 那么这个用户创建的数据库对象也在该表空间内. </DIV>
<DIV>&nbsp;&nbsp; Export某个表空间可用如下方法: </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - 查看表空间内所有用户 </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; spool owners </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select owner </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from dba_segments </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where tablespace_name = '&lt;TablespaceName&gt;'; </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; spool off </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - 查看表空间内所有数据库对象 </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; spool objects </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select owner, object_name, object_type </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from dba_objects </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where owner = 'owner1' </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; or owner = 'owner2' </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ... </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; or owner = 'ownern'; </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; spool off </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - 作表级Export </DIV>
<DIV>&nbsp;&nbsp; - 从Exp文件中提取创建数据库对象的命令 </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp; 在IMPORT时使用 'INDEXFILE = FileName', IMPORT把创建数据库对象的命令输出到指定的文件中, 编辑后运行这个文件就能建立数据库对象. </DIV>
<DIV>&nbsp;&nbsp;&nbsp; </DIV>
<DIV>下面介绍Export/Import 使用中几个常见的问题和解决办法 </DIV>
<DIV>&nbsp;&nbsp; - Export/Import 使用不同的字符集 </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Export文件中包含着字符信息. 如过输入/输出都使用担字节字符集, 如EBCDIC或US7ASCII, 输入时将自动进行字符集转换. 转换过程中, 若输出文件中含有的目标字符集中不能匹配的字符会自动设成缺省字符. </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 对于多字节字符集, 如ZHS16CGB231280, 通常不能自动转换, 只有在字符串长度不变的情况下才能自动转换. </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp; </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </DIV>
<DIV>&nbsp;&nbsp; - 空间不够 -- 碎片问题 </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 有些时候, 即使数据库仍有足够的空间, 使用IMPORT时却出空间不够的错误. 出现这种现象通常是由于数据库中存在碎片, 即有很多小的不连续的空闲空间. 解决办法是先将数据库全库EXPORT(FULL=Y), SHUTDOWN数据库, 重新建库(CREATE DATABASE)后用IMPORT FULL=Y恢复数据. </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp; </DIV>
<DIV>&nbsp;&nbsp; - ROLLBACK段不够 </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Export/Import使用过程中, 如果数据量很大会出现'ROLLBACK段不够'的错误. 这时要建一个足够大的ROLLBACK段, 使它ONLINE而其他ROLLBACK段OFFLINE. 这样, Export/Import使用这个大ROLLBACK段, 从而避免上述现象.<BR></DIV>
页: [1]
查看完整版本: Export 和 Import 是一对读写Oracle数据的工具