如何在MySQL中将列名从StudentName更改为FirstName?
使用ALTER语句和CHANGE命令。现在,让我们先创建一个表 –
mysql> create table DemoTable
-> (
-> StudentName varchar(100),
-> Age int
-> );
Query OK, 0 rows affected (0.84 sec)
现在检查表的描述 –
mysql> desc DemoTable;
阅读更多:MySQL 教程
输出
它会生成以下输出 –
+----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | 默认值 | Extra |
+----------------+--------------+------+-----+---------+-------+
| StudentName | varchar(100) | YES | | NULL | |
| Age | int(11) | YES | | NULL | |
+----------------+--------------+------+-----+---------+-------+
2 rows in set (0.25 sec)
下面是使用ALTER的查询 –
mysql> alter table DemoTable
-> add column Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> add column CountryName varchar(100) after Age,
-> change column StudentName FirstName varchar(200);
Query OK, 0 rows affected (1.51 sec)
Records: 0 Duplicates: 0 Warnings: 0
现在再次检查表的描述 –
mysql> desc DemoTable;
输出
它会生成以下输出 –
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | 默认值 | Extra |
+-------------+--------------+------+-----+---------+----------------+
| FirstName | varchar(200) | YES | | NULL | |
| Age | int(11) | YES | | NULL | |
| CountryName | varchar(100) | YES | | NULL | |
| Id | int(11) | NO | PRI | NULL | auto_increment |
+-------------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)