Code has been added to clipboard!

SQL ALTER TABLE

Reading time 2 min
Published Aug 9, 2017
Updated Nov 20, 2019

TL;DR – The ALTER TABLE statement in SQL is used to delete, add, or modify table columns. You can also use it to drop and add various table constraints.

Adding a new column to the table

Example
ALTER TABLE mytable_name
ADD mycolumn_name datatype;

Note: the new column will be added to the end of the table by default. You can add multiple columns by separating them with commas.

Deleting a column

Example
ALTER TABLE mytable_name
DROP COLUMN mycolumn_name;

Changing the data type of a particular column

Note: when changing the data type, make sure the old one and the new one are compatible. Otherwise, you might get conversion errors.

In MySQL / Oracle (< 10G)

Example
ALTER TABLE mytable_name
MODIFY COLUMN mycolumn_name datatype;

In SQL Server / MS Access

Example
ALTER TABLE mytable_name
ALTER COLUMN mycolumn_name datatype;

In Oracle 10G and later

Example
ALTER TABLE mytable_name
MODIFY mycolumn_name datatype;

Examples using a demo database

The Developers table

ID Name City Country
1 Tom Kurkutis New York USA
2 Ana Fernandez London UK
3 Antonio Indigo Paris France
4 Aarav Kaelin Delhi India
5 Andrew Tumota Miami USA
6 Basma Zlata Miami USA

Adding a column called BirthDate

Example
ALTER TABLE Developers
ADD BirthDate date;

The result

ID Name City Country BirthDate
1 Tom Kurkutis New York USA
2 Ana Fernandez London UK
3 Antonio Indigo Paris France
4 Aarav Kaelin Delhi India
5 Andrew Tumota Miami USA
6 Basma Zlata Miami USA

Changing the data type to year

Example
ALTER TABLE Developers
ALTER COLUMN BirthDate year;

Deleting the BirthDate column

Example
ALTER TABLE Developers
DROP COLUMN BirthDate;

The result

ID Name City Country
1 Tom Kurkutis New York USA
2 Ana Fernandez London UK
3 Antonio Indigo Paris France
4 Aarav Kaelin Delhi India
5 Andrew Tumota Miami USA
6 Basma Zlata Miami USA
Learn SQL
Introduction
Syntax
Data Types
Server Data Types
Commands
Commands List
Wildcards
Constraints
Aggregate Functions
Date Functions
Date Format
Injection
SQL Server Hosting
Views
Auto-incrementation
SQL Operators
AS
AND & OR
IN
BETWEEN
WHERE
GROUP BY
HAVING
ORDER BY
LIKE
NOT
NOT EQUAL
UNION
NULL
NOT NULL
DEFAULT
UNIQUE
FOREIGN KEY
PRIMARY KEY
CHECK
Indexes
ALTER TABLE
CREATE DATABASE
CREATE TABLE
DELETE
DROP
INSERT INTO SELECT
INSERT INTO
SELECT
SELECT DISTINCT
SELECT INTO
SELECT TOP
UPDATE
FULL OUTER JOIN
INNER JOIN
JOIN
LEFT JOIN
RIGHT JOIN
AVG()
COUNT
FIRST
LAST
MAX
MIN()
SUM()
LEN
UCASE
MID
NOW
ROUND
FORMAT
LOWER
CONVERT
ISNULL