I Have 2 schema in Oracle 19C with name MyCon
and sahe
. In MyCon
schema I have many tables but In sahe
schema I don't have any table as shown as below.
I want move table with name test
from MyCon
schema to sahe
schema. How can I do?
I run select * from all_tables order by table_name
and I get bellow result about (TableSpaceName, owner).
Note: sahe
schema have all PRIVILEGES .(GRANT all PRIVILEGES TO sahe;
)
CodePudding user response:
I'd use utility designed for such a purpose: data pump.
Here's an example. User scott
owns some objects; I'll export it and import it into a new user, mike
.
Connect as SYS
and create a directory (Oracle object that points to a filesystem directory that contains dump files (or any other kind of files)); grant privileges to user(s) who will be using it. Why do we do that? Because data pump requires the directory.
SQL> create directory ext_dir as 'c:\temp';
Directory created.
SQL> grant read, write on directory ext_dir to scott;
Grant succeeded.
SQL>
Now, export scott
:
c:\temp>expdp scott/tiger@pdb1 directory=ext_dir dumpfile=scott.dmp logfile=exp_scott.log
Export: Release 21.0.0.0.0 - Production on Mon Jul 25 22:59:37 2022
Version 21.3.0.0.0
Copyright (c) 1982, 2021, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production
Starting "SCOTT"."SYS_EXPORT_SCHEMA_01": scott/********@pdb1 directory=ext_dir dumpfile=scott.dmp logfile=exp_scott.log
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Processing object type SCHEMA_EXPORT/STATISTICS/MARKER
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/TABLE/TABLE
Processing object type SCHEMA_EXPORT/TABLE/COMMENT
Processing object type SCHEMA_EXPORT/FUNCTION/FUNCTION
Processing object type SCHEMA_EXPORT/PROCEDURE/PROCEDURE
Processing object type SCHEMA_EXPORT/FUNCTION/ALTER_FUNCTION
Processing object type SCHEMA_EXPORT/PROCEDURE/ALTER_PROCEDURE
Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/TRIGGER
. . exported "SCOTT"."ORAERR" 2.976 MB 59904 rows
. . exported "SCOTT"."ERRORS" 12.09 KB 110 rows
. . exported "SCOTT"."EMP" 8.789 KB 14 rows
. . exported "SCOTT"."SALESMEN" 6.546 KB 6 rows
. . exported "SCOTT"."INVOICE" 6.445 KB 4 rows
. . exported "SCOTT"."TESTS" 6.5 KB 3 rows
. . exported "SCOTT"."DEPT" 6.023 KB 4 rows
. . exported "SCOTT"."SALGRADE" 5.953 KB 5 rows
. . exported "SCOTT"."DEPARTMENTS" 5.945 KB 1 rows
. . exported "SCOTT"."PERSON" 5.492 KB 1 rows
. . exported "SCOTT"."ACTIVE_YEAR" 5.062 KB 1 rows
. . exported "SCOTT"."COPY_DEPARTMENTS" 5.078 KB 1 rows
. . exported "SCOTT"."FOO" 5.046 KB 1 rows
. . exported "SCOTT"."BONUS" 0 KB 0 rows
. . exported "SCOTT"."DAT" 0 KB 0 rows
. . exported "SCOTT"."DEPT_BACKUP" 6.031 KB 4 rows
. . exported "SCOTT"."PROGRAMMER" 0 KB 0 rows
. . exported "SCOTT"."PROGRAMS" 0 KB 0 rows
. . exported "SCOTT"."REGIONS" 0 KB 0 rows
. . exported "SCOTT"."TBL_HISTORY" 0 KB 0 rows
Master table "SCOTT"."SYS_EXPORT_SCHEMA_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SCOTT.SYS_EXPORT_SCHEMA_01 is:
C:\TEMP\SCOTT.DMP
Job "SCOTT"."SYS_EXPORT_SCHEMA_01" successfully completed at Mon Jul 25 23:00:27 2022 elapsed 0 00:00:48
c:\temp>
The next step is to import scott's schema into mike
. Note that I didn't pre-create user mike
, import data pump did it automatically.
c:\temp>impdp system@pdb1 directory=ext_dir dumpfile=scott.dmp logfile=imp_mike.log remap_schema=scott:mike full=y
Import: Release 21.0.0.0.0 - Production on Mon Jul 25 23:21:40 2022
Version 21.3.0.0.0
Copyright (c) 1982, 2021, Oracle and/or its affiliates. All rights reserved.
Password:
Connected to: Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production
Master table "SYSTEM"."SYS_IMPORT_FULL_01" successfully loaded/unloaded
Starting "SYSTEM"."SYS_IMPORT_FULL_01": system/********@pdb1 directory=ext_dir dumpfile=scott.dmp logfile=imp_mike.log remap_schema=scott:mike full=y
Processing object type SCHEMA_EXPORT/USER
Processing object type SCHEMA_EXPORT/SYSTEM_GRANT
Processing object type SCHEMA_EXPORT/ROLE_GRANT
Processing object type SCHEMA_EXPORT/DEFAULT_ROLE
Processing object type SCHEMA_EXPORT/ON_USER_GRANT
Processing object type SCHEMA_EXPORT/TABLESPACE_QUOTA
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/TABLE/TABLE
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
. . imported "MIKE"."ORAERR" 2.976 MB 59904 rows
. . imported "MIKE"."ERRORS" 12.09 KB 110 rows
. . imported "MIKE"."EMP" 8.789 KB 14 rows
. . imported "MIKE"."SALESMEN" 6.546 KB 6 rows
. . imported "MIKE"."INVOICE" 6.445 KB 4 rows
. . imported "MIKE"."TESTS" 6.5 KB 3 rows
. . imported "MIKE"."DEPT" 6.023 KB 4 rows
. . imported "MIKE"."SALGRADE" 5.953 KB 5 rows
. . imported "MIKE"."DEPARTMENTS" 5.945 KB 1 rows
. . imported "MIKE"."PERSON" 5.492 KB 1 rows
. . imported "MIKE"."ACTIVE_YEAR" 5.062 KB 1 rows
. . imported "MIKE"."COPY_DEPARTMENTS" 5.078 KB 1 rows
. . imported "MIKE"."FOO" 5.046 KB 1 rows
. . imported "MIKE"."BONUS" 0 KB 0 rows
. . imported "MIKE"."DAT" 0 KB 0 rows
. . imported "MIKE"."DEPT_BACKUP" 6.031 KB 4 rows
. . imported "MIKE"."PROGRAMMER" 0 KB 0 rows
. . imported "MIKE"."PROGRAMS" 0 KB 0 rows
. . imported "MIKE"."REGIONS" 0 KB 0 rows
. . imported "MIKE"."TBL_HISTORY" 0 KB 0 rows
Processing object type SCHEMA_EXPORT/FUNCTION/FUNCTION
Processing object type SCHEMA_EXPORT/PROCEDURE/PROCEDURE
Processing object type SCHEMA_EXPORT/FUNCTION/ALTER_FUNCTION
Processing object type SCHEMA_EXPORT/PROCEDURE/ALTER_PROCEDURE
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/TRIGGER
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Processing object type SCHEMA_EXPORT/STATISTICS/MARKER
Job "SYSTEM"."SYS_IMPORT_FULL_01" successfully completed at Mon Jul 25 23:21:53 2022 elapsed 0 00:00:08
c:\temp>
Finally, let's check what we've done (new user inherited scott's password):
c:\temp>sqlplus mike/tiger@pdb1
SQL*Plus: Release 21.0.0.0.0 - Production on Mon Jul 25 23:23:50 2022
Version 21.3.0.0.0
Copyright (c) 1982, 2021, Oracle. All rights reserved.
Connected to:
Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production
Version 21.3.0.0.0
SQL> show user
USER is "MIKE"
SQL> select count(*) from tab;
COUNT(*)
----------
20
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL>
That's it; way simpler and more efficient that doing it manually, table-by-table, writing your own PL/SQL programs and stuff.
CodePudding user response:
A table cannot be "moved" between schemas. You need to copy the table content, then drop the original, like this:
create table user_b.test
as
select * from user_a.test;
drop table user_a.test;
Note that you may want to specify additional parameters in your create table
command, and you should not drop the original table until you've confirmed you have all the data in the new table.