Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In MySQL, how do I batch rename tables within a database?

So basically I have a joomla database in MySQL which has a bunch of tables that have the prefix 'jmla_'. I would like to rename all of these tables by replacing the 'jmla_' prefix with a 'jos_' prefix. Any ideas about how to do this with a simple SQL script or SQL query?

like image 270
jaguarhaus Avatar asked Apr 08 '12 21:04

jaguarhaus


2 Answers

SELECT  concat ('rename table ',table_name,' to ',table_name,'_old;')
FROM information_schema.tables
WHERE table_name like 'webform%'
  and table_schema='weiss_db_new'

will work.

like image 152
roy Avatar answered Nov 16 '22 15:11

roy


Run this statement:

SELECT 'rename table '||table_name||' to '||'jos'||substr(table_name,5)||';'
FROM information_schema.tables
WHERE table_name like 'jmla%'

This creates a script that will rename all the tables. Just copy & paste the output into your SQL client.

(You will need to change the || to MySQL's non-standard concatenation operator in case you are not running it in ANSI mode)

like image 8
a_horse_with_no_name Avatar answered Nov 16 '22 14:11

a_horse_with_no_name