mirror of
https://github.com/MariaDB/server.git
synced 2025-01-26 00:34:18 +01:00
25 lines
604 B
Text
25 lines
604 B
Text
CREATE TABLE parent (
|
|
id1 INT,
|
|
id2 INT,
|
|
PRIMARY KEY (id1, id2) /* Multipart PK */
|
|
) ENGINE=InnoDB;
|
|
CREATE TABLE child (
|
|
id INT PRIMARY KEY,
|
|
parent_id1 INT,
|
|
FOREIGN KEY (parent_id1)
|
|
REFERENCES parent(id1) /* FK is subset of PK above */
|
|
ON UPDATE CASCADE
|
|
ON DELETE CASCADE
|
|
) ENGINE=InnoDB;
|
|
INSERT INTO parent VALUES (1, 2);
|
|
INSERT INTO child VALUES (1, 1);
|
|
UPDATE parent SET id1 = 3 WHERE id1 = 1;
|
|
SELECT COUNT(*) = 1 FROM child WHERE parent_id1 = 3;
|
|
COUNT(*) = 1
|
|
1
|
|
DELETE FROM parent WHERE id1 = 3;
|
|
SELECT COUNT(*) = 0 FROM child WHERE parent_id1 = 3;
|
|
COUNT(*) = 0
|
|
1
|
|
DROP TABLE child;
|
|
DROP TABLE parent;
|