Indexes, constraints and generated columns
Create and drop indexes, use unique, foreign-key and CHECK constraints, generated columns and functional indexes, with checked statements.
Indexes#
An index is declared in CREATE TABLE (KEY idx_name (name)) or added later:
CREATE INDEX idx_n ON words (n);
CREATE UNIQUE INDEX uq_tag ON tags (tag);
DROP INDEX kn ON a;
SHOW INDEX FROM a;(The tables words, tags and a are created first.) Creating a unique index on a table that already has rows claims every existing value; if a value occurs twice, nothing is made and the error names the duplicate.
Queries use indexes in the engine: a comparison or range on an indexed column, and on an integer primary key, becomes an index lookup, and a statement that names the whole primary key with constants reads one row. SHOW INDEX reports a cardinality of 0 (the engine keeps no statistics), which is one of the few documented differences from MySQL.
Invisible indexes and indexes on expressions#
CREATE TABLE t (id INT PRIMARY KEY, a INT, b INT, name VARCHAR(20), KEY ka ((a * 2)), UNIQUE KEY u1 ((LOWER(name))), KEY kb (b, (a + b)), KEY ((a - b)));
INSERT INTO t VALUES (1, 1, 10, 'Ann'), (2, 2, 20, 'Bob');
INSERT INTO t VALUES (3, 3, 30, 'ann');
SELECT id FROM t WHERE a * 2 = 4;
SELECT id FROM t WHERE LOWER(name) = 'bob';The key on LOWER(name) is unique: inserting 'ann' when 'Ann' exists fails with a duplicate-entry error. A functional index is a hidden generated column with an index on it. INVISIBLE columns and indexes are checked as well.
Foreign keys#
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 parent (name) VALUES ('a'), ('b'), ('c');
INSERT INTO child_cascade (pid, v) VALUES (1, 10), (1, 11), (2, 20);
INSERT INTO child_cascade (pid, v) VALUES (99, 1);
DELETE FROM parent WHERE id = 1;(ENGINE=InnoDB is accepted.) The insert with pid = 99 fails with error 1452 (a child row with no parent). DELETE FROM parent WHERE id = 1 removes the two child rows of child_cascade and sets the pid of child_null to NULL. A foreign key without an action refuses the delete or update with error 1451. The front-end enforces foreign keys, with cascades up to 15 levels deep.
CHECK constraints#
CREATE TABLE t (id INT PRIMARY KEY, qty INT CHECK (qty >= 0), price DECIMAL(8,2), CONSTRAINT price_pos CHECK (price > 0));
INSERT INTO t VALUES (1, 5, 1.50);
INSERT INTO t VALUES (2, -1, 1.50);
INSERT INTO t VALUES (3, 5, 0);(After the earlier statements, which also add a constraint on name.) The second and third inserts fail with error 3819 (a check constraint is violated). A NULL satisfies a check, as in MySQL.
Generated columns#
CREATE TABLE t (id INT PRIMARY KEY, a INT, b INT, s INT GENERATED ALWAYS AS (a + b) STORED, v INT AS (a * 2) VIRTUAL, UNIQUE KEY ux (v));
INSERT INTO t (id, a, b) VALUES (1, 1, 2), (2, 10, 20);
SELECT * FROM t ORDER BY id;
UPDATE t SET a = 100 WHERE id = 1;
INSERT INTO t (id, a, b, s) VALUES (4, 1, 1, 5);A generated column is filled from the other columns when a row is inserted and when its sources change; a stored one is kept, a virtual one is computed. After the first insert row 1 has s = 3, v = 2 and row 2 has s = 30, v = 20. Naming a value for a generated column is error 3105 (the last statement above). A generated column can be indexed, and can be UNIQUE.
Defaults that are expressions#
DEFAULT (expression) is checked; AUTO_INCREMENT columns take the next value, and a value you give raises the counter past it.