mirror of
https://github.com/MariaDB/server.git
synced 2025-01-26 00:34:18 +01:00
28 lines
599 B
Text
28 lines
599 B
Text
CREATE TABLE parent (
|
|
id INT,
|
|
KEY (id)
|
|
) ENGINE=InnoDB;
|
|
CREATE TABLE child (
|
|
id INT,
|
|
parent_id INT,
|
|
FOREIGN KEY (parent_id)
|
|
REFERENCES parent(id)
|
|
ON UPDATE CASCADE
|
|
ON DELETE CASCADE
|
|
) ENGINE=InnoDB;
|
|
INSERT INTO parent VALUES (1), (1), (2), (2);
|
|
INSERT INTO child VALUES (1,1), (2,2), (1,1), (2,2);
|
|
DELETE FROM parent WHERE id = 1;
|
|
SELECT COUNT(*) = 0 FROM child WHERE id = 1;
|
|
COUNT(*) = 0
|
|
1
|
|
UPDATE parent SET id = 3 WHERE id = 2;
|
|
SELECT COUNT(*) = 0 FROM child WHERE parent_id = 1;
|
|
COUNT(*) = 0
|
|
1
|
|
SELECT parent_id = 3 FROM child WHERE id = 2;
|
|
parent_id = 3
|
|
1
|
|
1
|
|
DROP TABLE child;
|
|
DROP TABLE parent;
|