• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1867
  • Last Modified:

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.
0
fcruz5
Asked:
fcruz5
  • 2
2 Solutions
 
Guy Hengel [angelIII / a3]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]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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now