How best can I recreate an Oracle database?

前端 未结 3 1611
执念已碎
执念已碎 2021-01-21 02:09

Oracle 11gR2 (x86 Windows):

I have a db with 250 tables with indexes and constraints. I need to re-create these tables, indexes and constraints in a new db and load the

3条回答
  •  北海茫月
    2021-01-21 02:35

    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. This is an interface to Datapump (as @Sylvain Leroux mentioned) that is callable from within the database.

    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.

提交回复
热议问题