bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/SQL/SQL Database
SQL•SQL Database

SQL ALTER TABLE Statement

Concept visual

SQL ALTER TABLE Statement

Pointer walk
two pointers
leftright102132436485116
left=0
right=6
1
3

Start at both ends

Overview

The ALTER TABLE

statement is used to add, delete, or modify columns in an existing table.

The ALTER TABLE

statement is also used to add and drop various constraints on an existing table.

Common

Alter Table

operations are:

Formula

Add column - Adds a new column to a table
Drop column - Deletes a column in a table
Rename column - Renames a column
Modify column - Changes the data type, size, or constraints of a column
Add constraint - Adds a new constraint
Rename table - Renames a table
ALTER TABLE - ADD Column

To add a column in a table, use the following syntax:

Syntax

Alter Table

table_name

Add

column_name datatype

;
The following SQL adds an "Email" column to the "Customers" table:

Example

ALTER TABLE Customers

ADD Email varchar(255);

Formula

ALTER TABLE - DROP COLUMN

To delete a column in a table, use the following syntax (notice that some database systems don't allow deleting a column):

Syntax

Alter Table

table_name

Drop Column

column_name

;
The following SQL deletes the "Email" column from the "Customers" table:

Example

ALTER TABLE Customers

DROP COLUMN Email;

Formula

ALTER TABLE - RENAME COLUMN

To rename a column in a table, use the following syntax:

Syntax

Alter Table

table_name

Rename Column

old_name to new_name

;

To rename a column in a table in SQL Server, use the following syntax:

Syntax for SQL Server:

EXEC sp_rename ' table_name.old_name ', ' new_name

', 'COLUMN';

Formula

ALTER TABLE - MODIFY Datatype

To modify the data type, size or constraints of a column in a table, use the following syntax:

Formula

Syntax for SQL Server / MS Access:

Alter Table

table_name

Alter Column

column_name new_datatype constraint

;

Syntax for MySQL / Oracle:

Alter Table

table_name

Modify

column_name new_datatype constraint

;
The following SQL modifies the size of the "Email" column to varchar(100), and we also add a NOT NULL

constraint:

Previous

SQL DROP TABLE Statement

Next

SQL Constraints