Solved

Add prefix to tables in MySQL database?

Posted on 2008-06-18
3
1,844 Views
Last Modified: 2013-12-12
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
Comment
Question by:fcruz5
  • 2
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 21817348
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
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 500 total points
ID: 21817362
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
 

Author Closing Comment

by:fcruz5
ID: 31468585
Thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question