Answer the question
In order to leave comments, you need to log in
Rows of related tables are deleted inside the overall transaction?
Let's say there are 2 tables linked via a foreign key:
CREATE TABLE `post` (
`id` int(11) NOT NULL AUTO_INCREMENT,
...
PRIMARY KEY (`id`)
);
CREATE TABLE `post_comment` (
`post_id` int(11) NOT NULL,
...
FOREIGN KEY (`post_id`) REFERENCES `post`(`id`) ON DELETE CASCADE
);
DELETE FROM `post` WHERE `id` = 1
START TRANSACTION
SELECT * FROM `post` WHERE `id` = 1 FOR UPDATE
DELETE FROM `post_comment` WHERE `post_id` = 1
DELETE FROM `post` WHERE `id` = 1
COMMIT
START TRANSACTION
DELETE FROM `post_comment` WHERE `post_id` = 1
COMMIT
START TRANSACTION
DELETE FROM `post` WHERE `id` = 1
COMMIT
Answer the question
In order to leave comments, you need to log in
In PostgreSQL, a transaction is consistent. That is, if you do DELETE FROM `post` WHERE `id` = 1, then all related deletions from `post_comment` will be in the same transaction. Those. option 1.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question