如何在不复制数据的情况下创建 Oracle 表的副本?

发布于 2024-07-08 01:13:14 字数 117 浏览 9 评论 0 原文

我知道这样的说法:

create table xyz_new as select * from xyz;

它复制结构和数据,但是如果我只想要结构怎么办?

I know the statement:

create table xyz_new as select * from xyz;

Which copies the structure and the data, but what if I just want the structure?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

枕头说它不想醒 2024-07-15 01:13:15

你可以这样做
创建表 New_table as select * from Old_table where 1=2 ;
但要小心
你创建的表没有像old_table那样有任何Index、PK等。

You can do this
Create table New_table as select * from Old_table where 1=2 ;
but be careful
The table you create does not have any Index, PK and so on like the old_table.

梦过后 2024-07-15 01:13:15
    DECLARE
    l_ddl   VARCHAR2 (32767);
BEGIN
    l_ddl      := REPLACE (
                      REPLACE (
                          DBMS_LOB.SUBSTR (DBMS_METADATA.get_ddl ('TABLE', 'ACTIVITY_LOG', 'OLDSCHEMA'))
                        , q'["OLDSCHEMA"]'
                        , q'["NEWSCHEMA"]'
                      )
                    , q'["OLDTABLSPACE"]'
                    , q'["NEWTABLESPACE"]'
                  );

    EXECUTE IMMEDIATE l_ddl;
END; 
    DECLARE
    l_ddl   VARCHAR2 (32767);
BEGIN
    l_ddl      := REPLACE (
                      REPLACE (
                          DBMS_LOB.SUBSTR (DBMS_METADATA.get_ddl ('TABLE', 'ACTIVITY_LOG', 'OLDSCHEMA'))
                        , q'["OLDSCHEMA"]'
                        , q'["NEWSCHEMA"]'
                      )
                    , q'["OLDTABLSPACE"]'
                    , q'["NEWTABLESPACE"]'
                  );

    EXECUTE IMMEDIATE l_ddl;
END; 
热风软妹 2024-07-15 01:13:15

如果需要创建一个表(具有空结构)只是为了EXCHANGE PARTITION,最好使用“..FOR EXCHANGE..”子句。 不过,它仅适用于 Oracle 12.2 版本

CREATE TABLE t1_temp FOR EXCHANGE WITH TABLE t1;

如果正常 CTAS 操作未精确复制表结构,则可以在“交换分区”期间无缝地解决“ORA-14097”问题。 我发现 Oracle 丢失了原始表中的一些“DEFAULT”列和“HIDDEN”列定义。

ORA-14097: ALTER TABLE EXCHANGE 中的列类型或大小不匹配
分区

请参阅此内容以进一步阅读...

If one needs to create a table (with an empty structure) just to EXCHANGE PARTITION, it is best to use the "..FOR EXCHANGE.." clause. It's available only from Oracle version 12.2 onwards though.

CREATE TABLE t1_temp FOR EXCHANGE WITH TABLE t1;

This addresses 'ORA-14097' during the 'exchange partition' seamlessly if table structures are not exactly copied by normal CTAS operation. I have seen Oracle missing some of the "DEFAULT" column and "HIDDEN" columns definitions from the original table.

ORA-14097: column type or size mismatch in ALTER TABLE EXCHANGE
PARTITION

See this for further read...

累赘 2024-07-15 01:13:15

只需编写如下查询:

create table new_table as select * from old_table where 1=2;

其中 new_table 是您要创建的新表的名称,old_table 是您要复制其结构的现有表的名称,这将仅复制结构。

Simply write a query like:

create table new_table as select * from old_table where 1=2;

where new_table is the name of the new table that you want to create and old_table is the name of the existing table whose structure you want to copy, this will copy only structure.

初心 2024-07-15 01:13:15

WHERE 1 = 0 或类似的错误条件有效,但我不喜欢它们的外观。 Oracle 12c+ 的稍微干净一点的代码是


