The SQL ALTER TABLE statement is the SQL command that makes changes to the definition of an SQL table.
Anytime you want to change the definition of an SQL table. For example, you could:
The SQL ALTER TABLE command is used as follows.
SQL ALTER TABLE Statement Syntax
SQL ALTER TABLE Statement Example 1 - Add a Column
The following example adds a new column into the person table. Before the operation takes place the following columns exists in the table:
Column Name | Datatype | Nullability |
person_id | INT | NOT NULL |
person_name | VARCHAR(20) | NOT NULL |
gender_code | CHAR(1) | NULL |
line_1_addr | VARCHAR(50) | NULL |
line_2_addr | VARCHAR(50) | NULL |
apartment_nbr | VARCHAR(50) | NULL |
city_name | VARCHAR(50) | NOT NULL |
state_code | CHAR(2) | NOT NULL |
zip_code | CHAR(9) | NOT NULL |
This ALTER TABLE Statement is executed:
Results from the execution of the SQL ALTER TABLE statement are:
Column Name | Datatype | Nullability |
person_id | INT | NOT NULL |
person_name | VARCHAR(20) | NOT NULL |
gender_code | CHAR(1) | NULL |
line_1_addr | VARCHAR(50) | NULL |
line_2_addr | VARCHAR(50) | NULL |
apartment_nbr | VARCHAR(50) | NULL |
city_name | VARCHAR(50) | NOT NULL |
state_code | CHAR(2) | NOT NULL |
zip_code | CHAR(9) | NOT NULL |
marital_status_code | CHAR(1) | NULL |
SQL ALTER TABLE Statement Example 2 - Alter a Column Datatype
The following example changes the datatype of an existing column in the person table. Before the operation takes place the following columns exists in the table:
Column Name | Datatype | Nullability |
person_id | INT | NOT NULL |
person_name | VARCHAR(20) | NOT NULL |
gender_code | CHAR(1) | NULL |
line_1_addr | VARCHAR(50) | NULL |
line_2_addr | VARCHAR(50) | NULL |
apartment_nbr | VARCHAR(50) | NULL |
city_name | VARCHAR(50) | NOT NULL |
state_code | CHAR(2) | NOT NULL |
zip_code | CHAR(9) | NOT NULL |
This ALTER TABLE Statement is executed:
Results from the execution of the SQL ALTER TABLE statement are:
Column Name | Datatype | Nullability |
person_id | INT | NOT NULL |
person_name | VARCHAR(50) | NOT NULL |
gender_code | CHAR(1) | NULL |
line_1_addr | VARCHAR(50) | NULL |
line_2_addr | VARCHAR(50) | NULL |
apartment_nbr | VARCHAR(50) | NULL |
city_name | VARCHAR(50) | NOT NULL |
state_code | CHAR(2) | NOT NULL |
zip_code | CHAR(9) | NOT NULL |
marital_status_code | CHAR(1) | NULL |
SQL ALTER TABLE Statement Example 3 - Drop a column
The following example removes an existing column in the person table. Before the operation takes place the following columns exists in the table:
Column Name | Datatype | Nullability |
person_id | INT | NOT NULL |
person_name | VARCHAR(50) | NOT NULL |
gender_code | CHAR(1) | NULL |
line_1_addr | VARCHAR(50) | NULL |
line_2_addr | VARCHAR(50) | NULL |
apartment_nbr | VARCHAR(50) | NULL |
city_name | VARCHAR(50) | NOT NULL |
state_code | CHAR(2) | NOT NULL |
zip_code | CHAR(9) | NOT NULL |
This ALTER TABLE Statement is executed:
Results from the execution of the SQL ALTER TABLE statement are:
Column Name | Datatype | Nullability |
person_id | INT | NOT NULL |
person_name | VARCHAR(50) | NOT NULL |
gender_code | CHAR(1) | NULL |
line_1_addr | VARCHAR(50) | NULL |
line_2_addr | VARCHAR(50) | NULL |
city_name | VARCHAR(50) | NOT NULL |
state_code | CHAR(2) | NOT NULL |
zip_code | CHAR(9) | NOT NULL |
Advertisements:
Infogoal.com is organized to help you gain mastery.
Examples may be simplified to facilitate learning.
Content is reviewed for errors but is not warranted to be 100% correct.
In order to use this site, you must read and agree to the
terms of use, privacy policy and cookie policy.
Copyright 2006-2020 by Infogoal, LLC. All Rights Reserved.