Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2794670
  • 博文数量: 423
  • 博客积分: 7770
  • 博客等级: 少将
  • 技术积分: 4766
  • 用 户 组: 普通用户
  • 注册时间: 2006-11-09 11:58
个人简介

Oracle/DB2/Postgresql/Mysql/Hadoop/Greenplum/Postgres-xl/Mongodb

文章分类

全部博文(423)

文章存档

2019年(3)

2018年(6)

2017年(27)

2016年(23)

2015年(30)

2014年(16)

2013年(31)

2012年(73)

2011年(45)

2010年(14)

2009年(30)

2008年(30)

2007年(63)

2006年(32)

分类: Oracle

2016-08-10 17:06:37

 How to truncate all tables in a schema which contain foreign key constraints:

1. Connect as system to a database, however it is possible to perform these scripts from another account which has got enough privileges.
2. Prepare scripts for disabling and enabling foreign key constraints.
2.1. The first script includes the output of the following query:

SELECT 'ALTER TABLE'||' '||owner||'.'||table_name||' DISABLE CONSTRAINT '||constraint_name||' ;' FROM dba_constraints WHERE constraint_type = 'R' and owner='name of schema' and status = 'ENABLED';

Let's name the output of this query as disable_constraints.sql

2.2. The second script includes the output of a very similar query.

SELECT 'ALTER TABLE'||' '||owner||'.'||table_name||' ENABLE CONSTRAINT '||constraint_name||' ;' FROM dba_constraints WHERE constraint_type = 'R' and owner='name of schema' and status = 'ENABLED';

Let's name the output of this query as enable_constraints.sql

3. Prepare scripts for truncating all tables in the schema.
SELECT 'TRUNCATE TABLE '||OWNER||'.'||TABLE_NAME||' ;' FROM DBA_TABLES WHERE OWNER='name of schema';

Name the output of this query as truncate_tables.sql

4. Run these scripts in the following order:
@disable_constraints.sql
@truncate_tables.sql
@enable_constraints.sql

That's it.

Thanks,
Sergey.
阅读(1587) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~