gpt4 book ai didi

sql - 如何删除db2中的所有表

转载 作者:行者123 更新时间:2023-12-03 18:39:09 27 4
gpt4 key购买 nike

我想删除带有外键的 db2 数据库中的所有表,而不是删除和重新创建。

最佳答案

如果您还想删除所有 View 、索引、外键等:

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);

关于sql - 如何删除db2中的所有表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8253964/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com