简体   繁体   English

如何将 Oracle 中一张表的所有数据传输到另一张表?

[英]How to transfer all data from one table to another table in Oracle?

How to transfer data from one table to another in oracle?如何在 oracle 中将数据从一个表传输到另一个表? All fields in both tables are identical.两个表中的所有字段都是相同的。

insert into second_table (column_1, column_2, column_3) 
values
select column_1, column_2, column_3
from first_table;
commit;

The above method will only write the data to the second table, but will not delete the data from the first table.上述方法只会将数据写入第二张表,不会删除第一张表中的数据。

Is there any function in oracle library that can solve this problem? oracle库中有没有可以解决这个问题的function?

As you can't do that in just one command, you'll have to use two: insert + delete .因为你不能只用一个命令来做到这一点,你必须使用两个: insert + delete

Note that syntax you posted is invalid;请注意,您发布的语法无效; you can't have values keyword in this context.在这种情况下,您不能使用values关键字。

Therefore:所以:

insert into second_table (column_1, column_2, column_3) 
select column_1, column_2, column_3
  from first_table;

truncate table first_table;

As truncate is considered to be a DDL, it implicitly commits so - you don't have to explicitly commit .由于truncate被认为是 DDL,因此它会隐式提交 - 您不必显式commit

However, note that truncate (or delete ) might fail if there are foreign key constraints which would prevent those rows to be removed from the first_table , and there's no on delete cascade specified.但是,请注意,如果存在会阻止从first_table中删除这些行的外键约束,并且没有指定on delete cascade ,则truncate (或delete可能会失败

Why wouldn't you just have a view of the origin table and use the WHERE clause to 'delete' the records you don't want?为什么您不只是查看原始表并使用 WHERE 子句“删除”您不想要的记录?

To expand on Littlefoot response you may want to consider using /*+ APPEND */ for the INSERT to speed things up.要扩展 Littlefoot 响应,您可能需要考虑使用 /*+ APPEND */ 来加快插入速度。

As for the constraints you can disable them if need be.至于约束,您可以根据需要禁用它们。 Here is an example that will get you started.这是一个可以帮助您入门的示例。 When done you should enable them if needed.完成后,如果需要,您应该启用它们。


DECLARE 
 sql_stmt varchar2(255);
 t1 pls_integer;
BEGIN
   t1 := dbms_utility.get_time; 
FOR c IN
    (
      SELECT * 
        FROM user_constraints c
       WHERE -- c.constraint_type = 'P' AND
          c.table_name = 'HOLIDAYS')
  LOOP
    sql_stmt:='ALTER TABLE '||c.table_name||
                      ' DISABLE CONSTRAINT '||c.constraint_name;
   dbms_output.put_line(sql_stmt);
   EXECUTE IMMEDIATE sql_stmt;
  END LOOP;

dbms_output.put_line((dbms_utility.get_time - t1)/100 || ' seconds');

END;  

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

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