Abby Abby - 5 months ago 15
SQL Question

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?

Answer

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;