如何重新排列MySQL列?
要重新排列MySQL列,请在showcreate命令的帮助下检查列的排列。语法如下-
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; 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)