There is such a table:
CREATE TABLE `tiny_url` (
`id` bigint NOT NULL AUTO_INCREMENT,
`user_id` int NOT NULL,
`short_uuid` varchar(255) NOT NULL,
`video_id` int NOT NULL,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `tinyurl_short_uuid` (`short_uuid`),
UNIQUE KEY `tinyurl_user_id_video_id` (`user_id`,`video_id`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
Operation 1: I use SQL in a transaction:
begin
First opening is a transaction, then select insert:
insert into tiny_url (user_id,video_id,short_uuid) values (100,300,'ilikeyou')
Operation two: and use another SQL in another transaction: insert into tiny_url (user_id,video_id,short_uuid) values (100,300,'ilikeyou too')
Because there is a unique constraint UNIQUE KEY
tinyurl_user_id_video_id (
user_id ,
video_id )
, so, do you think, will operate two error or jam?
The answer is blocking.
What happens if operation one is executing commit
at this moment?
Answer: Operation 1 is successfully inserted, and operation 2 reports an error
What happens if operation one is executing rollback
at this moment?
Answer: The first operation is rolled back successfully, and the second operation is inserted successfully
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。