Solved

Add prefix to tables in MySQL database?

Posted on 2008-06-18
3
1,842 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 142

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 142

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

773 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