SQL:删除所有可用表中的所有数据

时间:2009-05-06 22:41:40

标签: sql oracle

我使用oracle DB来维护30多个表,如何删除所有表中的所有数据?我只想删除数据但不删除表。

9 个答案:

答案 0 :(得分:32)

没有命令'ALTER TABLE XXX DISABLE ALL CONSTRAINTS'

我提议这个;

BEGIN
  FOR c IN (SELECT table_name, constraint_name FROM user_constraints WHERE constraint_type = 'R')
  LOOP
    EXECUTE IMMEDIATE ('alter table ' || c.table_name || ' disable constraint ' || c.constraint_name);
  END LOOP;
  FOR c IN (SELECT table_name FROM user_tables)
  LOOP
    EXECUTE IMMEDIATE ('truncate table ' || c.table_name);
  END LOOP;
  FOR c IN (SELECT table_name, constraint_name FROM user_constraints WHERE constraint_type = 'R')
  LOOP
    EXECUTE IMMEDIATE ('alter table ' || c.table_name || ' enable constraint ' || c.constraint_name);
  END LOOP;
END;

答案 1 :(得分:29)

生成一个脚本以截断(=删除所有行):

select 'truncate table ' || table_name || ';' from user_tables

然后执行脚本。

答案 2 :(得分:18)

要解决约束问题,这样的事情应该有效:

BEGIN

    FOR T in (SELECT table_name FROM user_tables) LOOP
      EXECUTE IMMEDIATE 'ALTER TABLE '||T.table_name||' DISABLE ALL CONSTRAINTS';
    END LOOP;

    FOR T in (SELECT table_name FROM user_tables) LOOP
      EXECUTE IMMEDIATE 'TRUNCATE TABLE '||T.table_name;
    END LOOP;

    FOR T in (SELECT table_name FROM user_tables) LOOP
      EXECUTE IMMEDIATE 'ALTER TABLE '||T.table_name||' ENABLE ALL CONSTRAINTS';
    END LOOP;
END;

答案 3 :(得分:4)

截断的潜在缺点是它可能在参照完整性约束上失败。因此,您首先要禁用外键约束,然后执行截断,然后重新启用约束。 克隆模式(exp和imp)的'plus'也可以删除并重新创建表空间(如果要删除所有数据,可能需要回收一些物理磁盘空间)。

答案 4 :(得分:2)

克隆架构然后删除旧表?

答案 5 :(得分:1)

我使用上面提到的答案创建了这个存储过程。 这完美无缺,没有任何错误或例外。

    create or replace PROCEDURE DELETE_ALL_DATA
AS 
cursor r1 is select * from user_constraints;
cursor r2 is select * from user_tables;
cursor r3 is select * from user_constraints;
cursor r4 is select * from user_tables;

BEGIN

    FOR c1 IN r1
  loop
    for c2 in r2
    loop
        begin
       if c1.table_name = c2.table_name and c1.status = 'ENABLED' THEN
        dbms_utility.exec_ddl_statement('alter table ' || c1.owner || '.' || c1.table_name || ' disable constraint ' || c1.constraint_name);
       end if;
        EXCEPTION
         WHEN NO_DATA_FOUND
           THEN
           continue;
         WHEN OTHERS 
           THEN
           continue;
           end;
    end loop;
  END LOOP;

    FOR T in (SELECT table_name FROM user_tables) LOOP
      begin
      EXECUTE IMMEDIATE 'TRUNCATE TABLE '||T.table_name;
      EXCEPTION
         WHEN NO_DATA_FOUND
           THEN
           continue;
         WHEN OTHERS 
           THEN
           continue;
           end;
    END LOOP;

    FOR c1 IN r3
  loop
    for c2 in r4
    loop
        begin
       if c1.table_name = c2.table_name and c1.status = 'DISABLED' THEN
        dbms_utility.exec_ddl_statement('alter table ' || c1.owner || '.' || c1.table_name || ' enable constraint ' || c1.constraint_name);
       end if;
        EXCEPTION
         WHEN NO_DATA_FOUND
           THEN
           continue;
         WHEN OTHERS 
           THEN
           continue;
           end;
    end loop;
  END LOOP;

    commit;
END DELETE_ALL_DATA;

答案 6 :(得分:1)

Andomar截断特定用户的所有表而不仅仅是当前用户的表的答案略有不同:

SELECT 'TRUNCATE TABLE ' || owner || '.' || table_name || ';' FROM all_tables WHERE owner = 'user/schema'

将上面的user/schema位替换为您感兴趣的用户/架构名称(引号之间)。

答案 7 :(得分:0)

删除oracle中所有表中的所有数据

DECLARE
  str VARCHAR2(100);
BEGIN
  FOR i IN
  (SELECT object_name FROM user_objects WHERE object_type='TABLE'
  )
  LOOP
    str := 'Truncate table '|| i.object_name;
    EXECUTE IMMEDIATE str;
    DBMS_OUTPUT.PUT_LINE('table data deleted :' || i.object_name);
  END LOOP;
END;

了解更多信息:http://www.oracleinformation.com/2014/10/delete-all-the-data-from-all-tables.html

答案 8 :(得分:-3)

这两行脚本是最好的

EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
GO

EXEC sp_MSForEachTable 'DELETE FROM ?'
GO