cancel
Showing results for 
Search instead for 
Did you mean: 

How to rename UPPER CASE table and column names to lower case?

Former Member
5,357

We have a database with the original table and column names created in all UPPER CASE (influenced from back in the day when one of those other databases had to have upper case names...). I would like to clean it up by changing all these to lower case (much more readable, and consistent with all the newer tables).

But an ALTER...RENAME tells me the table or column already exists.

It feels pretty scary to rename to a slighlty different name and then back to the original in lowercase... what underlying and undetectable effects will this have...

Is there a safe way?

Thanks, Bill

Accepted Solutions (1)

Accepted Solutions (1)

Former Member

So, sounds like I haven't missed anything obvious. I guess my plan wll be: 1. Do an UNLOAD 2. Write a Stored Proc to use the catalog tables to pickup the uppercase names and then REPLACE the values in the script file. 3. Do the RELOAD.

And... get a nice tidy defragged database file in the process.

The DB is only 8GB in size, and we have a substantial server, so not really a big deal.

Thanks Volker and Mark.

MarkCulp
Participant

FWIW: You may be able to easily update the reload.sql script using a perl script similar to:

#!perl -n
s/^CREATE (.*\\s)?TABLE "([^"]+)"\\."([^"]+)"/'CREATE '.$1.'TABLE "'.$2.'"."'.lc($3).'"'/se;
print;

If you were to name the above script file 'lc_table_names.pl' then you would run:

perl lc_table_names.pl reload.sql > reload_lc.sql
Former Member

PERL script... should that be spelled PEARL? grn

I remember Breck writing years ago about people who own a hammer always use a hammer to solve every problem. That would be me: I know PowerBuilder and SQLA very well, and have ignored all other languages as a distraction. So I only have 2 hammers in my toolkit.

VolkerBarth
Contributor
0 Kudos

Now, the inevitable xkcd link...

Breck_Carter
Participant

> should that be spelled PEARL?

No, purl...

VolkerBarth
Contributor
0 Kudos

So you are basically asking for a REGEXP_REPLACE SQL function in order to have something more - though not necessarily something "much more readable":) - in the familiar toolkit?

Breck_Carter
Participant
0 Kudos

No, let's not make it more complex than necessary... let's just ask for ALTER RENAME to work without checking for equality.

VolkerBarth
Contributor
0 Kudos

Well, I guess I had forgot to add another ":)" to my only partially serious suggestion...

s/^CREATE (.*\\s)?TABLE "([^"]+)"."([^"]+)"/'CREATE '.$1.'TABLE "'.$2.'"."'.lc($3).'"'/se;

is not a regex I would claim to understand and certainly not one I could come up with myself...

That being said, for other (and simpler) use cases I would like to have a REGEXP_REPLACE(): If you can find patterns with REGEXP_SUBSTR(), it's sometimes unsatisfying if you cannot change them...

Answers (0)