MySQL 重新命名表的操作方法及注意事項

2022-09-21 23:33:10 字數 2788 閱讀 7309

使用 rename table 語句或 alter table 語句都可以對錶進行重新命名,基本語法如下:

# rename table 語法:

rename table

&   tbl_name to new_tbl_name

[, tbl_name2 to new_tbl_name2] ...

# alter table 語法:

alter table old_table rename new_table;

# 具體示例:

mysql> show tables;

+------------------+

| tables_in_testdb |

+------------------+

| tb1              |

| tb2         &nbwww.cppcns.comsp;    |

+------------------+

2 rows in set (0.00 sec)

mysql> rename table tb1 to new_tb1;

query ok, 0 row程式設計客棧s affected (0.03 sec)

mysql> alter table tb2 rename new_tb2;

query ok, 0 rows affected (0.04 sec)

mysql> show tables;

+------------------+

| tables_in_testdb |

+------------------+

| new_tb1          |

| new_tb2          |

+------------------+

2 rows in set (0.00 sec)

顯然易見,在執行重新命名表時,舊表(old_table_name)必須存在,而新錶(new_table_name)一定不存在。如果新錶  new_table_name 確實存在,該語句將失敗。

執行重新命名表的使用者必須具有原始 table 的 alter 和 drop 許可權,以及新 table 的 create 和 insert 許可權。與 alter table 不同,rename table 可以在單個語句中重新命名多個表:

rename table old_table1 to new_table1,pqmuroupa

old_table2 to new_table2,

old_table3 to new_table3;

若一次性重新命名多個表,則重新命名操作從左到右執行。因此,要交換兩個表名,可以執行此操作(假設中間表名稱為tmp_table且不存在):

rename table old_table to tmp_table,

new_table to old_table,

tmp_table to new_table;

通過重新命名表,我們還可以將乙個表從乙個資料庫移動到另乙個資料庫中,語法如下:

rename table current_db.tbl_name to other_db.tbl_name;

alter table current_db.tbl_name rename other_db.tbl_name;

# 拼接sql 實現將某個資料庫中的表全部轉移至另乙個資料庫中

select

concat( 'rename table old_db.', table_name, ' to new_db.', table_name, ';' ) 

from

information_schema.tables 

where

table_schema = 'old_db';

事實上,mysql 並沒有提供重命程式設計客棧名資料庫的操作,我們可以通過將某個庫的所有表都通過重新命名轉移的另乙個庫中,來間接實現重新命名庫,只是原庫仍然存在。

值得注意的是,重新命名操作是原子完成的,需要獲取該錶的元資料鎖,因此我們在執行 rename table 前,要確保該錶沒有活躍的事務且沒有被鎖定。因為只需更改元資料,所以對於大表重新命名也是很迅速的。此外,如果該錶具有觸發器,則無法將該錶通過重新命名方式轉移到另外乙個庫中。

其實,rename table 語句和 alter table 語句還是有部分區別的,查詢官方文件,主要有幾點如下:

雖然重新命名操作快捷迅速,但實際生產場景中,對於表的重新命名還是要慎重考慮,也許你的重新命名操作沒問題,但後續物件之間的依賴呼叫可能出現問題。比如你將乙個表 tb1 重新命名為 new_tb1,若有檢視及函式依賴 tb1 ,並且你沒及時修改這些檢視及函式的話,那麼再次呼叫這些檢視和函式就可能報錯 tb1 不存在,因為在這些檢視及函式的定義中,仍用的是 tb1 的名稱。此外重新命名表或檢視後,要注意使用者許可權問題,如果有顯式指定某個使用者對該錶的許可權,則需要重新賦予對新錶的許可權。若表中存在外來鍵等約束時,執行重新命名操作也要格外小心,做好檢查。

本篇文章主要介紹了重新命名表的操作方法及注意事項,將本文的重點總結如下:

rename table 語句和 alter table 語句都可以對錶進行重新命名,二者稍有區別,更推薦用 rename table 語句。

重新命名操作需要獲取元資料鎖,執行前要確保無活躍事務占用。

通過重新命名表,可以將乙個表從乙個資料庫轉移到另乙個資料庫中,間接實現重新命名資料庫。

實際生產場景,重新命名表要慎重考慮,特別是存在檢視及函式依賴的。

重新命名操作執行完成後,要檢查使用者許可權及相關依賴問題,及時將依賴關係中的表名改為新的表名。

若表中存在觸發器或外來鍵等約束,重新命名時要格外注意。

重新命名操作一般在秒級完成,若執行時間過長,請檢查鏈結狀態。

mysql 83重新命名表 MySQL命令

mysql 是乙個關係型資料庫,存在表的概念.結構,資料庫可以存放多張表,每個表裡可以存放多個字段,每個字段可以存放多個記錄.phpstudy使用終端開啟資料庫的命令列 密碼 root 資料庫檢視資料庫的指令 show databases 建立資料庫 create database name 刪除資...

oracl重新命名表和對複製表資訊的操作

oracle修改表名 alter tabletable name rename to new table name oracle根據查詢插入資料 insert是t sql中常用語句,insert into table field1,field2,values value1,value2,這種形式的在...

mysql重新命名表,建立外來鍵,增 刪 改列名例項

mysql重新命名表,建立外來鍵,增 刪 改列名例項 增加到某個字段之後 alter table tb nippon mms info add province varchar 50 default null after retcode alter table tb nippon mms info ...