php實現批量修改表首碼的方法介紹~

來源:互聯網
上載者:User

標籤:blog   io   ar   os   使用   for   資料   div   on   

解決這個問題,網上通常有兩種方法,底部附第三種方法,按照官方文檔建議的方式來。

第一種方法就是批量產生alter table 的資料庫修改語句,然後複製到文本處理工具中,然後批量把首碼給修改成想要的,然後粘貼到mysql工具的執行視窗,這樣就可實現修改資料表首碼的功能;如下:

select concat(‘alter table‘,table_name,‘rename to‘,table_name) from information_schema.tables where table_name like ‘xx_%‘;

這晨xx就是你資料庫中舊的表首碼,在like後這部分就是指定把所有符合的表首碼都拼接成可獨立執行的alter table語句;

第二種方法是自己寫一個執行指令碼,然後運行這個指令碼,實現資料庫表首碼的批量轉換,如下:

<?php//設定好相關資訊echo ‘<meta charset="utf-8">‘;$dbserver=‘localhost‘;$dbname=‘aa‘;//替換成你的資料庫名$dbuser=‘root‘;//替換成你的資料庫使用者名稱$dbpassword=‘‘;//替換成你的資料庫密碼$old_prefix=‘bb_‘;//修改前的表首碼$new_prefix=‘aa_‘;//修改後的表首碼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 table\n";    print "MySQL Error: ".mysql_error();    exit;} //把表名加進$datawhile($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];        //$table_name[$k] = str_replace($old_prefix, ‘‘, $v);    }}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()這個方法官方不建議使用,所以,整個代碼,也就是這個部分做一下更改,其它部分,與上邊類似,如下:

<?php//設定好相關資訊echo ‘<meta charset="utf-8">‘;$dbserver=‘localhost‘;$dbname=‘aa‘;//替換成你的資料庫名$dbuser=‘root‘;//替換成你的資料庫使用者名稱$dbpassword=‘‘;//替換成你的資料庫密碼$old_prefix=‘bb_‘;//修改前的表首碼$new_prefix=‘aa_‘;//修改後的表首碼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;}//取得資料庫內所有表名(與第二種方法有差異的地方)$data = array();$sql = "use aa";//指定使用哪個資料庫mysql_query($sql);$sql = "SHOW TABLES";$result = mysql_query($sql);if(!$result){    echo "DB Error, could not list tables\n";    echo ‘MySQL Error: ‘ . mysql_error();    exit;}//把表名加進$datawhile($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];        //$table_name[$k] = str_replace($old_prefix, ‘‘, $v);    }}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;    }}

  

php實現批量修改表首碼的方法介紹~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.