直接在 user_constraint 表中重命名 Oracle 约束有问题吗?

发布于 2024-08-12 05:18:39 字数 602 浏览 4 评论 0原文

使用 Oracle 10g,我需要重命名一堆 FK 约束,这些约束全部以 LITE 结尾,以包含 FK 前缀。

我的想法是(我已确保所有名称都足够短以容纳前缀):

DECLARE
  v_name VARCHAR2(30 BYTE);
  v_new_name VARCHAR2(30 BYTE);
  CURSOR c1 is select CONSTRAINT name from user_constraints where constraint_type = 'R' and constraint_name like '%_LITE';
BEGIN
   OPEN c1;
   LOOP
      FETCH c1 into v_name;
      EXIT when c1%NOTFOUND;
      v_new_name:= 'FK_' || v_name;
      update user_constraints SET constraint_name = v_new_name where constraint_name = v_name;
   END LOOP;
   close c1;
END;

有什么原因会导致不安全,而我应该创建 alter table 语句呢?

Using Oracle 10g, I need to rename a bunch of FK constraints which all end in LITE to include an FK prefix.

My thinking was (I've ensured all names are short enough to accommodate the prefix):

DECLARE
  v_name VARCHAR2(30 BYTE);
  v_new_name VARCHAR2(30 BYTE);
  CURSOR c1 is select CONSTRAINT name from user_constraints where constraint_type = 'R' and constraint_name like '%_LITE';
BEGIN
   OPEN c1;
   LOOP
      FETCH c1 into v_name;
      EXIT when c1%NOTFOUND;
      v_new_name:= 'FK_' || v_name;
      update user_constraints SET constraint_name = v_new_name where constraint_name = v_name;
   END LOOP;
   close c1;
END;

Any reason why that would be unsafe and I should have to create alter table statements instead?

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

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

发布评论

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

评论(2

只想待在家 2024-08-19 05:18:39

USER_CONSTRAINTS 是一个视图,您无法以普通用户身份更新它。编辑:即使 SYS 也无法做到这一点,并且对数据字典进行更新对我来说似乎是一个非常糟糕的主意。

更好地使用 ALTER TABLE xxx RENAME CONSTRAINT yyy 至 zzz;

USER_CONSTRAINTS is a view, you cannot update it as a normal user. EDIT: Even SYS cannot do that, and doing updates on the data dictionary seems like an incredibly bad idea to me.

Better use ALTER TABLE xxx RENAME CONSTRAINT yyy TO zzz;

软糖 2024-08-19 05:18:39

正如 ammoQ 所说,别想这样做!这是使用 ALTER TABLE 的等效代码:

BEGIN
   FOR r IN (select constraint_name 
             from user_constraints 
             where constraint_type = 'R'
             and constraint_name like '%_LITE'
            )
   LOOP
      EXECUTE IMMEDIATE 'ALTER TABLE ' || r.table_name 
         || ' RENAME CONSTRAINT ' || r.constraint_name 
         || ' TO FK_' ||v_new_name;
   END LOOP;
END;

As ammoQ says, don't even think about doing that! This is the equivalent code using ALTER TABLE:

BEGIN
   FOR r IN (select constraint_name 
             from user_constraints 
             where constraint_type = 'R'
             and constraint_name like '%_LITE'
            )
   LOOP
      EXECUTE IMMEDIATE 'ALTER TABLE ' || r.table_name 
         || ' RENAME CONSTRAINT ' || r.constraint_name 
         || ' TO FK_' ||v_new_name;
   END LOOP;
END;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文