Simple MySQL operation-alter table change table structure

Source: Internet
Author: User

We know that,MySQLDatabases are often combined with PHP to develop powerful applications. So for those who are new to PHP programming, it is necessary to understand some simple MySQL database operations. In this article, we useAlter tableStatement to modify the table structure. Next let's take a look at this part.

1. Add Columns

Alter table tbl_name add col_name type

For example, add a weight column to the pet table,

Mysql> alter table pet add weight int;

2. Delete Columns

Alter table tbl_name drop col_name

For example, delete the weight column in the pet table.

Mysql> alter table pet drop weight;

3. Change Columns

It can be divided into changing column attributes and changing column names.

Change the attribute of a column -- Method 1:

Alter table tbl_name modify col_name type

For example, changing the weight type

Mysql> alter table pet modify weight varchar (30 );

Change the attribute of a column -- Method 2:

Alter table tbl_name change old_col_name col_name type

For example, changing the weight type

Alter table pet change weight varchar (30 );

Change the column Name:

Alter table tbl_name change old_col_name col_name

For example, change the weight name in the pet table:

Mysql> alter table pet change weight wei;

4. Change the table name

Alter table tbl_name rename new_tbl

For example, rename the pet table to animal

Mysql> alter table pet rename animal;

This article introduces how to change the table structure of the MySQL database alter table. Next, we will introduce some simple operation methods for the MySQL database. I hope this article will help you gain some benefits!

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.