Foreign keys
FOREIGN KEY constraints with enforcement, the referential actions, and the limits of this version.
Foreign keys are kept in the table definition and enforced (D64, fk.rs). Verified against MySQL and MariaDB.
Syntax#
[CONSTRAINT [symbol]] FOREIGN KEY [index_name] (col_name, ...)
REFERENCES tbl_name [(col_name, ...)] [MATCH match_type]
[ON DELETE reference_option] [ON UPDATE reference_option]
reference_option:
RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT
ALTER TABLE tbl_name ADD [CONSTRAINT [symbol]] FOREIGN KEY ... REFERENCES ...
ALTER TABLE tbl_name DROP { FOREIGN KEY | CONSTRAINT } symbol
SET foreign_key_checks = { 0 | 1 }Arguments#
| Name | Meaning | Default |
|---|---|---|
RESTRICT, NO ACTION | Refuse a parent delete or key change while child rows refer to it (error 1451). | NO ACTION |
CASCADE | Follow the change to the referring rows (depth 15; a self-referring row goes with itself). | none |
SET NULL | Clear the referring columns. | none |
SET DEFAULT | Read, but refused as InnoDB refuses it. | none |
MATCH x | Read and ignored. | none |
foreign_key_checks | 0 switches enforcement off for the session. | 1 |
Remarks#
- Creating. The key is checked against the parent when it is made: errors 1824, 3734, 1822, 1830, 1239. An index on the referring columns is added when there is none. The parent and the child must be in the same database.
- Enforced. A child row must refer to an existing parent row (error 1452), checked before an
AUTO_INCREMENTvalue is taken. A null in a referring column passes. - Actions. The rows a cascade changes are logged so that a failing statement or a rolled-back transaction puts them back; taking back never checks.
DROP TABLEof a referenced table is error 3730;TRUNCATE TABLEof one is error 1701.SHOW CREATE TABLEshows the constraints.information_schema.TABLE_CONSTRAINTS,KEY_COLUMN_USAGEandREFERENTIAL_CONSTRAINTSlist them, identical to MySQL.- The referring tables of a table come from a per-database list kept 10 seconds and forgotten when a definition changes.
REFERENCESwritten inside a column definition is refused with error 1235; write a table-levelFOREIGN KEY.- MariaDB's
ADD FOREIGN KEY IF NOT EXISTSandDROP FOREIGN KEY IF EXISTSare read (D81). - Cascading updates do not run the child table's
CHECKconstraints.
Not supported yet (known)#
- Keys to a table of another database.
- The table name in the error of an
ALTERthat finds rows that do not fit differs from MySQL's (MySQL names its internal#sql-...copy).
Examples#
Example:
CREATE TABLE parent (id INT NOT NULL AUTO_INCREMENT, name VARCHAR(20), PRIMARY KEY (id)) ENGINE=InnoDB;
CREATE TABLE child_cascade (id INT NOT NULL AUTO_INCREMENT, pid INT NOT NULL, v INT,
PRIMARY KEY (id), KEY pid (pid),
CONSTRAINT fk_cc FOREIGN KEY (pid) REFERENCES parent (id) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE=InnoDB;
CREATE TABLE child_null (id INT NOT NULL AUTO_INCREMENT, pid INT, PRIMARY KEY (id),
CONSTRAINT fk_cn FOREIGN KEY (pid) REFERENCES parent (id) ON DELETE SET NULL) ENGINE=InnoDB;
INSERT INTO child_cascade (pid, v) VALUES (99, 1); -- error 1452
DELETE FROM parent WHERE id = 1; -- child_cascade rows go, child_null rows get NULL
DROP TABLE parent; -- error 3730
SET foreign_key_checks = 0;