Consider a table with fields ID, X, Y, and Z.
CREATE TABLE TABLE_NAME (
ID int NOT NULL,
X varchar(255) NOT NULL,
Y varchar(255) NOT NULL,
Z varchar(255) NOT NULL,
PRIMARY KEY (ID)
);
create index idx on TABLE_NAME (X, Y);
Suppose I have the following transaction with repeatable read isolation level -
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
BEGIN TRANSACTION;
SELECT * FROM TABLE_NAME WHERE X="A" AND Y="B";
INSERT INTO TABLE (ID, X, Y, Z) VALUES (25, "A", "B", "C");
INSERT INTO TABLE (ID, X, Y, Z) VALUES (26, "A", "B", "D");
DELETE FROM TABLE_NAME WHERE X="A" AND Y="B" AND (Z != "C" OR Z != "D")
COMMIT TRANSACTION;
Now suppose I have a normal SELECT statement being executed outside the transaction. I want to know what happens to the SELECT statement if -
- It executes between the
SELECTandINSERTof the transaction. - It executes between the first
INSERTstatement and secondINSERTstatement of the transaction. - It executes between the
INSERTstatement 2 andDELETEstatement of the transaction. - It executes between
DELETEstatement and committing the transaction.
I did go through the docs, and these are my assumptions for the scenarios -
SELECTonly obtains a shared lock, so theSELECTstatement outside the transaction is allowed to execute and sees all related rows in the table.INSERTobtains an exclusive lock the the newly inserted row. TheSELECTstatement outside the transaction is allowed to execute but it does not see the newly inserted row.- Same as #2, allowed but it does not see both newly inserted rows.
DELETEobtains an exclusie lock, so theSELECTstatement outside the transaction is blocked, until the executing transaction is committed.
Is my assumption correct? Specially confused about #2 and #3 here.