前言
最近接到工作中要求,就是停用数据库所有的外键约束,为此呢,去网络上搜索了一番,找到了两种写法,第一种写法呢来自。oracle_base网站。内容如下:
-- -----------------------------------------------------------------------------------
-- File Name :
-- Author : Tim Hall
-- Description : Disables all Foreign Keys belonging to the specified table, or all tables.
-- Call Syntax : @disable_fk (table-name or all) (schema-name)
-- Last Modified: 28/01/2001
-- -----------------------------------------------------------------------------------
SET PAGESIZE 0
SET FEEDBACK OFF
SET VERIFY OFF
SPOOL temp.sql
SELECT 'ALTER TABLE "' || a.table_name || '" DISABLE CONSTRAINT "' || a.constraint_name || '";'
FROM all_constraints a
WHERE a.constraint_type = 'R'
AND a.table_name = DECODE(Upper('&1'),'ALL',a.table_name,Upper('&1'))
AND a.owner = Upper('&2');
SPOOL OFF
-- Comment out following line to prevent immediate run
@temp.sql
SET PAGESIZE 14
SET FEEDBACK ON
SET VERIFY ON
说说我的理解,我只是看到这种写法的是通过spool的方式将可执行的sql语句写出来,然后再执行。由于小弟刚刚接触数据库,不太懂这里面的关于性能的问题。只是简单的说一下,在这里。
下面看第二种写法
--禁用脚本
SET SERVEROUTPUT ON SIZE 100000
BEGIN
for c in (select 'ALTER TABLE '||TABLE_NAME||' DISABLE CONSTRAINT '||constraint_name||' ' as v_sql from user_constraints where CONSTRAINT_TYPE='R') loop
DBMS_OUTPUT.PUT_LINE(C.V_SQL);
begin
EXECUTE IMMEDIATE c.v_sql;
exception when others then
dbms_output.put_line(sqlerrm);
end;
end loop;
for c in (select 'ALTER TABLE '||TNAME||' DISABLE ALL TRIGGERS ' AS v_sql from tab where tabtype='TABLE') loop
dbms_output.put_line(c.v_sql);
begin
execute immediate c.v_sql;
exception when others then
dbms_output.put_line(sqlerrm);
end;
end loop;
for c in (select 'ALTER trigger '||trigger_name ||' DISABLE ' AS v_sql from user_triggers ) loop
dbms_output.put_line(c.v_sql);
begin
execute immediate c.v_sql;
exception when others then
dbms_output.put_line(sqlerrm);
end;
end loop;
end;
/
这种写法呢,小弟是经过实际使用,并且成功了的,这个是我的网络上搜索得到了。(源出处,我也找不到了。兄弟如果你看了,联系我吧!我一定把你的名字写上。)经过这个脚本的启发,根据工作要求,我有写了下语句,虽然简陋,但是可用啊。
--删除数据的脚本
SET SERVEROUTPUT ON SIZE 100000
BEGIN
for c in (select 'TRUNCATE TABLE '||TABLE_NAME||' ' as v_sql from user_tables where table_name not in ('PERSONS_XK') ) loop
DBMS_OUTPUT.PUT_LINE(C.V_SQL);
begin
EXECUTE IMMEDIATE c.v_sql;
exception when others then
dbms_output.put_line(sqlerrm);
end;
end loop;
end;
/
剩下就是第三种写法了,是通过游标的方式.
------删除数据库内特定用户的表(测试通过)
set serveroutput on
declare
cursor cur_object is select object_type,object_name from user_objects;
c1 user_objects.object_type%type;
c2 user_objects.object_name%type;
sql_str varchar(500);
begin
----------------------drop all objects-------------
open cur_object;
loop
FETCH cur_object into a1;
exit when cur_object%notfound;
dbms_output.put_line('delete '||c1||' '||c2);
IF c1='TABLE' THEN
sql_str:='drop '||a1.object_type||' '||a1.object_name||' CASCADE CONSTRAINT';
ELSE
sql_str:='drop '||a1.object_type||' '||a1.object_name;
END IF;
BEGIN
EXECUTE IMMEDIATE sql_str;
EXCEPTION WHEN others then
dbms_output.put_line(sqlerrm);
END;
end loop;
close cur_object;
end;
/
将以上sql 生成脚本就可以在相应的schema 下面运行了。就能删除所有的表啦!
阅读(1649) | 评论(0) | 转发(0) |