Add prefix to tables in MySQL database?

Hi,

Is there a way to add a prefix to all existing tables in a MySQL database? Lets say I want to add the prefix 'abc' in front of each table name in the database.
fcruz5Asked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
you can rename a table in MySQL:
http://dev.mysql.com/doc/refman/5.0/en/alter-table.html
ALTER TABLE tablename RENAME TO new_tablename;

so, as you posted in php, all you have to do is to get the list of the tables:
SHOW TABLES

and for each table run the above ALTER TABLE statement, filling in the table names...
0
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
quick and dirty php code:
<?php
  // connect code ... 
  mysql_connect( ... ); 
  // fetch table listing
  $rows = mysql_query("SHOW TABLES");
  while ($row = mysql_fetch_array($rows"))
  {
    $table_name = $row[0];
    mysql_query("ALTER TABLE $table_name RENAME TO abc$table_name "); 
  } 
?>

Open in new window

0
 
fcruz5Author Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.