如何更改MySQL表中现有列的名称?
我们可以通过使用CHANGE语句和ALTER语句来更改MySQL表中特定现有列的名称。它的语法如下-
语法
ALTER TABLE table_name CHANGE old_column_name new_column_name datatype;
这里,
table_name是我们要从中删除列的表的名称。
Old_column_name是要更改的列的名称。
new_column_name是必须赋予旧列的列名。
示例
在此示例中,我们将表 “ student_info”的列 “ id” 的名称更改为 “ studentid ”,如下所示:
mysql> Select * from Student_info;+------+---------+------------+------------+
| id | Name | Address | Subject |
+------+---------+------------+------------+
| 101 | YashPal | Amritsar | History |
| 105 | Gaurav | Chandigarh | Literature |
| 130 | Ram | Jhansi | Computers |
| 132 | Shyam | Chandigarh | Economics |
| 133 | Mohan | Delhi | Computers |
+------+---------+------------+------------+
5 rows in set (0.02 sec)
mysql> ALTER TABLE Student_info CHANGE id studentid int;
Records: 0 Duplicates: 0 Warnings: 0
mysql> Select * from Student_info;
+-----------+---------+------------+------------+
| studentid | Name | Address | Subject |
+-----------+---------+------------+------------+
| 101 | YashPal | Amritsar | History |
| 105 | Gaurav | Chandigarh | Literature |
| 130 | Ram | Jhansi | Computers |
| 132 | Shyam | Chandigarh | Economics |
| 133 | Mohan | Delhi | Computers |
+-----------+---------+------------+------------+
5 rows in set (0.00 sec)
上面的结果集显示列“ id”的名称已更改为“ Studentid”。
以上是 如何更改MySQL表中现有列的名称? 的全部内容, 来源链接: utcz.com/z/356625.html