SQL statement concise tutorial for linux --- ALTER TABLE, concise tutorial --- alter

Source: Internet
Author: User

SQL statement concise tutorial for linux --- ALTER TABLE, concise tutorial --- alter

After a table is created in a database, we often find that the structure of the table needs to be changed. Common changes are as follows:

  • Add a column
  • Delete a column
  • Change Field name
  • Change the Data Type of the column

The changes listed above are not all possible changes.ALTER TABLEIt can also be used for other changes, such as changing the definition of the primary key.

ALTER TABLESyntax:

Alter table "table_name"
[Change method];

The detailed Writing Method of [change method] varies depending on what we want to achieve. In the changes listed above, the [change method] is as follows:

  • ADD a column: ADD "column 1" "column 1 data type"
  • Delete a field: DROP "column 1"
  • CHANGE Field name: CHANGE "original column name" "new column name" "new column name data type"
  • Change the Data Type of the column: MODIFY "column 1" "New data type"

We useCREATE TABLECreated on one pageCustomerTable as an example:

Customer table

Column name Data Type
First_Name Char (50)
Last_Name Char (50)
Address Char (50)
City Char (50)
Country Char (25)
Birth_Date Datetime

First, we need to add a field named "Gender. This can be achieved using the following commands:

Alter table Customer ADD Gender char (1 );

The table architecture after this command is executed is:

Customer table

Column name Data Type
First_Name Char (50)
Last_Name Char (50)
Address Char (50)
City Char (50)
Country Char (25)
Birth_Date Datetime
Gender Char (1)

Next, we will rename the "Address" field to "Addr ". This can be achieved using the following commands:

Alter table Customer CHANGE Address Addr char (50 );

The table architecture after this command is executed is:

Customer table

Column name Data Type
First_Name Char (50)
Last_Name Char (50)
Addr Char (50)
City Char (50)
Country Char (25)
Birth_Date Datetime
Gender Char (1)

Next, we will change the data type in the "Addr" field to char (30 ). This can be achieved using the following commands:

Alter table Customer MODIFY Addr char (30 );

The table architecture after this command is executed is:

Customer table

Column name Data Type
First_Name Char (50)
Last_Name Char (50)
Addr Char (30)
City Char (50)
Country Char (25)
Birth_Date Datetime
Gender Char (1)

Finally, we will delete the "Gender" field. This can be achieved using the following commands:

Alter table Customer DROP Gender;

The table architecture after this command is executed is:

Customer table

Column name Data Type
First_Name Char (50)
Last_Name Char (50)
Addr Char (30)
City Char (50)
Country Char (25)
Birth_Date Datetime


Linux has been tested as follows:









Reprinted, please note: Xiao Liu


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.