Oracle - 删除表约束而不删除表

发布于 2024-09-19 03:56:59 字数 193 浏览 8 评论 0原文

我正在对大型 Oracle 数据库进行批量迁移。第一步涉及重命名整个表负载,为以后删除它们做准备(但我现在需要保留其中的数据)。需要删除对它们的任何外键约束 - 它们根本不应该连接到数据库的其余部分。如果我现在删除它们,我可以级联约束,但重命名只会改变约束。

有没有一种方法可以删除 CASCADE CONSTRAINTS 将删除的所有约束而不删除表本身?

I'm doing some bulk migration of a large Oracle database. The first step of this involves renaming a whole load of tables as a preparation for dropping them later (but I need to keep the data in them around for now). Any foreign key constraints on them need to be dropped - they shouldn't be connected to the rest of the database at all. If I were dropping them now I could CASCADE CONSTRAINTS, but rename simply alters the constraints.

Is there a way I can drop all of the constraints that CASCADE CONSTRAINTS would drop without dropping the table itself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的往事 2024-09-26 03:56:59

您可以使用动态 SQL 和数据字典来做到这一点:

begin
    for r in ( select table_name, constraint_name
               from user_constraints
               where constraint_type = 'R' )
    loop
        execute immediate 'alter table '|| r.table_name
                          ||' drop constraint '|| r.constraint_name;
    end loop;
end;

如果这些表由多个用户拥有,您将需要从 DBA_CONSTRAINTS 驱动并将 OWNER 包含在投影和执行的语句中。如果您不想接触所有表,恐怕您需要在 WHERE 子句中指定列表,除非它们的名称有某种模式。

You can do it with dynamic SQL and the data dictionary:

begin
    for r in ( select table_name, constraint_name
               from user_constraints
               where constraint_type = 'R' )
    loop
        execute immediate 'alter table '|| r.table_name
                          ||' drop constraint '|| r.constraint_name;
    end loop;
end;

If the tables are owned by more than one user you'll need to drive from DBA_CONSTRAINTS and include OWNER in the projection and the executed statement. If you want to touch less than all the tables I'm afraid you'll need to specify the list in the WHERE clause, unless there's some pattern to their names.

栀梦 2024-09-26 03:56:59

您可以禁用/重新启用约束而不删除它们。请查看这篇文章

You can disable/re-enable constraints without dropping them. Take a look at this article.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文