如何重新排列 MySQL 列?
mysqlmysqli database
要重新排列 MySQL 列,请借助 show create 命令检查列排列。语法如下 −
show create table yourTableName;
重新排列 MySQL 列的语法如下 −
alter table yourTableName change column yourColumnName yourColumnName dataType first
出于相同目的,您可以使用 after 关键字。语法如下 −
alter table yourTableName change column yourColumnName yourColumnName dataType after yourSpecificColumnName;
让我们首先检查已创建的表"AddColumn"的列排列 −
mysql> show create table AddColumn;
以下是输出 −
+-----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | AddColumn | CREATE TABLE `addcolumn` (`StudentId` varchar(50) DEFAULT NULL, `StudentName` varchar(300) DEFAULT NULL ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci| +-----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
现在,您可以重新排列 StudentId 之前的 StudentName 列。查询如下 −
mysql> alter table AddColumn change StudentName StudentName varchar(300) first; Query OK, 0 rows affected (1.28 sec) Records: 0 Duplicates: 0 Warnings: 0
以下是可用于检查 StudentName 是否为第一列的查询 −
mysql> desc AddColumn;
以下是显示列已成功重新排列的输出 −
+-------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+-------+ | StudentName | varchar(300) | YES | | NULL | | | StudentId | varchar(50) | YES | | NULL | | +-------------+--------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)