Summary: in this tutorial, you will learn how to use the MySQL
ALTER TABLE
statement to add a column, alter a column, rename a column, drop a column and rename a table.Setting up a sample table
Let’s create a table named
vehicles
for the demonstration:
MySQL ALTER TABLE
– Add columns to a table
The
ALTER TABLE ADD
statement allows you to add one or more columns to a table.1) Add a column to a table
To add a column to a table, you use the
ALTER TABLE ADD
syntax:
In this syntax:
table_name
– specify the name of the table that you want to add a new column or columns after theALTER TABLE
keywords.new_column_name
– specify the name of the new column.column_definition
– specify the datatype, maximum size, and column constraint of the new columnFIRST | AFTER column_name
specify the position of the new column in the table. You can add a column after an existing column (ATER column_name
) or as the first column (FIRST
). If you omit this clause, the column is appended at the end of the column list of the table.
The following example uses the
ALTER TABLE ADD
statement to add a column at the end of the vehicles
table:
This statement shows the column list of the
vehicles
table:
As shown clearly from the output, the column
model
has been added to the vehicles
table.2) Add multiple columns to a table
To add multiple columns to a table, you use the following form of the
ALTER TALE ADD
statement:
For example, this statement adds two columns
color
and note
to the vehicles
table:
This statement shows the new structure of the
vehicles
table:
MySQL ALTER TABLE
– Modify columns
1) Modify a column
Here is the basic syntax for modifying a column in a table:
It’s a good practice to view the attributes of a column before modifying it.
Suppose that you want to change the
note
column a NOT NULL
column with a maximum of 100 characters.
First, show the column list of the
vehicles
table:
Then, modify the
note
column:
Finally, show the column list of the
vehicles
table to verify the change:2) Modify multiple columns
The following statement allows you to modify multiple columns:
First, show the current columns of the
vehicles
table:
Second, use the
ALTER TABLE MODIFY
statement to modify multiple columns:
In this example:
- First, modify the data type of the
year
column fromINT
toSMALLINT
- Second, modify the
color
column by setting themaximum
length to20
, removing theNOT NULL
constraint, and changing its position to appear after themake
column.
Third, show the new column list of the
vehicles
table to verify the modifications:
MySQL ALTER TABLE
– Rename a column in a table
To rename a column, you use the following statement:
In this syntax:
- First, specify the name of the table to which the column belongs.
- Second, specify the column name and the new name followed by column definition after the
CHANGE COLUMN
keywords. - Third, use the
FIRST
orAFTER column_name
option to determine the new position of the column.
The following example uses the
ALTER TABLE CHANGE COLUMN
statement to rename the column note
to vehicleCondition
:
Let’s review the column list of the
vehicles
table:
MySQL ALTER TABLE
– Drop a column
To drop a column in a table, you use the
ALTER TABLE DROP COLUMN
statement:
In this syntax:
- First, specify the name of the table that you want to drop a column after the
ALTER TABLE
keywords. - Second, specify the name of the column that you want to drop after the
DROP COLUMN
keywords.
This example shows how to remove the
vehicleCondition
column from the vehicles
table:
MySQL ALTER TABLE
– Rename table
To rename a table, you use the
ALTER TABLE RENAME TO
statement:
In this syntax:
- First, specify the name of the table that you want to rename after the
ALTER TABLE
keywords. - Second, specify the new name for the table after the
RENAME TO
keywords.
This example renames the
vehicles
table to cars
:
In this tutorial, you have learned how to use the MySQL
ALTER TABLE
statement to add a column, modify a column, rename a column, drop a column and rename a table.
0 comments:
Post a Comment