// dialect comparison

Add a foreign key or check constraint

Wire tables together or give them a rule: one ALTER, unless you are on SQLite.

Filed under Tables & schema

The verdict

ADD CONSTRAINT reads the same on four engines; SQLite's rebuild is the price of keeping constraints in the stored CREATE TABLE text.

Where they differ

SQL Server, PostgreSQL, MySQL and Oracle share ADD CONSTRAINT for foreign keys (with ON DELETE CASCADE) and CHECK rules. SQLite has no ADD CONSTRAINT at all: constraints live in the CREATE TABLE, so adding one means rebuilding the table with it declared.

How each engine writes it

SQL Server

ALTER TABLE orders ADD CONSTRAINT fk_orders_customer
  FOREIGN KEY (customer_id) REFERENCES customers (id) ON DELETE CASCADE;
ALTER TABLE orders ADD CONSTRAINT ck_orders_total CHECK (total >= 0);

ALTER TABLE orders WITH NOCHECK ADD CONSTRAINT … skips checking existing rows. It is fast, but the constraint stays untrusted, and the optimizer ignores it, until you run WITH CHECK CHECK CONSTRAINT.

PostgreSQL

ALTER TABLE orders ADD CONSTRAINT fk_orders_customer
  FOREIGN KEY (customer_id) REFERENCES customers (id) ON DELETE CASCADE;
ALTER TABLE orders ADD CONSTRAINT ck_orders_total CHECK (total >= 0);

ADD CONSTRAINT ... NOT VALID, then VALIDATE CONSTRAINT in a second (short-lock) transaction, keeps big tables available.

MySQL

ALTER TABLE orders ADD CONSTRAINT fk_orders_customer
  FOREIGN KEY (customer_id) REFERENCES customers (id) ON DELETE CASCADE;
ALTER TABLE orders ADD CONSTRAINT ck_orders_total CHECK (total >= 0);

InnoDB enforces both and creates an index on customer_id if none exists. CHECK is enforced since 8.0.16; older versions parsed it and ignored it.

SQLite

PRAGMA foreign_keys = OFF;
BEGIN;
CREATE TABLE orders_new (id INTEGER PRIMARY KEY, customer_id INTEGER
  REFERENCES customers (id) ON DELETE CASCADE, total REAL CHECK (total >= 0));
INSERT INTO orders_new SELECT id, customer_id, total FROM orders;
DROP TABLE orders;
ALTER TABLE orders_new RENAME TO orders;
COMMIT;
PRAGMA foreign_keys = ON;

No ADD CONSTRAINT: constraints live in the CREATE TABLE, so rebuild. Foreign keys are only enforced when PRAGMA foreign_keys = ON, which is off by default on every new connection.

Oracle

ALTER TABLE orders ADD CONSTRAINT fk_orders_customer
  FOREIGN KEY (customer_id) REFERENCES customers (id) ON DELETE CASCADE;
ALTER TABLE orders ADD CONSTRAINT ck_orders_total CHECK (total >= 0);

Appending ENABLE NOVALIDATE adds the constraint without checking existing rows; RELY tells the optimizer to trust it anyway. Oracle has no ON UPDATE CASCADE.

Questions that come up

Why add a constraint NOT VALID first?

Validation scans the whole table while holding a lock. In PostgreSQL, ADD CONSTRAINT … NOT VALID applies the rule to new writes immediately with only a brief lock, and VALIDATE CONSTRAINT later checks old rows under a lock that still allows reads and writes.

What does ON DELETE CASCADE do exactly?

Deleting a customers row deletes its orders automatically. SET NULL, NO ACTION and RESTRICT are the other actions (Oracle supports CASCADE and SET NULL only). SQLite supports them all, but only with PRAGMA foreign_keys = ON.

Can I add a constraint without checking old data?

SQL Server: WITH NOCHECK. PostgreSQL: NOT VALID. Oracle: ENABLE NOVALIDATE. MySQL: SET foreign_key_checks = 0 before adding a foreign key skips the check; CHECK constraints are always checked unless declared NOT ENFORCED. SQLite checks nothing when copying rows during the rebuild; run PRAGMA foreign_key_check afterwards.

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.