// dialect comparison

Set or remove a column default

Defaults are cheap to change, except where they are constraints or where they are frozen in the CREATE TABLE.

Filed under Tables & schema

The verdict

PostgreSQL, MySQL and Oracle change a default in one short statement. SQL Server's constraint handling takes more ceremony but works; SQLite is the one engine where changing a default means rebuilding the table.

Where they differ

Three engines treat a default as a column attribute with SET / DROP DEFAULT (Oracle spells it MODIFY … DEFAULT). SQL Server models it as a named constraint you add and drop. SQLite can't change a default at all: it is part of the stored CREATE TABLE text, so changing it means rebuilding the table.

How each engine writes it

SQL Server

ALTER TABLE customers
  ADD CONSTRAINT df_customers_email DEFAULT 'unknown' FOR email;
ALTER TABLE customers DROP CONSTRAINT df_customers_email;

A default is a constraint object: to change one, drop the old constraint and add a new one. There is no ALTER COLUMN SET DEFAULT.

PostgreSQL

ALTER TABLE customers ALTER COLUMN email SET DEFAULT 'unknown';
ALTER TABLE customers ALTER COLUMN email DROP DEFAULT;

Metadata-only and instant; only rows inserted afterwards are affected.

MySQL

ALTER TABLE customers ALTER COLUMN email SET DEFAULT 'unknown';
ALTER TABLE customers ALTER COLUMN email DROP DEFAULT;

Same shape as PostgreSQL, with one difference: after DROP DEFAULT the column has no default at all, so in strict mode an INSERT that omits it fails with error 1364 even when the column is nullable. Expression defaults such as (UUID()) need 8.0.13+ and parentheses.

SQLite

CREATE TABLE customers_new (id INTEGER PRIMARY KEY, email TEXT DEFAULT 'unknown');
INSERT INTO customers_new SELECT id, email FROM customers;
DROP TABLE customers;
ALTER TABLE customers_new RENAME TO customers;

No ALTER for defaults: the default lives in the CREATE TABLE. This rebuild sets the default; removing it is the same rebuild without the DEFAULT clause. Run it in a transaction with PRAGMA foreign_keys = OFF.

Oracle

ALTER TABLE customers MODIFY (email DEFAULT 'unknown');
ALTER TABLE customers MODIFY (email DEFAULT NULL);

MODIFY … DEFAULT sets it; DEFAULT NULL is how you remove it. Only new inserts are affected.

Questions that come up

Does changing a default affect existing rows?

No, in every engine. Defaults apply when a row is inserted without that column; existing rows keep whatever they store. (Adding a new column with a default is different: that fills existing rows.)

Why is SQL Server's default a constraint?

SQL Server stores each default as an object in sys.default_constraints. If you don't name it, it gets a generated name like DF__customers__email__3B75D760, which differs per database, so migration scripts have to look the name up before dropping it. Name your defaults.

Can a default be an expression?

PostgreSQL: any expression without subqueries or column references. SQL Server: constants and functions such as GETDATE() or NEWID(). MySQL: parenthesized expressions from 8.0.13. Oracle: expressions such as SYSDATE, plus sequence.NEXTVAL since 12c. SQLite: constants, CURRENT_TIMESTAMP and the like, or a constant expression in parentheses.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.