MYSQL批量修改表首碼與表名sql語句

來源:互聯網
上載者:User

標籤:

修改表名

ALTER TABLE 原表名 RENAME TO 新表名;

一句SQL語句只能修改一張表

show tables;

1.

SELECT    CONCAT(        ‘ALTER TABLE ‘,        table_name,        ‘ RENAME TO db_‘,        substring(table_name, 4),        ‘;‘    )FROM    information_schema. TABLESWHERE    table_name LIKE ‘ct%‘;

批量複製一下到Notepad++中,只保留sql語句,再複製到mysql中執行

2.php指令碼批量修改mysql資料庫表首碼

 

<?php//設定好相關資訊$dbserver=‘localhost‘;//已連線的服務器一般為localhost$dbname=‘corethink‘;//資料庫名$dbuser=‘root‘;//資料庫使用者名稱$dbpassword=‘root‘;//資料庫密碼$old_prefix=‘ct_‘;//資料庫的首碼$new_prefix=‘new_‘;//資料庫的首碼修改為if ( !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;}//取得資料庫內所有的表名$result = mysql_list_tables($dbname);if (!$result){    print "DB Error, could not list tablesn";    print ‘MySQL Error: ‘ . mysql_error();    exit;}//把表名存進$data while ($row = mysql_fetch_row($result)) {    $data[] = $row[0];}//過濾要修改首碼的表名foreach($data as $k => $v){    $preg = preg_match("/^($old_prefix{1})([a-zA-Z0-9_-]+)/i", $v, $v1);    if($preg){        $tab_name[$k] = $v1[2];    }}if($preg){                     foreach($tab_name as $k => $v){        $sql = ‘RENAME TABLE `‘.$old_prefix.$v.‘` TO `‘.$new_prefix.$v.‘`‘;        mysql_query($sql);    }    print  資料表首碼:.$old_prefix."<br>".已經修改為:.$new_prefix."<br>";   }else{ print 您的資料庫表的首碼.$old_prefix.輸入錯誤。請檢查相關的資料庫表的首碼;      if ( mysql_free_result($result) ){        return true;    }}?>

由於mysql_list_tables方法已經過時,運行以上程式時會給出方法過時的提示資訊

Deprecated: Function mysql_list_tables() is deprecated in … on line xxx 

在php.ini中設定error_reporting,不顯示方法過時提示資訊 

error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED 

 

 3.大量刪除表
SELECT    CONCAT(        ‘drop table ‘,        table_name,        ‘;‘    )FROM    information_schema. TABLESWHERE    table_name LIKE ‘uc_%‘;

 

執行查詢,會自動產生出 drop table table_name這樣的SQL語句

 

 

 

 

MYSQL批量修改表首碼與表名sql語句

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.