创建表栏 AS
选择 *
来自富
仅获取前 0 行;

存在相同的限制:仅将列定义及其可为空性复制到新表中。

WHERE 1 = 0 or similar false conditions work, but I dislike how they look. Marginally cleaner code for Oracle 12c+ IMHO is


CREATE TABLE bar AS
SELECT *
FROM foo
FETCH FIRST 0 ROWS ONLY;

Same limitations apply: only column definitions and their nullability are copied into a new table.

时光倒影 2024-07-15 01:13:15

使用 pl/sql Developer,您可以在 sql 工作区或对象资源管理器中右键单击 table_name,然后单击“view”,然后单击“view sql”,这会生成 sql 脚本来创建表以及所有约束、索引、分区等。

接下来,使用 new_table_name 运行脚本

Using pl/sql developer you can right click on the table_name either in the sql workspace or in the object explorer, than click on "view" and than click "view sql" which generates the sql script to create the table along with all the constraints, indexes, partitions etc..

Next you run the script using the new_table_name

滥情哥ㄟ 2024-07-15 01:13:15
SELECT * INTO newtable
FROM oldtable
WHERE 1 = 0;

使用另一个表的架构创建一个新的空表。 只需添加一个导致查询不返回任何数据的 WHERE 子句:

SELECT * INTO newtable
FROM oldtable
WHERE 1 = 0;

Create a new, empty table using the schema of another. Just add a WHERE clause that causes the query to return no data:

回心转意 2024-07-15 01:13:15

通过其他方式,您可以从下面列出的命令获取表创建的 ddl,并执行创建。

SELECT DBMS_METADATA.GET_DDL('TYPE','OBJECT_NAME','DATA_BASE_USER') TEXT FROM DUAL 
  • TYPETABLEPROCEDURE 等。

使用此命令您可以从数据库对象获取大部分 ddl。

In other way you can get ddl of table creation from command listed below, and execute the creation.

SELECT DBMS_METADATA.GET_DDL('TYPE','OBJECT_NAME','DATA_BASE_USER') TEXT FROM DUAL 
  • TYPE is TABLE,PROCEDURE etc.

With this command you can get majority of ddl from database objects.

对你再特殊 2024-07-15 01:13:15

您还可以

create table abc_new as select * from abc; 

先截断表abc_new。 希望这能满足您的要求。

you can also do a

create table abc_new as select * from abc; 

then truncate the table abc_new. Hope this will suffice your requirement.

蝶舞 2024-07-15 01:13:15

不带表格数据复制

create table <target_table> as select * from <source_table> where 1=2;

带表格数据复制

create table <target_table> as select * from <source_table>;

copy without table data

create table <target_table> as select * from <source_table> where 1=2;

copy with table data

create table <target_table> as select * from <source_table>;
习ぎ惯性依靠 2024-07-15 01:13:15
Create table target_table 
As
Select * 
from source_table 
where 1=2;

Source_table 是您想要复制其结构的表。

Create table target_table 
As
Select * 
from source_table 
where 1=2;

Source_table is the table u wanna copy the structure of.

惯饮孤独 2024-07-15 01:13:15
  1. 创建表 xyz_new 作为 select * from xyz;

