MySQL 使用SQL語(yǔ)句修改表名的實(shí)現(xiàn)
MySQL中可以使用rename table這個(gè)SQL語(yǔ)句來(lái)修改表名。
rename table這個(gè)SQL語(yǔ)句來(lái)修改表名的基本語(yǔ)法是:
RENAME TABLE <舊表名> TO <新表名>;
我們來(lái)把test表修改為test1表。
1、首先查看一下當(dāng)前數(shù)據(jù)庫(kù)中有哪些表。
mysql> show tables; +-------------------+ | Tables_in_cainiao | +-------------------+ | test | | test2 | +-------------------+ 2 rows in set (0.00 sec)
2、執(zhí)行重命名操作,把test修改為test1。
mysql> rename table test to test1; Query OK, 0 rows affected (0.08 sec)
3、再次查看一下結(jié)果。
mysql> show tables; +-------------------+ | Tables_in_cainiao | +-------------------+ | test1 | | test2 | +-------------------+ 2 rows in set (0.00 sec)
補(bǔ)充:MySQL中修改表名,表屬性名等的操作
alter table 表名 change 原列名 新列名 類型; --修改表的列屬性名
alter table 表名 modify 列名 類型 ; --修改表的類類型
alter table 表名 drop 列名; --刪除表的某一列
alter table 表名 add 列名 類型;--添加某一列
alter table 表名 rename 新表名; --修改表名
補(bǔ)充:MYSQL批量修改表前綴與表名sql語(yǔ)句
修改表名
ALTER TABLE 原表名 RENAME TO 新表名;
一句SQL語(yǔ)句只能修改一張表
show tables;
1.
SELECT CONCAT( 'ALTER TABLE ', table_name, ' RENAME TO db_', substring(table_name, 4), ';' ) FROM information_schema. TABLES WHERE table_name LIKE 'ct%';
批量復(fù)制一下到Notepad++中,只保留sql語(yǔ)句,再?gòu)?fù)制到mysql中執(zhí)行
2.php腳本批量修改mysql數(shù)據(jù)庫(kù)表前綴
<?php //設(shè)置好相關(guān)信息 $dbserver='localhost';//連接的服務(wù)器一般為localhost $dbname='corethink';//數(shù)據(jù)庫(kù)名 $dbuser='root';//數(shù)據(jù)庫(kù)用戶名 $dbpassword='root';//數(shù)據(jù)庫(kù)密碼 $old_prefix='ct_';//數(shù)據(jù)庫(kù)的前綴 $new_prefix='new_';//數(shù)據(jù)庫(kù)的前綴修改為 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; } //取得數(shù)據(jù)庫(kù)內(nèi)所有的表名 $result = mysql_list_tables($dbname); if (!$result){ print "DB Error, could not list tablesn"; print 'MySQL Error: ' . mysql_error(); exit; } //把表名存進(jìn)$data while ($row = mysql_fetch_row($result)) { $data[] = $row[0]; } //過(guò)濾要修改前綴的表名 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 數(shù)據(jù)表前綴:.$old_prefix."<br>".已經(jīng)修改為:.$new_prefix."<br>"; }else{ print 您的數(shù)據(jù)庫(kù)表的前綴.$old_prefix.輸入錯(cuò)誤。請(qǐng)檢查相關(guān)的數(shù)據(jù)庫(kù)表的前綴; if ( mysql_free_result($result) ){ return true; } } ?>
由于mysql_list_tables方法已經(jīng)過(guò)時(shí),運(yùn)行以上程序時(shí)會(huì)給出方法過(guò)時(shí)的提示信息
Deprecated: Function mysql_list_tables() is deprecated in … on line xxx
在php.ini中設(shè)置error_reporting,不顯示方法過(guò)時(shí)提示信息
error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED
3.批量刪除表
SELECT CONCAT( 'drop table ', table_name, ';' ) FROM information_schema. TABLES WHERE table_name LIKE 'uc_%';
執(zhí)行查詢,會(huì)自動(dòng)生成出 drop table table_name這樣的SQL語(yǔ)句
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章

MySQL查詢結(jié)果復(fù)制到新表的方法(更新、插入)

mysql隨機(jī)查詢?nèi)舾蓷l數(shù)據(jù)的方法

MySQL中MAX()和MIN()函數(shù)的高效使用及技巧

redhat7.1 安裝mysql 5.7.10步驟詳解(圖文詳解)

如何更改Linux(CentOS)系統(tǒng)下的MySQL數(shù)據(jù)庫(kù)目錄位置

Lost connection to MySQL server at ''reading authorization p