MySQL bulk modify table prefix with table name SQL statement

Source: Internet
Author: User
Tags deprecated one table php script preg

Modify Table Name

ALTER table name RENAME to new table name;

A single SQL statement can only modify one table

Show tables;

1.

SELECT    CONCAT (        ',        table_name,        '  RENAME to Db_ ' ,         4 ),        '; '     ) from    information_schema. Tableswhere    'ct%';

Bulk copy to notepad++, keep only SQL statements, and then copy to MySQL to execute

2.PHP script to bulk modify MySQL database table prefix

<?PHP//set up relevant information.$dbserver ='localhost';//the server you are connecting to is typically localhost$dbname ='Corethink';//Database name$dbuser ='Root';//Database user name$dbpassword ='Root';//Database Password$old _prefix='ct_';//the prefix of the database$new _prefix='New_';//the database prefix is modified toif(!is_string ($dbname) | |!is_string ($old _prefix) | |is_string ($new _prefix)) {    return false;} if(!mysql_connect ($dbserver, $dbuser, $dbpassword)) {Print'Could not connect to MySQL'; Exit;}//get all the table names in the database$result =mysql_list_tables ($dbname);if(!$result) {Print"DB Error, could not list TABLESN"; Print'MySQL Error:'. Mysql_error (); Exit;}//Save the table name in $data  while($row =mysql_fetch_row ($result)) {$data []= $row [0];}//Filter the name of the table to modify the prefixforeach($data as$k =$v) {$preg= Preg_match ("/^ ($old _prefix{1}) ([a-za-z0-9_-]+)/ I", $v, $v 1); if($preg) {$tab _name[$k]= $v 1[2]; }}if($preg) {foreach($tab _name as$k =$v) {$sql='RENAME TABLE ''. $old _prefix. $v.'' to ''. $new _prefix. $v.'`';    mysql_query ($sql); } Print data table prefix:. $old _prefix."<br>". has been modified to:. $new _prefix."<br>"; }Else{Print the prefix of your database table. $old _prefix. Input error.      Please check the prefix of the related database table; if(Mysql_free_result ($result)) {return true; }}?>

Since the Mysql_list_tables method is obsolete, it will give the method outdated hints when running the above program.

Deprecated:function mysql_list_tables () is Deprecated in ... on line xxx

Set error_reporting in php.ini, do not display method obsolete prompt information

3. Deleting a table in bulk
SELECT    CONCAT (        ',        table_name        ,'; '     ) from    information_schema. Tableswhere    'uc_%';

Executes a query that automatically generates SQL statements such as the DROP table table_name

MySQL bulk modify table prefix with table name SQL statement

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.