In MySQL, how does one use an SQL statement to rename a table field? We will use the altertable SQL statement. For more information, see MySQL. how can I use the SQL statement to rename a field in the table? We will use the alter table SQL statement.
Syntax for renaming a field: alter table <表名> Change <字段名> <字段新名称> <字段的类型> .
Now we will try to rename the t_name field in the test table to the t_name_new field.
1. First, check the structure of the current test table.
Mysql> describe test;
+ ------------ + ------------- + ------ + ----- + --------- + ------- +
| Field | Type | Null | Key | Default | Extra |
+ ------------ + ------------- + ------ + ----- + --------- + ------- +
| T_id | int (11) | YES | NULL |
| T_name | varchar (20) | YES | NULL |
| T_password | char (32) | YES | NULL |
| T_birth | date | YES | NULL |
+ ------------ + ------------- + ------ + ----- + --------- + ------- +
4 rows in set (0.00 sec)
2. use the alter table statement to modify the field name
Mysql> alter table test change t_name t_name_new varchar (20 );
Query OK, 0 rows affected (0.11 sec)
Records: 0 Duplicates: 0 Warnings: 0
3. view the modified results
Mysql> describe test;
+ ------------ + ------------- + ------ + ----- + --------- + ------- +
| Field | Type | Null | Key | Default | Extra |
+ ------------ + ------------- + ------ + ----- + --------- + ------- +
| T_id | int (11) | YES | NULL |
| T_name_new | varchar (20) | YES | NULL |
| T_password | char (32) | YES | NULL |
| T_birth | date | YES | NULL |
+ ------------ + ------------- + ------ + ----- + --------- + ------- +
4 rows in set (0.00 sec)
Now, we can smoothly modify the field name in the table.
This article describes how to rename a field using SQL statements in MySQL. I hope it will help you. thank you!