Adding a prefix to all tables in a MySQL database

By Joel Stein on December 14, 2011

Here’s a helpful tip for situations where you need to add a prefix to all of your database tables. Supposing that your database was called “my_database” and the prefix you want to add is “my_prefix”, execute the following query:

SELECT Concat('ALTER TABLE ', TABLE_NAME, ' RENAME TO my_prefix_', TABLE_NAME, ';')
FROM information_schema.tables
WHERE table_schema = 'my_database'

Your result set will be a bunch of queries that you can copy and paste into your favorite MySQL editor (Sequel Pro, phpMyAdmin, whatever). Just paste those in and execute, and you’re all done.