// columns reference

ALTER TABLE SET DEFAULT

Gives future inserts a fallback value for a column. Three engines change a default in one clause; SQL Server adds a named constraint and SQLite rebuilds the table.

Categoria
Colunas
In this category
4 of 5

Sintaxe

ALTER TABLE t ALTER COLUMN c SET DEFAULT value  -- PostgreSQL, MySQL
ALTER TABLE t ADD CONSTRAINT df_name DEFAULT value FOR c  -- SQL Server: the default is a constraint
ALTER TABLE t MODIFY c DEFAULT value  -- Oracle
-- SQLite: no clause; rebuild the table with the new default in CREATE TABLE

Each block is a separate form. The support table and the examples show which engine takes which.

Suporte a mecanismos

MotorSupportNotas
SQL Server Different syntax no SET DEFAULT clause: a default is a constraint, added as ADD CONSTRAINT df_name DEFAULT value FOR c; to change an existing default, DROP CONSTRAINT the old one first (a second default fails with Msg 1781)
PostgreSQL Sim one line, metadata only; the value can be any expression, including volatile ones like clock_timestamp() or random()
MySQL Sim one line, metadata only (ALGORITHM=INSTANT); expressions must be wrapped in parentheses
SQLite No equivalent the default lives in the table definition; changing it means the create-new-table-and-copy rebuild
Oracle No equivalent different clause: MODIFY c DEFAULT value; DEFAULT NULL removes the default

Exemplos

PostgreSQL

ALTER TABLE orders ALTER COLUMN status SET DEFAULT 'new';

SQL Server

-- the name is yours to keep: DROP CONSTRAINT wants it later
ALTER TABLE dbo.orders
  ADD CONSTRAINT df_orders_status DEFAULT 'new' FOR status;

MySQL

-- literals need no parentheses on any version:
ALTER TABLE orders ALTER COLUMN status SET DEFAULT 'new';
-- expressions do, from 8.0.13:
ALTER TABLE orders ALTER COLUMN placed_at SET DEFAULT (CURRENT_DATE);

SQLite

-- no SET DEFAULT: rebuild (shortened; the full procedure
-- also re-creates indexes and triggers)
CREATE TABLE orders_new (
  id INTEGER PRIMARY KEY,
  customer_id INTEGER NOT NULL,
  status TEXT NOT NULL DEFAULT 'new'
);
INSERT INTO orders_new (id, customer_id, status) SELECT id, customer_id, status FROM orders;
DROP TABLE orders;
ALTER TABLE orders_new RENAME TO orders;

Oracle

ALTER TABLE orders MODIFY status DEFAULT 'new';
-- DEFAULT ON NULL also covers inserts that send NULL (12c+)
ALTER TABLE orders MODIFY status DEFAULT ON NULL 'new';

Bom saber

  • Changing a default never touches existing rows on any engine. Defaults fill in when an INSERT omits the column, so a new default is invisible until the next insert that needs it.
  • SQL Server wraps every default in a constraint, and an unnamed one gets a machine name such as DF__orders__status__5AEE82B9. You need that exact name to drop it later, so name defaults when you create them.
  • MySQL takes a literal bare; an expression must be in parentheses and needs 8.0.13 or newer, as in SET DEFAULT (UPPER('new')). PostgreSQL accepts either shape, bare or parenthesized.
  • Oracle's MODIFY c DEFAULT value rewrites nothing either, but from 12c you can add ON NULL: DEFAULT ON NULL value feeds the default to inserts that explicitly send NULL instead of erroring.

Questions about SET DEFAULT

Why does SQL Server have no SET DEFAULT clause?

Defaults are constraints on SQL Server, so they get names and live in the catalog alongside primary keys and foreign keys. Adding one without a name makes SQL Server generate one, and the generated name is what DROP CONSTRAINT wants later. ADD CONSTRAINT ... DEFAULT ... FOR lets you pick the name.

Does setting a new default update existing rows?

No, on every engine. A default is consulted only when an INSERT leaves the column out; rows already in the table keep their values. Run an UPDATE afterwards for the rows you want changed.

Can a default be an expression like now()?

PostgreSQL yes, bare or parenthesized. MySQL accepts expressions in parentheses from 8.0.13, and through SET DEFAULT even CURRENT_TIMESTAMP must be parenthesized, as in SET DEFAULT (CURRENT_TIMESTAMP); it goes bare only in a full column definition such as MODIFY c DATETIME DEFAULT CURRENT_TIMESTAMP. Subqueries, variables and stored functions are never allowed. SQL Server and Oracle accept functions such as SYSDATETIME() and SYSDATE. SQLite allows parenthesized constant expressions in CREATE TABLE, non-deterministic ones like (datetime('now')) and (random()) included, and nothing at all after creation.

Browse every statement in the SQL DDL reference, or see what the SQLly desktop app does for your database.

// dialect-aware editing

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.