Since we now know how to look at the state of locking in a live
database, let’s look at what happens when we run a normal insert
or update and an insert or update with foreign key relationships
defined, and compare.
We will be using the tables and structures from our previous
examples, a simple 1:n relationship between a
and
b
:
CREATE TABLE a (
a_id int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (a_id)
);
INSERT INTO a VALUES (10), (20), (30), (40);
CREATE TABLE b (
b_id int NOT NULL AUTO_INCREMENT,
a_id int NOT NULL,
PRIMARY KEY (b_id),
KEY `a_id` (a_id),
CONSTRAINT a_id_exists FOREIGN KEY (a_id) REFERENCES a (a_id) ON DELETE RESTRICT ON UPDATE RESTRICT
);
INSERT INTO b VALUES (10,10), (40,40);
or the same definition for b
without the constraint.
A normal INSERT and UPDATE
First, let’s look at an insert and update into b
without any …
[Read more]