繁体   English   中英

如何将表从一个模式移动到 Oracle 中的另一个模式?

[英]How to move tables from one schema to another schema in Oracle?

我在 Oracle 19C 中有 2 个架构,名称为MyConsahe MyCon模式中,我有很多表,但在sahe模式中,我没有任何表,如下所示。 在此处输入图像描述

我想将名称为test的表从MyCon模式移动到sahe模式。 我能怎么做?

我运行select * from all_tables order by table_name ,我得到关于(TableSpaceName,所有者)的以下结果。 在此处输入图像描述

注意: sahe模式具有所有 PRIVILEGES.( GRANT all PRIVILEGES TO sahe; )

表不能在模式之间“移动”。 您需要复制表格内容,然后删除原始内容,如下所示:

create table user_b.test
as
select * from user_a.test;

drop table user_a.test;

请注意,您可能希望在create table命令中指定其他参数,并且在确认新表中有所有数据之前,不应删除原始表。

我会使用为此目的设计的实用程序: data pump

这是一个例子。 用户scott拥有一些对象; 我将导出它并将其导入新用户mike

SYS身份连接并创建一个目录(Oracle object 指向包含转储文件(或任何其他类型的文件)的文件系统目录); 向将使用它的用户授予权限。 我们为什么要这样做? 因为数据泵需要目录。

SQL> create directory ext_dir as 'c:\temp';

Directory created.

SQL> grant read, write on directory ext_dir to scott;

Grant succeeded.
   
SQL>

现在,导出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>

下一步是将 scott 的模式导入mike 请注意,我没有预先创建用户mike ,导入数据泵是自动完成的。

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>

最后,让我们检查一下我们做了什么(新用户继承了scott 的密码):

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>

而已; 比手动、逐个表、编写自己的 PL/SQL 程序和东西更简单、更高效。

暂无
暂无

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

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