Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a prefix name from every table name in a mysql database

I have a joomla mysql database with a table name prefix of "jos_" on all of my table names. But I would like to remove it from all of my tables. I understand how to rename each table, one at a time, but I have 600 tables. Is there an easy to run a sql query to do this.

If someone has a solution, could you please post the exact sql query I can use?

like image 614
Abby Avatar asked Jun 19 '11 17:06

Abby


1 Answers

You can generate the necessary statements with a single query:

select 'RENAME TABLE ' || table_name ||  ' TO ' || substr(table_name, 5) ||';'
from information_schema.tables

Save the output of that query to a file and you have all the statements you need.

Or if that returns 0s and 1s rather the statemenets, here's the version using concat instead:

select concat('RENAME TABLE ', concat(table_name, concat(' TO ', concat(substr(table_name, 5), ';'))))
from information_schema.tables;
like image 144
a_horse_with_no_name Avatar answered Sep 23 '22 02:09

a_horse_with_no_name