views:

182

answers:

2

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?

+7  A: 

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;

ammoQ
+1: never EVER update the data dictionary directly (unless explicitely told by support)
Vincent Malgrat
+1, not to mention you don't know what relationships that constraint name is participating in within the dictionary.
DCookie
+1 from me........
Tony Andrews
And this is why I asked first :) (Of course I wouldn't have succeed anyhow)
Wade Williams
+4  A: 

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;
Tony Andrews