Use SQL statements in MySQL to rename fields.

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.