Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create copy of full schema on same database in oracle

I want to create full copy of the schema on the same database. What is the best technique to do that?

I have few ideas in mind:

  1. Datapump Over Db link
  2. IMPDP on network link
  3. EXPDP and then IMPDP.

What would be the advantages and/or drawbacks of each technique?

like image 759
user3209595 Avatar asked Nov 06 '14 17:11

user3209595


People also ask

How do you duplicate a database schema?

If using SQL Developer: Tools -> Database exportThen select the objects and click the right arrow, or click the double right arrow to select all objects. Keep moving through the wizard and it should export multiple scripts to run. Run them all and you've got yourself a copy.

How do I copy schema from one schema to another?

In SQL Management studio right click the database that has the source table, select Tasks -> Export data. You will be able to set source and destination server and schema, select the tables you wish to copy and you can have the destination schema create the tables that will be exported.


3 Answers

You don't need a fancy network link or anything just the expdp/impdp commands. First export the schema that you want:

expdp fromUser/fromPword schemas=sourceSchema directory=DUMPDIR dumpfile=dump.dmp logfile=explog.txt

Tip: make sure that that user has the ability to write to DUMPDIR or whatever it is you call it

Then reimport the schema using the remap_schema parameter:

impdp toUser/toPword directory=DUMPDIR dumpfile=dump.dmp logfile=implog.txt remap_schema=fromUser:toUser

If you really want to use the network_link parameter (so you don't create a dumpfile for some reason) then I recommend reading this or this.
Great info on your question is found here also.

like image 145
mmmmmpie Avatar answered Oct 12 '22 20:10

mmmmmpie


Try this:

Copy schema into same database

I tested and it worked. but you have to create a public link like mentioned in the comments.

If you create a .par file which remaps your tablespaces you can reuse it. With some search replace of course.

O and don't forget to put the transaform in the .par file:

TRANSFORM=oid:n

Otherwise you might get errors.

like image 29
Rooie3000 Avatar answered Oct 12 '22 19:10

Rooie3000


The steps I did to copy schema's content from schema user_a to schema user_b in Oracle: DBMS is located inside an Oracle Linux Server container.

let's assume the schemas are defined like that:

create user user_a identified by user_a_pass default tablespace tablespace_a;
grant
    create materialized view,
    create procedure,
    create sequence,
    create session,
    create table,
    create type,
    create trigger,
    create view to user_a;

create user user_b identified by user_b_pass default tablespace tablespace_a;
grant
    create materialized view,
    create procedure,
    create sequence,
    create session,
    create table,
    create type,
    create trigger,
    create view to user_b;

The important point here: the schemas have access to the same tablespace and have create session privilege.

in order to copy, data schemas' directory are required: check granted directory:

select tp.grantee, tp.table_name from all_tab_privs tp where tp.privilege = 'WRITE' and tp.type = 'DIRECTORY';

check that the directory's path phisically exists and a group dba has an access to that.

if directory is not assigned to the schema user, do that:

create or replace directory user_dir as '/opt/oracle/product/19c/dbhome_1/user_dir_name';
grant read, write on directory user_dir to user_a;
grant read, write on directory user_dir to user_b;

run export:

expdp user_a/user_a_pass@host/database schemas=user_a directory=user_dir dumpfile=user_a.dmp logfile=schema_exp.log

run import:

impdp user_b/user_b_pass@host/database directory=user_dir dumpfile=user_a.dmp logfile=schema_imp.log remap_schema=user_a:user_b 
like image 43
PavelPraulov Avatar answered Oct 12 '22 20:10

PavelPraulov