ALTER TABLE ADD FOREIGN KEY
Points a column at another table's key and starts checking every write. The clause reads the same on four engines; validation and indexing are where they diverge.
- 类别
- 表
- In this category
- 9 of 16
语法
ALTER TABLE t ADD CONSTRAINT fk_name FOREIGN KEY (col) REFERENCES other (id) -- SQL Server, PostgreSQL, MySQL, Oracle
ALTER TABLE t ADD CONSTRAINT fk_name FOREIGN KEY (col) REFERENCES other (id) NOT VALID -- PostgreSQL: check old rows in a second step
-- SQLite: no ADD CONSTRAINT form; rebuild, or ADD COLUMN ... REFERENCES for a new column
Each block is a separate form. The support table and the examples show which engine takes which.
引擎支持
| 引擎 | Support | 备注 |
|---|---|---|
| SQL Server | 是 | validates existing rows by default; WITH NOCHECK skips the scan but leaves the constraint not trusted |
| PostgreSQL | 是 | the plain form scans and locks; NOT VALID plus VALIDATE CONSTRAINT splits the work |
| MySQL | 是 | InnoDB only; both tables InnoDB, the referenced columns a primary or unique key, and an index on the referencing columns gets built for you if missing |
| SQLite | No equivalent | no ALTER form for an existing column (ADD COLUMN ... REFERENCES works for a new one); even then it is inert until PRAGMA foreign_keys = ON |
| Oracle | 是 | ENABLE NOVALIDATE enforces new rows without scanning old ones |
示例
PostgreSQL
ALTER TABLE orders ADD CONSTRAINT fk_orders_customer FOREIGN KEY (customer_id) REFERENCES customers (id);
PostgreSQL
-- scan-free add, then validate while writes continue ALTER TABLE orders ADD CONSTRAINT fk_orders_customer FOREIGN KEY (customer_id) REFERENCES customers (id) NOT VALID; ALTER TABLE orders VALIDATE CONSTRAINT fk_orders_customer;
SQL Server
-- fast add that skips the row scan ALTER TABLE dbo.orders WITH NOCHECK ADD CONSTRAINT fk_orders_customer FOREIGN KEY (customer_id) REFERENCES dbo.customers (id); -- later, make it trusted again: ALTER TABLE dbo.orders WITH CHECK CHECK CONSTRAINT fk_orders_customer;
MySQL
ALTER TABLE orders ADD CONSTRAINT fk_orders_customer FOREIGN KEY (customer_id) REFERENCES customers (id);
Oracle
-- enforce new rows, skip the scan of old ones ALTER TABLE orders ADD CONSTRAINT fk_orders_customer FOREIGN KEY (customer_id) REFERENCES customers (id) ENABLE NOVALIDATE;
SQLite
-- no ALTER form for an existing column; declare it at creation or rebuild: -- CREATE TABLE orders (..., FOREIGN KEY (customer_id) REFERENCES customers (id)) -- and per connection: PRAGMA foreign_keys = ON;
值得了解
- The plain ADD CONSTRAINT walks the whole table to validate existing rows, on all four engines that support it. On a large table that is minutes of scanning; PostgreSQL's NOT VALID and Oracle's NOVALIDATE exist for exactly this.
- MySQL's InnoDB quietly creates an index on the referencing columns if none exists, and that index stays when you drop the constraint later. Create your own index with the name you want before adding the key.
- SQL Server's WITH NOCHECK has a trap: the constraint is stored untrusted, and the optimizer skips untrusted constraints in its plans. Re-trust it with ALTER TABLE t WITH CHECK CHECK CONSTRAINT fk_name.
- SQLite has no ALTER TABLE ADD FOREIGN KEY: an existing column only gains a reference through the full table rebuild. ALTER TABLE ADD COLUMN ... REFERENCES does work for a brand-new column with a NULL default, and enforcement still needs PRAGMA foreign_keys = ON per connection.
Questions about ADD FOREIGN KEY
Can I add a foreign key without checking existing rows?
Yes. PostgreSQL: NOT VALID, then VALIDATE CONSTRAINT later. Oracle: ENABLE NOVALIDATE. SQL Server: WITH NOCHECK (and remember the untrusted flag). MySQL: SET FOREIGN_KEY_CHECKS = 0 around the ALTER skips the scan, so turn checks back on and reconcile any bad rows the scan would have caught.
Does the referenced column need to be a primary key?
No. A unique constraint works on every engine, and a plain unique index also works on SQL Server, PostgreSQL, MySQL and SQLite; Oracle needs an actual primary key or unique constraint. The referenced columns need uniqueness, not the primary key specifically.
Why did MySQL create an index I did not ask for?
InnoDB must be able to find the child rows to police updates and deletes, so it builds an index on the referencing columns when none exists, naming it after the constraint if you gave one, otherwise after the column. Define your own index first if the name matters.
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.