SQL reference (MySQL and MariaDB dialects)

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#

SQL
[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#

NameMeaningDefault
RESTRICT, NO ACTIONRefuse a parent delete or key change while child rows refer to it (error 1451).NO ACTION
CASCADEFollow the change to the referring rows (depth 15; a self-referring row goes with itself).none
SET NULLClear the referring columns.none
SET DEFAULTRead, but refused as InnoDB refuses it.none
MATCH xRead and ignored.none
foreign_key_checks0 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_INCREMENT value 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 TABLE of a referenced table is error 3730; TRUNCATE TABLE of one is error 1701.
  • SHOW CREATE TABLE shows the constraints. information_schema.TABLE_CONSTRAINTS, KEY_COLUMN_USAGE and REFERENTIAL_CONSTRAINTS list 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.
  • REFERENCES written inside a column definition is refused with error 1235; write a table-level FOREIGN KEY.
  • MariaDB's ADD FOREIGN KEY IF NOT EXISTS and DROP FOREIGN KEY IF EXISTS are read (D81).
  • Cascading updates do not run the child table's CHECK constraints.

Not supported yet (known)#

  • Keys to a table of another database.
  • The table name in the error of an ALTER that finds rows that do not fit differs from MySQL's (MySQL names its internal #sql-... copy).

Examples#

Example:

SQL
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;

See also#