简体   繁体   中英

Move table from one schema to another by changing ownership oracle?

I have a four tables in a schema named DEMO. I want to move/clone/copy them in another schema named TEST.

Is it posible to move the table by changing ownership from DEMO to TEST ?

I use Oracle 11 and oracle sql developer 4.

You can use Oracle's data pump utilities to export the tables and then import them to a different schema,

Oracle Setup :

CREATE DIRECTORY dump_dir AS '/path/to/put/dumps';
GRANT WRITE ON DIRECTORY dump_dir TO DEMO;
GRANT READ, WRITE ON DIRECTORY dump_dir TO TEST;
GRANT CREATE TABLE TO TEST;

Export :

EXPDP DEMO/DEMOPASSWORD directory=DUMP_DIR tables=DEMO.TABLE1,DEMO.TABLE2,DEMO.TABLE3,DEMO.TABLE4 dumpfile=DEMO.dmp logfile=DEMO.log

Import :

use the remap_schema (and, if you need to, remap_tablespace ):

IMPDP TEST/TESTPASSWORD  directory=DUMP_DIR tables=DEMO.TABLE1,DEMO.TABLE2,DEMO.TABLE3,DEMO.TABLE4 remap_schema DEMO:TEST remap_tablespace=DEMO_TBS:TEST_TBS dumpfile=DEMO.dmp logfile=DEMO.log

If there are foreign keys in the tables you are remapping that point to other tables in the DEMO schema that you have not remapped then the remapping will try remapping those too and fail to create the foreign keys (since the table does not exist in the TEST schema) but should still import the data. Review the output of IMPDP to find these failures and then use, for example:

ALTER TABLE TEST.TABLE1
  ADD CONSTRAINT TABLE1__COLUMN1__FK
  FOREIGN KEY ( COLUMN1 ) REFERENCES DEMO.OTHER_TABLE ( COLUMN1 );

To create the appropriate foreign keys.

If you have access to the old schema from the new one, you can use:

CREATE table_name UNRECOVERABLE AS SELECT * FROM sourceSchema.SOURCE_table;

This will recreate the table under the new schema; and then you can drop the table from the old schema if you wish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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