ALTER TABLE ADD COLUMN
Extends a table with a new column. The basic clause is near-identical everywhere; the interesting parts are which engines drop the COLUMN keyword, and the rules a new column's default and NOT NULL must follow.
- Categoria
- Colunas
- In this category
- 1 of 5
Sintaxe
ALTER TABLE t ADD COLUMN col type [constraints] -- PostgreSQL, MySQL, SQLite
ALTER TABLE t ADD col type [constraints] [, ...] -- SQL Server, Oracle: no COLUMN keyword
ALTER TABLE t ADD COLUMN col type AFTER other_col -- MySQL: position
Each block is a separate form. The support table and the examples show which engine takes which.
Suporte a mecanismos
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Sim | ADD without COLUMN; several columns comma-separated |
| PostgreSQL | Sim | a constant default skips the table rewrite (fast default, 11+) |
| MySQL | Sim | ADD COLUMN, with AFTER or FIRST to place it; instant adds from 8.0.12 |
| SQLite | Different syntax | the new column cannot be PRIMARY KEY or UNIQUE; NOT NULL needs a constant default |
| Oracle | Sim | ADD without COLUMN; several columns in parentheses |
Exemplos
PostgreSQL
ALTER TABLE orders ADD COLUMN shipped_at TIMESTAMPTZ;
PostgreSQL
-- instant on 11+, no rewrite despite the default ALTER TABLE orders ADD COLUMN priority SMALLINT NOT NULL DEFAULT 0;
SQL Server
ALTER TABLE dbo.orders ADD shipped_at DATETIME2 NULL, priority SMALLINT NOT NULL DEFAULT 0;
MySQL
ALTER TABLE orders ADD COLUMN shipped_at DATETIME NULL AFTER placed_at;
SQLite
-- constant default only, no PRIMARY KEY or UNIQUE ALTER TABLE orders ADD COLUMN priority INTEGER NOT NULL DEFAULT 0;
Oracle
ALTER TABLE orders ADD (shipped_at DATE, priority NUMBER(3) DEFAULT 0 NOT NULL);
Bom saber
- SQL Server and Oracle drop the COLUMN keyword: ALTER TABLE t ADD created_at DATETIME2. MySQL, PostgreSQL and SQLite accept ADD COLUMN, where COLUMN is optional. Pasting ADD COLUMN into SQL Server is the classic cross-dialect error: it fails with Incorrect syntax near the keyword 'COLUMN'.
- Adding a NOT NULL column without a default fails on a non-empty table in SQL Server, PostgreSQL, SQLite and Oracle: the existing rows would have nothing to put in the column. Give it a DEFAULT and the backfill is automatic. MySQL is the exception: it accepts the statement, even in strict mode, and quietly fills existing rows with the type's implicit default, such as 0 or an empty string.
- SQLite keeps ADD COLUMN conservative: the new column cannot be PRIMARY KEY or UNIQUE, a NOT NULL column needs a non-NULL constant default, and defaults must be constants rather than expressions.
- PostgreSQL 11+ stores a constant default as metadata and skips the rewrite, so ADD COLUMN ... DEFAULT 0 on a billion-row table is instant. A volatile default such as DEFAULT clock_timestamp() or random() still rewrites every row; now() is only stable, so it is evaluated once and stays a fast default.
Questions about ADD COLUMN
Why does my NOT NULL ADD COLUMN fail?
Existing rows have no value for the new column. SQL Server, PostgreSQL, SQLite and Oracle demand a DEFAULT for a NOT NULL column on a non-empty table; add the default and the backfill happens as part of the statement. MySQL lets it through and fills existing rows with the type's implicit default, such as 0 or an empty string.
Does adding a column lock or rewrite the table?
Depends on the engine and the form. PostgreSQL 11+ with a constant default is metadata-only, and SQLite never rewrites because the default lives in the schema. MySQL 8.0.12+ can add a plain trailing column with the INSTANT algorithm. For anything else on a large table, check the engine's docs before running the statement.
Can I add a column in a specific position?
MySQL: ADD COLUMN x INT AFTER other_col, or FIRST. SQL Server, PostgreSQL and SQLite append at the end. Column order is cosmetic anyway; queries that name columns do not care where the column sits.
Browse every statement in the SQL DDL reference, or see what the SQLly desktop app does for your database.
DDL that matches the engine you're connected to
SQLly checks your statements against the dialect of the open connection, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle, so an ALTER TABLE that works on one engine gets flagged before it runs on another.