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