我們能否改變 MySQL 中的列順序?


是的,我們可以更改列的順序。可以使用 ALTER 命令和 AFTER 設定單個列的新順序。我們首先建立一個表 -

mysql> create table DemoTable
   -> (
   -> `Student_Key_Age` int,
   -> `Student_Key_Name` varchar(20),
   -> `Student_Key_CountryName` varchar(20)
   -> );
Query OK, 0 rows affected (0.64 sec)

以下是用於更改列順序的查詢 -

mysql> alter table DemoTable modify column `Student_Key_Age` int after `Student_Key_Name`;
Query OK, 0 rows affected (1.15 sec)
Records: 0 Duplicates: 0 Warnings: 0

讓我們再次查看錶說明 -

mysql> desc DemoTable;

這會產生以下輸出。如你所見,列的順序發生了改變 -

+-------------------------+-------------+------+-----+---------+-------+
| Field                   | Type        | Null | Key | Default | Extra |
+-------------------------+-------------+------+-----+---------+-------+
| Student_Key_Name        | varchar(20) | YES  |      | NULL   |       |
| Student_Key_Age         | int(11)     | YES  |      | NULL   |       |
| Student_Key_CountryName | varchar(20) | YES  |      | NULL   |       |
+-------------------------+-------------+------+-----+---------+-------+
3 rows in set (0.11 sec)

更新日期: 18-Dec-2019

217 檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告