我想将现有的 table 从一个架构复制到另一个架构而不做任何更改

I want to copy existing table from one schema to another without changing anything

我正在尝试使用 DBViizualizer 或 SQL Developer 将 1 table 从 1 模式复制到另一个不存在的 table 模式中。 此外,如果可能仅出于测试目的,将需要示例如何将 1 table 复制到同一模式中的另一个(不存在的 table)。

非常感谢任何帮助。

试过这个:

SELECT * 
INTO new_table 
FROM old_table;
ALTER TABLE old_table 
RENAME TO new_table;

在 DBVizuallizer 和 SQL Developer 中都不起作用,我不太确定我做了什么,因为它是从 w3school 复制粘贴的。

CTAS(Create Table As Select)是一个简单的选项:

SQL> show user
USER is "SCOTT"
SQL> create table dept_new as select * From dept;

Table created.

SQL> select * from dept_new;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON

SQL>

但是,如果新的 table 应该驻留在另一个模式中,那么您可能会遇到问题,因为您无法在其他模式中创建对象 除非这样做的特权。通常情况下,我们不需要,因为这需要 create any table 权限,而这不是您(或您的 DBA)应该轻易授予的权限。

SQL> connect scott/tiger
Connected.
SQL> create table mike.dept_new as select * From dept;
create table mike.dept_new as select * From dept
                                            *
ERROR at line 1:
ORA-01031: insufficient privileges


SQL> connect sys as sysdba
Enter password:
Connected.
SQL> grant create any table to scott;

Grant succeeded.

SQL> connect scott/tiger
Connected.
SQL> create table mike.dept_new as select * From dept;

Table created.

SQL>

假设另一个模式已经包含这样的 table(但它是空的),您只需复制数据。但是,这也不容易,因为 - 你猜怎么着? - 缺少特权。我们不应该弄乱其他用户的数据。

SQL> insert into mike.dept_new select * From dept;
insert into mike.dept_new select * From dept
                 *
ERROR at line 1:
ORA-01031: insufficient privileges


SQL>

这次我们不需要 DBA 来授予权限;其他用户 (mike) 可以这样做:

SQL> connect mike/lion
Connected.
SQL> grant insert on dept_new to scott;

Grant succeeded.

SQL> connect scott/tiger
Connected.
SQL> insert into mike.dept_new select * From dept;

4 rows created.

SQL>

另一种选择是从源模式导出数据并将其导入目标模式。首先,将其从 mike 中删除(如您所说,它不应该已经存在):

SQL> connect mike/lion
Connected.
SQL> drop table dept_new;

Table dropped.

SQL>

导出:

SQL> $exp scott/tiger file=dept.dmp tables=dept

Export: Release 11.2.0.2.0 - Production on Pon Pro 20 21:41:36 2021

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.


Connected to: Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
Export done in EE8MSWIN1250 character set and AL16UTF16 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)

About to export specified tables via Conventional Path ...
. . exporting table                           DEPT          4 rows exported
EXP-00091: Exporting questionable statistics.
Export terminated successfully with warnings.

导入:

SQL> $imp mike/lion file=dept.dmp

Import: Release 11.2.0.2.0 - Production on Pon Pro 20 21:41:50 2021

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.


Connected to: Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

Export file created by EXPORT:V11.02.00 via conventional path

Warning: the objects were exported by SCOTT, not by you

import done in EE8MSWIN1250 character set and AL16UTF16 NCHAR character set
import server uses AL32UTF8 character set (possible charset conversion)
. importing SCOTT's objects into MIKE
Import terminated successfully without warnings.

SQL>