patternsqlMinor
SQL command to remove prefix from a set of tables
Viewed 0 times
tablessqlremoveprefixcommandfromset
Problem
I am no DBA. I have a situation with a MySQL database (5.0.51a) where I want to remove a 'dr_' prefix to all tables having this prefix (about 110 tables). I could rename them one by one of course, but I was wondering whether there was an SQL command to perform this in one shoot?
To make it clear, a table called
To make it clear, a table called
dr_hjkd would have to be renamed hjkd. A table called rfefd would keep the same name. Thanks.Solution
Wow I answered a similar question a year ago.
Nevertheless, your question is unique.
Here it goes:
If you are renaming all tables in the database
Run this in the OS and capture it to a SQL file. Then, execute the SQL file.
If you are renaming all tables in all databases, here is the query you need:
Run this in the OS and capture it to a SQL file. Then, execute the SQL file.
Nevertheless, your question is unique.
Here it goes:
If you are renaming all tables in the database
mydb, here is the query you need:SELECT
CONCAT('ALTER TABLE ',db,'.',old_tblname,' RENAME ',db,'.',new_tblname,';')
FROM
(
SELECT
table_schema db,
table_name old_tblname,
substr(table_name,4) new_tblname
FROM
information_schema.tables
WHERE
SUBSTR(table_name,1,3)='dr_'
AND table_schema = 'mydb'
) A;Run this in the OS and capture it to a SQL file. Then, execute the SQL file.
mysql -u... -p... -AN -e"SELECT CONCAT('ALTER TABLE ',db,'.',old_tblname,' RENAME ',db,'.',new_tblname,';') FROM (SELECT table_schema db,table_name old_tblname,substr(table_name,4) FROM information_schema.tables WHERE SUBSTR(table_name,1,3)='dr_' AND table_schema = 'mydb') A" > BigRename.sql
mysql -u... -p... < BigRename.sqlIf you are renaming all tables in all databases, here is the query you need:
SELECT
CONCAT('ALTER TABLE ',db,'.',old_tblname,' RENAME ',db,'.',new_tblname,';')
FROM
(
SELECT
table_schema db,
table_name old_tblname,
substr(table_name,4) new_tblname
FROM
information_schema.tables
WHERE
SUBSTR(table_name,1,3)='dr_'
) A;Run this in the OS and capture it to a SQL file. Then, execute the SQL file.
mysql -u... -p... -AN -e"SELECT CONCAT('ALTER TABLE ',db,'.',old_tblname,' RENAME ',db,'.',new_tblname,';') FROM (SELECT table_schema db,table_name old_tblname,substr(table_name,4) FROM information_schema.tables WHERE SUBSTR(table_name,1,3)='dr_') A" > BigRename.sql
mysql -u... -p... < BigRename.sqlCode Snippets
SELECT
CONCAT('ALTER TABLE ',db,'.',old_tblname,' RENAME ',db,'.',new_tblname,';')
FROM
(
SELECT
table_schema db,
table_name old_tblname,
substr(table_name,4) new_tblname
FROM
information_schema.tables
WHERE
SUBSTR(table_name,1,3)='dr_'
AND table_schema = 'mydb'
) A;mysql -u... -p... -AN -e"SELECT CONCAT('ALTER TABLE ',db,'.',old_tblname,' RENAME ',db,'.',new_tblname,';') FROM (SELECT table_schema db,table_name old_tblname,substr(table_name,4) FROM information_schema.tables WHERE SUBSTR(table_name,1,3)='dr_' AND table_schema = 'mydb') A" > BigRename.sql
mysql -u... -p... < BigRename.sqlSELECT
CONCAT('ALTER TABLE ',db,'.',old_tblname,' RENAME ',db,'.',new_tblname,';')
FROM
(
SELECT
table_schema db,
table_name old_tblname,
substr(table_name,4) new_tblname
FROM
information_schema.tables
WHERE
SUBSTR(table_name,1,3)='dr_'
) A;mysql -u... -p... -AN -e"SELECT CONCAT('ALTER TABLE ',db,'.',old_tblname,' RENAME ',db,'.',new_tblname,';') FROM (SELECT table_schema db,table_name old_tblname,substr(table_name,4) FROM information_schema.tables WHERE SUBSTR(table_name,1,3)='dr_') A" > BigRename.sql
mysql -u... -p... < BigRename.sqlContext
StackExchange Database Administrators Q#13259, answer score: 9
Revisions (0)
No revisions yet.