So, we just found out that 254 tables in our Oracle DBMS have one column named “Foo” with the wrong length- Number(10) instead of Number(3).
That foo column is a part from the PK of the tables.
Those tables have other tables with forigen keys to it.
What I did is:
- backed-up the table with a temp table.
- Disabled the forigen keys to the table.
- Disabled the PK with the
foocolumn. - Nulled the
foocolumn for all the rows. - Restored all the above
But now we found out it’s not just couple of tables but 254 tables.
Is there an easy way, (or at least easier than this) to alter the columns length?
P.S. I have DBA permissions.
There’s an easier way to generate the scripts that you want, use the system tables
user_tablesanduser_constraintsto dynamically generate the DDL. The downside is that this requires downtime. Also note that I use thetruncatecommand rather thandelete, which should be faster.Assuming a simple table that looks like:
This unlovely looking query
Will produce the following:
Due to the column
stage, this won’t be done table by table but stage by stage so that all the constraints will be disabled at the same time, which will avoid problems. If you’re scared (I would be) then remove thedropof the_backuptables from the query; this means that whatever goes wrong you’re safe.If you’re running this in SQL*Plus you also want to include
whenever sqlerror exitso that if there’s a problem, for instance no more tablespace, you don’t truncate things that you haven’t backed-up. It might almost be worth running it stage by stage so that you know that everything has completed correctly.I would suggest testing this on a different user with a few tables to ensure that it does everything you need.