Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I rename a table in Oracle so that all foreign keys, constraints, triggers and sequences are updated and any existing data is preserved?

I need to rename a table in Oracle but I want to be sure that any foreign keys, constraints, triggers and sequences that reference the table are updated to use the new name.

How can I be sure that I have not broken anything?

Note that I want to preserve any existing data that the table contains.

like image 247
Warren Blumenow Avatar asked Feb 14 '12 15:02

Warren Blumenow


People also ask

What happens to indexes when you rename a table in Oracle?

When you rename a table, Oracle automatically transfers indexes, constraints, and grants on the old table to the new one. In addition, it invalidates all objects that depend on the renamed table such as views, stored procedures, function, and synonyms.

How do I update a constraint in Oracle?

You can't update the constraint itself. If you want to change the values (new lower or upper bounds), you need to drop and recreate it: alter table hr. employees drop constraint sal_min_max; alter table hr.


2 Answers

If you

ALTER TABLE old_table_name
 RENAME TO new_table_name;

all the existing constraints (foreign key and other constraints) and triggers will reference the newly renamed object. Sequences have no relationship to tables so there will be no impact on the sequences (though if you mean that you are referencing the sequence in a trigger on the table, the trigger will continue to reference the same sequence after the rename). Any stored procedures that you have written that reference the old table name, however, will need to be updated to reference the new table name.

Now, while the constraints and triggers will continue to work correctly, they will retain their original names. If you have naming conventions for these objects that you want to maintain after the table name, you'd need to do more. For example, if you want a row-level before insert trigger on table FOO to be named TRG_BI_FOO and you rename the table to BAR, you'd need to alter the trigger explicitly to change its name

ALTER TRIGGER trg_bi_foo
  RENAME TO trg_bi_bar;

Similarly, you'd need to rename your constraints and indexes

ALTER TABLE bar
  RENAME CONSTRAINT pk_foo TO pk_bar;
like image 132
Justin Cave Avatar answered Sep 22 '22 08:09

Justin Cave


It depends on what you mean by "any foreign keys, constraints, triggers and sequences that reference the table are updated to use the new name."

Any existing indexes, constraints, and triggers against the table being renamed will automatically reference the new name.

However, any naming conventions used for those objects won't automatically use the updated name. For example, if the primary key for TABLE_NAME is generally named TABLE_NAME_PK, renaming TABLE_NAME to NEW_TABLE_NAME won't automatically rename the primary key constraint to NEW_TABLE_NAME_PK.

What will need to be checked is code - packages, procedures, and functions - which referenced the old table name, as well as any triggers which referenced the old table name. Similarly, views against the old table name will break as well. The view ALL_DEPENDENCIES can help identify which of those objects need to be updated.

like image 32
Adam Musch Avatar answered Sep 20 '22 08:09

Adam Musch