CSE 344 Section 6
Transactions
BCNF
Boyce-Codd Normal Form (BCNF)
A relation R is in BCNF if every set of attributes in R is either a superkey or its closure is the same set (trivial FD).
*that is, either {a}+ -> a OR {a}+ -> {all cols}, where a is any col in the table
Boyce-Codd Normal Form (BCNF)
Not BCNF! SSN is not a trivial FD or a superkey
BCNF Decomposition
Algorithm
C = the set of all attributes in the relation R
Look for an attribute (or a set of attributes) that meets the following conditions (non-BCNF flag!):
If such attribute exists, it is a violation of the BCNF condition; decompose R:
Else, the table is in BCNF
BCNF
Example
Relation: R (A, B, C, D, E)
FDs:
Goal: Decompose R into BCNF
BCNF
Example
R (B, C, A)
R (B, C, D)
R (A, E)
R (A, B, C, D)
R (A, B, C, D, E)
A+ = {A, E}
BCNF compliant
BCNF compliant
BCNF compliant
{B, C}+ = {B, C, A}
BCNF
Example
Notice that {A}+ = {A,E}, which violates the BCNF condition
{B,C}+ = {B,C,A} violates the BCNF condition
Transactions
ACID
Atomic�Consistent�Isolated�Durable
Everything a concurrent database user wants…
How do ACID properties help us?
Atomicity
What? “Atom” - a transaction is indivisible; data operations inside the transaction work in an all or nothing fashion
How? Transaction commit and rollback
Example: Transfer of funds from one account to another involves an atomic transaction that includes operations of debit and credit
Consistency
What? Only data operations that comply with database validity constraints are allowed.
Once a transaction is completed, it must not leave the database incomplete or inconsistent.
How? Enforcement of consistency rules upon data entry/updation
Example: A database having both first and last name fields will not accept if you enter only one of those - must enter both for the transaction to be complete
Isolation
What? The appearance of a transaction happening by itself irrespective of what other transactions are happening concurrently
How? User views the database that was present right before the view request is made and any further transactions that are in process after it are ignored
Example: A teller looking up a balance must be isolated from a concurrent transaction involving a withdrawal from the same account
Durability
What? Once a transaction is complete the information as changed will survive failures of any kind
How? Creation of data “mirrors”
Example: Trying to make an update that leads to a system failure should not tamper with any of the previous data and transactions
Txn Perspective
A database is a collection of "elements" that can be written to or read from.
Operation - A read or write to an element (later: or an insert or delete)
Example: What operation does the following SQL query contain?�SELECT cases FROM Country WHERE cname = ‘France’;
Txn Perspective
A database is a collection of "elements" that can be written to or read from.
Operation - A read or write to an element (later: or an insert or delete)
Example: What operation does the following SQL query contain?�SELECT cases FROM Country WHERE cname = ‘France’;
Answer: R(Country) or R(France), depending on whether the "element" is a table or a row (assuming cname is a key). Let's go with the finer-grained element.
Definitions
Operation -
Transaction -
Schedule -
Serial schedule -�
Serializable schedule - �
Definitions
Operation - read or write of an element (later: insert or delete)
Transaction - series of operations meant to have the ACID guarantees
Schedule - ordering of the operations of some txns
Serial schedule - schedule where each txn is executed one after another.� No interleaving
Serializable schedule - a schedule that is behaviorally equivalent � to a serial schedule
Practice - convert SQL to operations
Txn 1:
1a) x = SELECT cases FROM Country WHERE cname = ‘France’;
1b) x = x + 200;
1c) UPDATE Country SET cases = `x` WHERE cname = ‘France’;
Txn 2:
2a) y = SELECT cases FROM Country WHERE cname = ‘Spain’;
2b) y = y / 5;
2c) z = SELECT cases FROM Country WHERE cname = ‘France’;
2d) UPDATE Country SET cases = `y + z` WHERE cname = ‘France’;
Practice - convert SQL to operations
Txn 1:
1a) x = SELECT cases FROM Country WHERE cname = ‘France’;
1b) x = x + 200;
1c) UPDATE Country SET cases = `x` WHERE cname = ‘France’;
Txn 2:
2a) y = SELECT cases FROM Country WHERE cname = ‘Spain’;
2b) y = y / 5;
2c) z = SELECT cases FROM Country WHERE cname = ‘France’;
2d) UPDATE Country SET cases = `y + z` WHERE cname = ‘France’;
1a) R(France)
1b)
1c) W(France)
2a) R(Spain)
2b)
2c) R(France)
2d) W(France)
Conflict Serializability
Definitions
Non-conflicting swap - �
Conflicting swap -
Conflict serializable schedule - �
Definitions
Non-conflicting swap - a pair of adjacent operations in a schedule � that can be reversed without affecting the schedule's behavior
Conflicting swap - (any two ops in same txn), RW, WR, WW
Conflict serializable schedule - a schedule that can be transformed � into a serial schedule via a series of non-conflicting swaps
Conflict Serializability
Checking for conflict serializability -> precedence graph and cycle checking
Conflict Serializability
Checking for conflict serializability -> precedence graph and cycle checking
Serializability
“Conflict serializable” is a stronger constraint than “serializable”
I.e. Any schedule that is conflict serializable must be serializable.
Serializable
Conflict
Serializable
Easy to check
Not easy to check
Serializability
S1: w1(Y); w2(Y); w1(X); w2(X); w3(X)
S2: w1(Y); w2(Y); w2(X); w1(X); w3(X)
Are these serializable? Conflict serializable?
Serializability
S1: w1(Y); w2(Y); w1(X); w2(X); w3(X)
S2: w1(Y); w2(Y); w2(X); w1(X); w3(X)
Conflict Serializable
Serializability
S1: w1(Y); w2(Y); w1(X); w2(X); w3(X)
S2: w1(Y); w2(Y); w2(X); w1(X); w3(X)
Conflict Serializable
Not Conflict Serializable, but Serializable (depending on our definition of write)
Worksheet