Use SQL statements in MySQL to rename fields.
In MySQL, how does one use an SQL statement to rename a table field? We will use the alter table SQL statement.
Syntax for renaming a field: alter table <table Name> change <field Name> <new field Name> <field type>.
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!