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

來源:互聯網
上載者:User

方法一:使用sql語句修改mysql資料庫表首碼名

首先我們想到的就是用sql查詢語句來修改,這個方法也很方便,只需進入phpmyadmin後,在運行 SQL 查詢方塊中輸入如下語名就可以了。

ALTER TABLE 原表名 RENAME TO 新表名;

如:ALTER TABLE old_post RENAME TO new_post;

Sql查詢語句有一個缺點,那就是一句SQL語句只能修改一張資料庫的表名,如果你要精確修改某一張表,很好用。如果資料庫表很多的話,不推薦使用。有木有,批量修改多個資料庫表首碼名稱的方法類?有的,請看下面的介紹

 代碼如下 複製代碼

Select CONCAT( 'ALTER TABLE ', table_name, 'RENAME TO ', table_name,';' )
FROM information_schema.tables
Where table_name LIKE 'uc_%';

注意: like ‘uc_%’ 其中 uc_是你需要替換的表首碼.


下面這種代碼是今天遇到的,表頭前面是 db,但是沒有下橫線顯得很亂,於是批量將”dbtable_name”改成”db_table_name”
主要用的函數是mysql的substring函數

substring(str,pos)文法

substring(filed,m):截取filed欄位從第m個字元開始到結束的字串;

substring(filed,m,n):截取filed欄位從第m個字元開始的長度為n的字串;

str,字元

pos,從第幾個開始取

 代碼如下 複製代碼


Select CONCAT( 'ALTER TABLE ', table_name, 'RENAME TO db_', substring(table_name,3),';' )
FROM information_schema.tables
Where table_name LIKE 'db%';

重到結果


ALTER TABLE uc_aaa RENAME TO uc_aaa;
ALTER TABLE uc_bbb RENAME TO uc_bbb;

批量複製一下到記事本或者 et之類的編輯工具中,然後批量替換 RENAME TO uc 成 RENAME TO 你想要的表首碼
完成後 再執行.

這樣就完成了表名的批量修改拉…

 

方法二:php指令碼批量修改mysql資料庫表首碼

經測試,成功修改。如果你需要,請參考借鑒如下:

1、將下面的代碼複製到記事本,根據自己人情況修改好資料庫資訊,並儲存了editdata.php。

 代碼如下 複製代碼

<?php
//設定好相關資訊
$dbserver='localhost';//已連線的服務器一般為localhost
$dbname='newdata';//資料庫名
$dbuser='root';//資料庫使用者名稱
$dbpassword='123456';//資料庫密碼
$old_prefix='old_';//資料庫的首碼
$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];
                                //$tab_name[$k] = str_replace($old_prefix, '', $v);
                        }
  
                }
if($preg)
{
              //        echo '<pre>';
        //        print_r($tab_name);
        //        exit();
                //批量重新命名
                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;
            }
}
?>

大量刪除表也很簡單

 代碼如下 複製代碼


Select CONCAT( 'drop table ', table_name, ';' )
FROM information_schema.tables
Where table_name LIKE 'uc_%';

注意: like ‘uc_%’ 其中 uc_是你需要替換的表首碼.
執行查詢,會自動產生出 drop table table_name這樣的SQL語句.
然後複製 drop語句 可以執行刪除的操作了.

聯繫我們

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