简体   繁体   中英

transactions in SQL

I have two transactions that are the following:

T1:

Begin Transaction;
S1: Insert Into Salary Values (103, 60000);
S2: Update Salary Set salary = salary + 10000 Where id = 101;
Commit;

T2:

Begin Transaction;
S3: Select Avg(salary) As a1 From Salary;
S4: Select Avg(salary) As a2 From Salary; 

Assume that the individual statements S1, S2, S3, and S4 always execute atomically. Suppose initially there are two tuples in Salary: (101,30000) and (102,50000). Each transaction runs once and commits. Transaction T1 always executes with isolation level Serializable (the highest level).

a) If transaction T2 executes with isolation level Serializable, what possible pairs of valuesa1 and a2 are returned by T2?

b) If transaction T2 executes with isolation level Read-Committed, what possible pairs of values a1 and a2 are returned by T2?

c) If transaction T2 executes with isolation level Read-Uncommitted what possible pairs of values a1 and a2 are returned by T2?

Here are my answers:

a) S1 – S2 – S3 – S4 Value: a1: 50000 , a2: 50000

S3 – S4 – S1 – S2 Value: a1: 40000, a2: 40000

But I'm a bit confused on how read-committed and how read-uncommitted work. Any help with B and C, would be a great help.

  • read-committed = show only what was already committed (or SAVED IN DB)
  • read-uncommitted = read what is currently in the database (regardless of commit).

given a long transaction, before you issue a "commit" command, all the inserts/updates/deletes cannot be viewed unless you do a "read-uncommitted". under the hood, the data you insert/update are locked for normal viewing. sometimes even the entire table is locked - you cannot view any record at all unless you do a read uncommitted.

for fast operations (eg less than 1 sec) you would not really feel the difference between the two but to test the difference, you should open a different connection for the T2 and run T1 without the "commit" command, then you will feel the "lock" on read-committed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM