ALTER TABLE is the SQL statement that modifies the definition of an existing table without recreating it. It belongs to the data definition language (DDL) and is used to add, drop, rename or change columns, to add or remove constraints and, in many systems, to change storage options or partitioning. Unlike CREATE TABLE, which defines a new structure, ALTER TABLE evolves a structure that already holds data.
Because it acts on live tables, ALTER TABLE is central to schema migrations. The syntax is defined in the ISO SQL standard, but every database engine extends it and implements it differently, so the locking behavior and run time of the same statement can range from instant to hours.
What ALTER TABLE can change
ALTER TABLE accepts one or more actions that each change one aspect of the table definition. The general form is ALTER TABLE table_name action [, action ...], although some engines allow only one action per statement.
The table below lists the most common actions and their purpose.
| Action | Example clause | Purpose |
|---|---|---|
| Add column | ADD COLUMN email VARCHAR(255) | Store a new attribute |
| Drop column | DROP COLUMN fax | Remove an unused attribute |
| Change data type | ALTER COLUMN price TYPE NUMERIC(10,2) | Widen or convert values |
| Set default | ALTER COLUMN status SET DEFAULT 'new' | Fill values on future inserts |
| Add constraint | ADD CONSTRAINT fk_cust FOREIGN KEY ... | Enforce integrity rules |
| Rename | RENAME COLUMN name TO full_name | Change identifiers |
Adding a NOT NULL constraint, a primary key or a foreign key forces the engine to validate every existing row. If any row violates the rule, the statement fails and the table stays unchanged.
How database engines execute schema changes
Engines execute ALTER TABLE in one of three ways: as a metadata-only change, as an in-place change that touches data without copying the table, or as a full table rewrite that builds a new copy and swaps it in. The strategy determines how long the statement runs and how long other sessions are blocked.
Most engines take a strong lock on the table for at least part of the operation. In PostgreSQL, many forms require an ACCESS EXCLUSIVE lock, which blocks even reads. A subtle consequence is lock queuing: if a long-running query holds a weaker lock, the waiting ALTER TABLE blocks every new query behind it, which can stall an application within seconds.
The following table summarizes typical cost by operation. Exact behavior depends on the engine and version.
| Operation | Usual strategy | Impact on large tables |
|---|---|---|
| Rename column or table | Metadata only | Near instant |
| Add nullable column without default | Metadata only | Near instant |
| Add column with constant default | Metadata only in recent versions | Near instant in modern engines |
| Change column type | Often full rewrite | Long run time, heavy locking |
| Add foreign key or check | Validation scan | Reads every row |
| Drop column | Metadata or rewrite | Varies by engine |
Some engines support transactional DDL, meaning an ALTER TABLE can be rolled back together with other statements. PostgreSQL offers this, while MySQL and Oracle Database commit DDL implicitly, so a failed multi-step migration can leave the schema partially changed.
Dialect differences between SQL engines
The same intent often requires different syntax per engine, which matters when writing portable migration scripts. Changing a column type is the clearest example.
| Engine | Typical syntax | Notable behavior |
|---|---|---|
| PostgreSQL | ALTER COLUMN c TYPE new_type | Transactional DDL; USING clause for conversion |
| MySQL | MODIFY COLUMN c new_type | Online DDL options with ALGORITHM and LOCK |
| SQL Server | ALTER COLUMN c new_type | Separate statements per column change |
| Oracle Database | MODIFY (c new_type) | Implicit commit before and after DDL |
| SQLite | Limited support | Type changes need table recreation |
Because of these gaps, migration tools generate engine-specific statements from a common definition, and teams test migrations on a copy of production data before running them live.
Running ALTER TABLE safely in production
Safe production use rests on avoiding long exclusive locks and keeping every change backward compatible with the application version that is still running. Large schema changes are therefore split into several small, reversible steps.
The widely used expand and contract pattern adds new structures first, migrates data and code gradually, and removes old structures only after nothing depends on them. Renaming a column, for instance, becomes adding a new column, writing to both, backfilling in batches, switching reads, and finally dropping the old column.
- Set a lock timeout such as
SET lock_timeout = '5s'so a blocked ALTER TABLE fails fast instead of stalling traffic. - Add constraints in two phases: create them as
NOT VALID, then validate separately with a weaker lock where the engine allows it. - Backfill in batches of a few thousand rows instead of one huge UPDATE.
- Use online schema change tools for MySQL when native online DDL is insufficient; they copy the table in the background and swap it in.
Common mistakes and their consequences
Most ALTER TABLE incidents come from underestimating the effect on existing data and traffic. The table below pairs frequent mistakes with their symptoms and fixes.
| Mistake | Symptom | Fix |
|---|---|---|
| Type change on a large table at peak time | Minutes of blocked queries | Expand and contract during low traffic |
| Dropping a column still read by code | Application errors after deploy | Remove code references first |
| Adding NOT NULL without backfill | Statement fails on existing NULLs | Backfill, then add the constraint |
| Narrowing a type | Truncation or conversion errors | Check maximum lengths beforehand |
| No lock timeout | Lock queue stalls all sessions | Set a short lock timeout and retry |
Unlike TRUNCATE TABLE or DELETE, ALTER TABLE rarely destroys rows directly, yet a dropped column is unrecoverable without a backup, so destructive actions deserve the same review as data deletion.
A non-technical view
Picture a large spreadsheet that many colleagues use all day. Sometimes you need a new column, such as an email address, or you must rename a heading or forbid empty cells in a column.
ALTER TABLE is the command that changes the layout of a database table while the data stays inside. Small changes happen instantly. Big ones can make everyone wait while the whole sheet is rearranged, so experienced teams plan them carefully and do them in small steps.
A concrete example
An online retailer stores about 80 million order rows in PostgreSQL and needs to change the order_total column from INTEGER cents to NUMERIC(12,2). A direct ALTER COLUMN ... TYPE would rewrite the table under an exclusive lock and block checkout for an estimated 40 minutes.
The database team instead adds a nullable column order_total_new, which completes in milliseconds. The application writes to both columns, a background job backfills old rows in batches of 5,000 over two nights, and reads switch to the new column after verification. Each ALTER TABLE runs with a 5-second lock timeout.
A week later, the team drops the old column and renames the new one. The outcome is a type change with no visible downtime and a clean rollback path at every step.