如何更改 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; Query OK, 0 rows affected (0.22 sec) 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'。
广告