简体   繁体   English

如何才能最好地重新创建Oracle数据库?

[英]How best can I recreate an Oracle database?

Oracle 11gR2 (x86 Windows): Oracle 11gR2(x86 Windows):

I have a db with 250 tables with indexes and constraints. 我有一个包含250个带索引和约束的表的数据库。 I need to re-create these tables, indexes and constraints in a new db and load the data. 我需要在新数据库中重新创建这些表,索引和约束并加载数据。 I need to know how to do the following in SQL Plus and/or SQL Developer, unless there's a magical utility that can automate all of this. 我需要知道如何在SQL Plus和/或SQL Developer中执行以下操作,除非有一个神奇的实用程序可以自动执行所有这些操作。 Thanks in advance! 提前致谢!

  1. Unload (export) all the data from the 250 tables. 卸载(导出)250个表中的所有数据。

  2. Create an sql script file containing the CREATE TABLE statements for the 250 tables. 创建一个包含250个表的CREATE TABLE语句的sql脚本文件。

  3. Create an sql script file containing the CREATE INDEX statements for the 250 tables. 创建一个包含250个表的CREATE INDEX语句的sql脚本文件。

  4. Create an sql script file containing the ALTER TABLE ADD CONSTRAINT statements for the 250 tables. 创建一个sql脚本文件,其中包含250个表的ALTER TABLE ADD CONSTRAINT语句。

  5. Run the script to create the tables in a new db. 运行脚本以在新数据库中创建表。

  6. Load the exported data into the tables in the new db. 将导出的数据加载到新数据库的表中。

  7. Run the script to create all the indexes. 运行脚本以创建所有索引。

  8. Run the script to add all the contraints. 运行脚本以添加所有约束。

EDIT: I'm connected to the remote desktop which links to the source db on a Windows Server 2008. The remote only has an Oracle client installed. 编辑:我已连接到远程桌面,该桌面链接到Windows Server 2008上的源数据库。远程只安装了Oracle客户端。 For security reasons, I'm not allowed to link directly from my local computer to the Win Server, so can I dump the whole source db to the remote then zip it to my local target machine? 出于安全原因,我不允许直接从我的本地计算机链接到Win Server,所以我可以将整个源数据库转储到远程数据库然后将其压缩到我的本地目标计算机吗? I'm trying to replicate the entire db on my computer. 我正在尝试在我的计算机上复制整个数据库。

Starting from Oracle 10g, you could use the Data Pump command-line clients expdb and impdb to export/import data and/or schema from one DB to an other. 从Oracle 10g开始,您可以使用Data Pump命令行客户端 expdbimpdb将数据和/或模式从一个DB导出/导入到另一个DB。 As a matter of fact, those two command-line utilities are only wrappers that "use the procedures provided in the DBMS_DATAPUMP PL/SQL package to execute export and import commands, using the parameters entered at the command line." 事实上,这两个命令行实用程序只是“使用DBMS_DATAPUMP PL / SQL包中提供的过程执行导出和导入命令,使用在命令行输入的参数”的包装器 (quoted from Oracle's documentation) (引自Oracle的文档)

Given your needs, you will have to create a directory then generate a full dump of your database using expdb : 根据您的需要,您必须创建一个目录,然后使用expdb生成数据库的完整转储:

SQL> CREATE OR REPLACE DIRECTORY dump_dir AS '/path/to/dump/folder/';
sh$ expdp system@db10g full=Y directory=DUMP_DIR dumpfile=db.dmp logfile=db.log

As the dump is written using some binary format, you will have to use the corresponding import utility to (re)import your DB. 由于转储是使用某种二进制格式编写的,因此您必须使用相应的导入实用程序来(重新)导入数据库。 Basically replacing expdb by impdb in the above command: 基本上替换expdb通过impdb在上述命令:

sh$ impdp system@db10g full=Y directory=DUMP_DIR dumpfile=db.dmp logfile=db.log

For simple table dump, use that version instead: 对于简单的表转储,请使用该版本:

sh$ expdp sylvain@db10g tables=DEPT,EMP directory=DUMP_DIR dumpfile=db.dmp logfile=db.log

As you noticed, you can use it with your standard user account, provided you have access to the given directory ( GRANT READ, WRITE ON DIRECTORY dump_dir TO sylvain; ). 正如您所注意到的,您可以将其与标准用户帐户一起使用,前提是您可以访问给定目录( GRANT READ, WRITE ON DIRECTORY dump_dir TO sylvain; )。


For detailed usage explanations, see 有关详细用法说明,请参阅

If you can create a database link from your local database to the one that currently contains the data, you can use the DBMS_DATAPUMP package to copy the entire schema. 如果可以创建从本地数据库到当前包含数据的数据库链接,则可以使用DBMS_DATAPUMP包来复制整个模式。 This is an interface to Datapump (as @Sylvain Leroux mentioned) that is callable from within the database. 这是Datapump的接口(如提到的@Sylvain Leroux),可以从数据库中调用。

DECLARE
   dph NUMBER;
   source_schema VARCHAR2 (30) := 'SCHEMA_TO_EXPORT';
   target_schema VARCHAR2 (30) := 'SCHEMA_TO_IMPORT';
   job_name VARCHAR2 (30) := UPPER ('IMPORT_' || target_schema);
   p_parallel NUMBER := 3;
   v_start TIMESTAMP := SYSTIMESTAMP;
   v_state VARCHAR2 (30);
BEGIN
   dph :=
      DBMS_DATAPUMP.open ('IMPORT',
                          'SCHEMA',
                          'DB_LINK_NAME',
                          job_name);
   DBMS_OUTPUT.put_line ('dph = ' || dph);
   DBMS_DATAPUMP.metadata_filter (dph,
                                  'SCHEMA_LIST',
                                  '''' || source_schema || '''');
   DBMS_DATAPUMP.metadata_remap (dph,
                                 'REMAP_SCHEMA',
                                 source_schema,
                                 target_schema);
   DBMS_DATAPUMP.set_parameter (dph, 'TABLE_EXISTS_ACTION', 'REPLACE');
   DBMS_DATAPUMP.set_parallel (dph, p_parallel);
   DBMS_DATAPUMP.start_job (dph);
   DBMS_DATAPUMP.wait_for_job (dph, v_state);
   DBMS_OUTPUT.put_line ('Export/Import time: ' || (SYSTIMESTAMP - v_start));
   DBMS_OUTPUT.put_line ('Final state: ' || v_state);
END;

The script above actually copies and renames the schema. 上面的脚本实际上复制并重命名架构。 If you want to keep the same schema name, I believe you'd just remove the metadata_remap call. 如果您想保留相同的模式名称,我相信您只需删除metadata_remap调用。

SQL Developer can help with #1 by creating INSERT statements with a formatted query result : SQL Developer可以通过使用格式化查询结果创建INSERT语句来帮助#1:

Select /*insert*/ * 
from My_Table;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM