Can a transaction consist only of a read operation?

EvideShow :

Recently I was at an technical interview, the interviewer insisted that there are no read transactions in SQL. Transactions are only needed to modify data. Is it so? Could you please argument your answer? Thanks

David דודו Markovitz :

The interviewer was wrong.
A transaction can consist only of a read operation (and the transaction is significant)

Here is an example for SQL Server

REPEATABLE READ
Specifies that statements cannot read data that has been modified but not yet committed by other transactions and that no other transactions can modify data that has been read by the current transaction until the current transaction completes.
SET TRANSACTION ISOLATION LEVEL (Transact-SQL)

-- setup
create table t (i int);
insert into t(i) values (1);

-- session 1
set transaction isolation level repeatable read
begin transaction
select * from t

-- session 2
update t set i = 2 -- session 2 is blocked until session 1 commits/rollbacks

As you can see session 1 is consist only of a read operation and yet the transaction is significant

Here is another example for Oracle

Transaction-Level Read Consistency
Oracle Database can also provide read consistency to all queries in a transaction, known as transaction-level read consistency. In this case, each statement in a transaction sees data from the same point in time, which is the time at which the transaction began.
Data Concurrency and Consistency

-- setup
create table t (i int);
insert into t(i) values (1);

-- session 1
set transaction read only;

-- session 2
update t set i = 2;

-- session 1
select * from t; -- for sesion 1 --> i = 1

-- session 2
update t set i = 3;

-- session 1
select * from t; -- for sesion 1 --> i = 1
commit;

select * from t; -- for sesion 1 --> i = 3

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=33140&siteId=1