Sample Exam Questions

From the objective of OSS-DB Exam Silver
S3.3 Transactions (Transaction syntax)

Sample Question

3.25

The following sequence of SQL statements was executed. What is the value returned by the last SELECT statement?

create table test(id integer);
begin;
insert into test(id) values(1);
commit;
begin;
insert into test(id) values(2);
savepoint x;
insert into test(id) values(3);
rollback to x;
insert into test(id) values(4);
commit;
begin;
insert into test(id) values(5);
savepoint x;
insert into test(id) values(6);
rollback;
commit;
select sum(id) from test;

※This sample exam is different from those that appear in the actual OSS-DB Exam.
2024/05/02

Answer and Explanation

A transaction begins with BEGIN (or START TRANSACTION) and ends with COMMIT or ROLLBACK. If you execute COMMIT, all updates within the transaction are written to the database. However, if you execute ROLLBACK, all updates within the transaction are cancelled and the state is reverted to the state before the transaction started.

Transactions have a feature similar to temporary saving, called SAVEPOINT. You can assign a label as a savepoint name, and if you specify that label during a rollback, you can revert to the state at the time the SAVEPOINT was executed. However, this is only a temporary state, so if you don't execute COMMIT afterwards, it won't be written to the database, and if you execute ROLLBACK, it will revert to the original state.
In the SQL sequence in this example, three transactions are executed in total.

The first transaction inserts a row with id 1 and executes COMMIT.

The second transaction inserts a row with id 2, issues a SAVEPOINT, inserts a row with id 3, and then executes ROLLBACK to the SAVEPOINT, so the insertion of the row with id 3 is cancelled. Then it inserts a row with id 4 and executes COMMIT, so in the end, rows with ids 2 and 4 are inserted.

The third transaction inserts a row with id 5, issues a SAVEPOINT, inserts a row with id 6, and then executes ROLLBACK. However, since no savepoint name is specified, the entire transaction is cancelled and terminated. In other words, neither row with id 5 nor row with id 6 is inserted.

Although COMMIT is issued after ROLLBACK, it does nothing because it is outside of a transaction. It does not result in an error, but a warning message is issued.
The final SELECT statement calculates the sum of the values in the id column. As we saw above, rows with ids 1, 2, and 4 are inserted, so the sum(id) is 7.

Therefore, the correct answer is 7.

A transaction begins with BEGIN (or START TRANSACTION) and ends with COMMIT or ROLLBACK. If you execute COMMIT, all updates within the transaction are written to the database. However, if you execute ROLLBACK, all updates within the transaction are cancelled and the state is reverted to the state before the transaction started.

Transactions have a feature similar to temporary saving, called SAVEPOINT. You can assign a label as a savepoint name, and if you specify that label during a rollback, you can revert to the state at the time the SAVEPOINT was executed. However, this is only a temporary state, so if you don't execute COMMIT afterwards, it won't be written to the database, and if you execute ROLLBACK, it will revert to the original state.
In the SQL sequence in this example, three transactions are executed in total.

The first transaction inserts a row with id 1 and executes COMMIT.

The second transaction inserts a row with id 2, issues a SAVEPOINT, inserts a row with id 3, and then executes ROLLBACK to the SAVEPOINT, so the insertion of the row with id 3 is cancelled. Then it inserts a row with id 4 and executes COMMIT, so in the end, rows with ids 2 and 4 are inserted.

The third transaction inserts a row with id 5, issues a SAVEPOINT, inserts a row with id 6, and then executes ROLLBACK. However, since no savepoint name is specified, the entire transaction is cancelled and terminated. In other words, neither row with id 5 nor row with id 6 is inserted.

Although COMMIT is issued after ROLLBACK, it does nothing because it is outside of a transaction. It does not result in an error, but a warning message is issued.
The final SELECT statement calculates the sum of the values in the id column. As we saw above, rows with ids 1, 2, and 4 are inserted, so the sum(id) is 7.

Therefore, the correct answer is 7.