我想用外键删除 db2 数据库中的所有表,而不删除和重新创建。
问问题
15700 次
3 回答
7
如果您还想删除所有视图、索引、外键等:
select 'drop index "' || TRIM(INDSCHEMA) || '"."' || TRIM(INDNAME) || '";'
from SYSCAT.INDEXES
where UNIQUERULE = 'D'
and INDSCHEMA = (select current schema from SYSIBM.SYSDUMMY1);
select 'alter table "' || TRIM(TABSCHEMA) || '"."' || TRIM(TABNAME) || '" drop foreign key "' || TRIM(CONSTNAME) || '";'
from SYSCAT.TABCONST
where TYPE = 'F'
and TABSCHEMA = (select current schema from SYSIBM.SYSDUMMY1)
select 'alter table "' || TRIM(TABSCHEMA) || '"."' || TRIM(TABNAME) || '" drop unique "' || TRIM(INDNAME) || '";'
from SYSCAT.INDEXES
where UNIQUERULE = 'U'
and INDSCHEMA = (select current schema from SYSIBM.SYSDUMMY1);
select 'alter table "' || TRIM(TABSCHEMA) || '"."' || TRIM(TABNAME) || '" drop primary key;'
from SYSCAT.INDEXES
where UNIQUERULE = 'P'
and INDSCHEMA = (select current schema from SYSIBM.SYSDUMMY1);
select 'drop table "' || TRIM(TABSCHEMA) || '"."' || TRIM(TABNAME) || '";'
from SYSCAT.TABLES
where TYPE = 'T'
and TABSCHEMA = (select current schema from SYSIBM.SYSDUMMY1);
select 'drop view "' || TRIM(TABSCHEMA) || '"."' || TRIM(TABNAME) || '";'
from SYSCAT.TABLES
where TYPE = 'V'
and TABSCHEMA = (select current schema from SYSIBM.SYSDUMMY1);
于 2015-09-11T09:18:51.803 回答
3
如果您在 Linux 或 Unix 环境中。
#!/bin/ksh
## load profile of your instance owner
db2 "connect to <db_name">
db2 -x "select tabschema,tabname from syscat.tables where type='T' and tabschema not like 'SYS%' with ur"|while read a b
do
db2 "load from /dev/null of del replace into $a.$b nonrecoverable"
done
return 0
此脚本将数据删除到表中。
于 2011-11-24T11:50:18.130 回答
2
db2 "Select 'DROP TABLE ', tabname, ';' from syscat.tables where owner='DBUSER'" >> filename
删除文件的第一行和最后一行并使用运行它,
db2 -tvf filename.
这样我们还可以保存我们删除的表的日志。
PS:确保一次,文件中只有您想要的表格。不要错误地删除一些系统表。
于 2012-10-29T07:13:58.063 回答