-- 这将创建表并复制所有数据。

  • 从 xyz_new 中删除;
  • -- 这将具有相同的表结构,但复制的所有数据都将被删除。

    如果您想克服答案指定的限制:
    如何在不复制数据的情况下创建 Oracle 表的副本?

    1. create table xyz_new as select * from xyz;

    -- This will create table and copy all data.

    1. delete from xyz_new;

    -- This will have same table structure but all data copied will be deleted.

    If you want to overcome the limitations specified by answer:
    How can I create a copy of an Oracle table without copying the data?

    善良天后 2024-07-15 01:13:15

    上述任务可以通过两个简单的步骤完成。

    第 1 步:

    CREATE table new_table_name AS(Select * from old_table_name);
    

    上面的查询创建一个表的副本(也包含内容)。

    要获取结构,请使用删除表的内容。

    第 2 步:

    DELETE * FROM new_table_name.
    

    希望这能解决您的问题。 并感谢之前的帖子。 给了我很多感悟。

    The task above can be completed in two simple steps.

    STEP 1:

    CREATE table new_table_name AS(Select * from old_table_name);
    

    The query above creates a duplicate of a table (with contents as well).

    To get the structure, delete the contents of the table using.

    STEP 2:

    DELETE * FROM new_table_name.
    

    Hope this solves your problem. And thanks to the earlier posts. Gave me a lot of insight.

    久伴你 2024-07-15 01:13:14

    只需使用不会选择任何行的 where 子句:

    create table xyz_new as select * from xyz where 1=0;
    

    限制

    以下内容不会被复制到新表:

    • 序列
    • 触发器
    • 索引
    • 某些约束可能不会被复制
    • 物化视图日志

    这也不能处理分区


    Just use a where clause that won't select any rows:

    create table xyz_new as select * from xyz where 1=0;
    

    Limitations

    The following things will not be copied to the new table:

    • sequences
    • triggers
    • indexes
    • some constraints may not be copied
    • materialized view logs

    This also does not handle partitions


    九八野马 2024-07-15 01:13:14

    我使用了您经常接受的方法,但正如有人指出的那样,它不会重复约束(我认为除了 NOT NULL 之外)。

    如果您想复制完整的结构,更高级的方法是:

    SET LONG 5000
    SELECT dbms_metadata.get_ddl( 'TABLE', 'MY_TABLE_NAME' ) FROM DUAL;
    

    这将为您提供完整的创建语句文本,您可以根据需要进行修改以创建新表。 当然,您必须更改表的名称和所有约束。

    (您也可以在旧版本中使用 EXP/IMP 执行此操作,但现在更容易。)

    编辑添加
    如果您所在的表位于不同的架构中:

    SELECT dbms_metadata.get_ddl( 'TABLE', 'MY_TABLE_NAME', 'OTHER_SCHEMA_NAME' ) FROM DUAL;
    

    I used the method that you accepted a lot, but as someone pointed out it doesn't duplicate constraints (except for NOT NULL, I think).

    A more advanced method if you want to duplicate the full structure is:

    SET LONG 5000
    SELECT dbms_metadata.get_ddl( 'TABLE', 'MY_TABLE_NAME' ) FROM DUAL;
    

    This will give you the full create statement text which you can modify as you wish for creating the new table. You would have to change the names of the table and all constraints of course.

    (You could also do this in older versions using EXP/IMP, but it's much easier now.)

    Edited to add
    If the table you are after is in a different schema:

    SELECT dbms_metadata.get_ddl( 'TABLE', 'MY_TABLE_NAME', 'OTHER_SCHEMA_NAME' ) FROM DUAL;
    
    只等公子 2024-07-15 01:13:14
    create table xyz_new as select * from xyz where rownum = -1;
    

    避免一次又一次迭代,根据 1=2 的条件不插入任何内容

    create table xyz_new as select * from xyz where rownum = -1;
    

    To avoid iterate again and again and insert nothing based on the condition where 1=2

    可爱咩 2024-07-15 01:13:14

    使用 sql Developer 选择表并单击 DDL 选项卡

    当您在 sql 工作表中运行该代码时,您可以使用该代码创建一个没有数据的新表

    sqldeveloper 是 Oracle 的一款免费应用程序。

    如果表有序列或触发器,ddl 有时也会为您生成这些序列或触发器。 您只需要注意制作它们的顺序,并知道何时打开或关闭触发器即可。

    Using sql developer select the table and click on the DDL tab

    You can use that code to create a new table with no data when you run it in a sql worksheet

    sqldeveloper is a free to use app from oracle.

    If the table has sequences or triggers the ddl will sometimes generate those for you too. You just have to be careful what order you make them in and know when to turn the triggers on or off.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文