Sub topic 2
Anatomy of an `ALTER TABLE` Statement
ALTER TABLE changes the definition of an existing table. This chapter assumes that you know basic SELECT, INSERT, UPDATE, and DELETE, and that you understand tables, columns, primary keys, and foreign keys.
ALTER TABLE is a DDL statement. DDL, or data definition language, changes database objects such as tables and constraints. DML, or data manipulation language, changes the rows inside those objects. For example, UPDATE changes row values, while ALTER TABLE changes which columns exist.
The basic structure
A typical statement has this shape:
ALTER TABLE table_name
action;The table_name identifies the table to change. The action says what to change. Different actions have different additional clauses.
flowchart TD
A["ALTER TABLE"] --> B["table name"]
B --> C["action"]
C --> D["object details"]
D --> E["semicolon"]The action is part of the statement's meaning:
| Action | Purpose | Example |
|---|---|---|
| `ADD COLUMN` | Creates a column | `ADD COLUMN email VARCHAR(255)` |
| `DROP COLUMN` | Removes a column and its data | `DROP COLUMN email` |
| `ALTER COLUMN` | Changes a property or type | `ALTER COLUMN name TYPE VARCHAR(200)` |
| `RENAME COLUMN` | Changes a column name | `RENAME COLUMN name TO full_name` |
| `ADD CONSTRAINT` | Adds a rule | `ADD CONSTRAINT ... UNIQUE (...)` |
Reading an `ALTER TABLE` statement
This PostgreSQL-style example adds a nullable column:
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
ALTER TABLE customers
ADD COLUMN email VARCHAR(255);Read the second statement in order:
ALTER TABLEselects the kind of operation.customersselects the table.ADD COLUMNselects the action.email VARCHAR(255)defines the new column and its data type.
Because the statement does not specify NOT NULL, existing and future rows may contain NULL in email. NULL means that a value is missing or unknown. It is different from an empty string.
Some database systems allow several actions in one statement:
ALTER TABLE customers
ADD COLUMN phone VARCHAR(30),
ADD COLUMN created_at TIMESTAMP;Check your database documentation before using this form. Action syntax and support for multiple actions differ between PostgreSQL, MySQL, SQL Server, Oracle, and SQLite.
`ALTER TABLE` versus DML
An UPDATE changes stored values without changing the table's shape:
UPDATE customers
SET name = 'Ada Lovelace'
WHERE id = 1;An ALTER TABLE changes the shape itself:
ALTER TABLE customers
ADD COLUMN status VARCHAR(20);Adding a column can still affect application behavior. Queries using SELECT *, object-relational mappers, exports, and validation code may observe the new column.
Dialect-sensitive syntax
The general structure is portable, but the action syntax may not be. PostgreSQL uses ALTER COLUMN ... TYPE to change a type. MySQL commonly uses MODIFY COLUMN, and SQL Server uses ALTER COLUMN. SQL Server also commonly omits the COLUMN keyword in ADD.
Identify the database engine and version before copying an ALTER TABLE script between systems.
Exercise
In ALTER TABLE customers ADD COLUMN email VARCHAR(255), what are the table name, action, column name, and data type?
Show answer
The table name is customers. The action is ADD COLUMN. The column name is email, and its data type is VARCHAR(255).