ALTER TABLE Statement

SQL · Working with Tables

Theory

ALTER TABLE changes the structure of a table that already exists, while keeping its data. With it you can add a column, drop a column, rename a column or the table, change a column's data type or default, and add or drop constraints such as PRIMARY KEY, FOREIGN KEY, UNIQUE or CHECK. For example, ALTER TABLE employees ADD COLUMN hire_date DATE adds an empty column to every existing row.

Exact syntax varies: PostgreSQL uses ALTER COLUMN ... TYPE, MySQL uses MODIFY COLUMN, SQL Server uses ALTER COLUMN, and SQLite supports only a few actions (add, rename, drop column). Some changes are instant metadata updates, while others, like changing a type, rewrite every row and can lock a large table for a long time. Adding a constraint fails if existing rows already break it.

Why we use it

Requirements change after a table goes live: a new field is needed, a name was wrong, a rule must be enforced. ALTER TABLE evolves the table in place without dropping and reloading the data.

When to use it

Use ALTER TABLE for schema changes on existing tables, ideally through versioned migration scripts. For big type changes on huge tables, consider creating a new table and copying data, or run the change during a quiet window.

How it works

  1. ALTER TABLE employees ...
  2. Choose the action: ADD, DROP, RENAME, ALTER type, ADD CONSTRAINT
  3. Does existing data satisfy the change? Yes: Apply the change. No: Error: fix the data first.
  4. Rewrites every row? Yes: Expect locks and time on big tables. No: Metadata-only change, fast.
  5. Table has the new structure

Syntax

ALTER TABLE table_name
ADD | DROP | MODIFY column ...;

Example

ALTER TABLE employees ADD COLUMN hire_date DATE;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic