dbms 3
dbms 3
Class DBMS
transaction query
schema : acc
tables: accnt
BEGIN TRANSACTION;
UPDATE accounts.accountdetails
SET balance = balance - 5000
WHERE accountnumber = 'I89012';
UPDATE accounts.accountdetails
SET balance = balance + 5000
WHERE accountnumber = 'E45678';
COMMIT;
IF (SELECT balance FROM accounts.accountdetails WHERE accountnumber = 'I89012') <
BEGIN
PRINT 'Insufficient funds';
ROLLBACK;
RETURN;
END
UNIT 3 1
Why Normalize Database?
Eliminate redundant data
1 John Math
1 John Physics
1 John Chemistry
UNIT 3 2
StudentID,
Name,
TRIM(value) AS Subject
FROM
OriginalTable
CROSS APPLY STRING_SPLIT(Subjects, ','); -- STRING_SPLIT splits comma-separated v
UNIT 3 3
DROP TABLE StudentSubjects;
EXEC sp_rename 'CleanedStudentSubjects', 'StudentSubjects';
UNIT 3 4
2. Second Normal Form (2NF)
Rules for 2NF:
Should be in 1NF
All non-key attributes should be fully functionally dependent on the primary key
1 P1 Laptop 2
2 P1 Laptop 3
Table 1: Orders
1 P1 2
2 P1 3
Table 2: Products
Product_ID Product_Name
P1 Laptop
Should be in 2NF
1 D1 Computer Science
2 D1 Computer Science
UNIT 3 5
Table 1: Students
Student_ID Department_ID
1 D1
2 D1
Table 2: Departments
Department_ID Department_Name
D1 Computer Science
Benefits of Normalization
Better database organization
Easier maintenance
When to Denormalize?
Sometimes, denormalization might be necessary for:
Note: The level of normalization needed depends on your specific use case and requirements. Not all
databases need to be normalized to 5NF.
A transaction is a unit of program execution that accesses and possibly updates various data items.
Operations of Transaction
A user can make different types of requests to access and modify the contents of a database. So, we
have different types of operations relating to a transaction. They are discussed as follows:
i) Read(X)
UNIT 3 6
A read operation is used to read the value of X from the database and store it in a buffer in the main
memory for further actions such as displaying that value.
Such an operation is performed when a user wishes just to see any content of the database and not
make any changes to it.
For example, when a user wants to check his/her account's balance, a read operation would be
performed on user's account balance from the database.
ii) Write(X)
A write operation is used to write the value to the database from the buffer in the main memory. For
a write operation to be performed, first a read operation is performed to bring its value in buffer, and
then some changes are made to it, e.g. some set of arithmetic operations are performed on it
according to the user's request, then to store the modified value back in the database, a write
operation is performed.
For example, when a user requests to withdraw some money from his account, his account balance
is fetched from the database using a read operation, then the amount to be deducted from the
account is subtracted from this value, and then the obtained value is stored back in the database
using a write operation.
iii) Commit
This operation in transactions is used to maintain integrity in the database. Due to some failure of
power, hardware, or software, etc., a transaction might get interrupted before all its operations are
completed. This may cause ambiguity in the database, i.e. it might get inconsistent before and after
the transaction.
To ensure that further operations of any other transaction are performed only after work of the
current transaction is done, a commit operation is performed to the changes made by a transaction
permanently to the database.
iv) Rollback
This operation is performed to bring the database to the last saved state when any transaction is
interrupted in between due to any power, hardware, or software failure.
In simple words, it can be said that a rollback operation does undo the operations of transactions
that were performed before its interruption to achieve a safe state of the database and avoid any
kind of ambiguity or inconsistency.
Properties of Transaction
Transactions follow 4 properties, namely, Atomicity, Consistency, Isolation, and Durability.
i) Atomicity
This property ensures that either all operations of a transaction are executed or it is aborted. In any
case, a transaction can never be completed partially.
Each transaction is treated as a single unit (like an atom). Atomicity is achieved through commit and
rollback operations, i.e. changes are made to the database only if all operations related to a
transaction are completed, and if it gets interrupted, any changes made are rolled back using
rollback operation to bring the database to its last saved state.
ii) Consistency
UNIT 3 7
This property of a transaction keeps the database consistent before and after a transaction is
completed.
Execution of any transaction must ensure that after its execution, the database is either in its prior
stable state or a new stable state.
In other words, the result of a transaction should be the transformation of a database from one
consistent state to another consistent state.
Consistency, here means, that the changes made in the database are a result of logical operations
only which the user desired to perform and there is not any ambiguity.
iii) Isolation
This property states that two transactions must not interfere with each other, i.e. if some data is
used by a transaction for its execution, then any other transaction can not concurrently access that
data until the first transaction has completed.
It ensures that the integrity of the database is maintained and we don't get any ambiguous values.
Thus, any two transactions are isolated from each other.
iv) Durability
This property ensures that the changes made to the database after a transaction is completely
executed, are durable.
It indicates that permanent changes are made by the successful execution of a transaction.
In the event of any system failures or crashes, the consistent state achieved after the completion of
a transaction remains intact. The recovery subsystem of DBMS is responsible for enforcing this
property.
ACID in a Nutshell
Property Ensures Example
Consistency Database remains valid Constraints like "no negative balance" are preserved.
No interference in
Isolation Concurrent transactions appear to run serially.
execution
Durability Changes are permanent After committing a transfer, the data persists through failures.
transaction states
During the complete process of a transaction, there are a lot of states which are described below:
Active State:
When the transaction is going well without any error, then this is called an active state. If all the
operations are good, then it goes to a partially committed state, and if it fails, then it enters into a failed
state.
UNIT 3 8
All the changes in the database after the read and write operation needs to be reflected in permanent
memory or database. So, a partially committed state system enters into a committed state for the
permanent changes, and if there is any error, then it enters into a failed state.
Failed State:
If there is any error in hardware or software which makes the system fail, then it enters into the failed
state. In the failed state, all the changes are discarded, and the system gets its previous state which was
consistent.
Advertisement
Aborted State:
If there is any failure during the execution, then the system goes from failed to an aborted state. From an
aborted state, the transaction will start its execution from a fresh start or from a newly active state.
Committed State:
If the execution of a transaction is successful, the changes are made into the main memory and stored
in the database permanently, which is called the committed state.
Terminated State:
If the transaction is in the aborted state(failure) or committed state(success), then the execution stops,
and it is called the terminated state.
serializability
A serializability schedule in DBMS ensures that concurrent transactions produce the same final result as
if they were executed one after the other. It also helps to maintain the data consistency and integrity
2. Serial Schedule: A serial schedule is one where the operations of one transaction are executed
completely before the operations of another transaction begin. There is no interleaving of
operations.
UNIT 3 9
Example of a Serial Schedule:
In this schedule, T1 runs first, and after it is finished, T2 begins. There are no overlapping
operations.
3. Non-Serial Schedule: A non-serial schedule involves the interleaving of operations from multiple
transactions. In these schedules, transactions are executed concurrently, and their operations may
overlap.
T1: Read(A)
T2: Write(B)
T1: Write(A)
T2: Read(B)
This type of serializability can be determined by drawing a precedence graph of the transactions
in the schedule and checking if the graph is acyclic. If the graph is acyclic, then the schedule is
conflict serializable. If it contains a cycle, then the schedule is not conflict serializable.
Example
Consider a schedule with three transactions, T1, T2, and T3, and the following set of operations:
The operations that are performed in the transactions are R(Read) and W(Write) operations on
the data items ‘P’ and ‘Q’.
In a precedence graph, the edges represent the ordering constraints between transactions in the
concurrent execution of a database system. If we encounter any conflicts between transactions,
then we will draw an edge. The conflict serializability of the schedule can be determined by
constructing a precedence graph as follows:
T1 -> T2
T2 -> T3
T3 -> T1
UNIT 3 10
The graph has a cycle (T1 -> T2 -> T3 -> T1), indicating that the schedule is not conflict
serializable.
2. View Serializability:
A schedule is view serializable if the final outcome (the view) is the same as a serial
execution, without necessarily being able to transform it into a serial schedule by swapping
operations.
The concept of view equivalence ensures that each transaction sees the same data as it
would in a serial execution.
View serializability can be determined by constructing a view serializable schedule graph (VSG)
of the transactions in the schedule. The VSG contains nodes representing the transactions and
edges representing conflicts and views between the transactions. If the VSG is acyclic, then the
schedule is view serializable.
Example
Consider a schedule with three transactions, T1, T2, and T3, and the following set of operations:
The operations that are performed in the transactions are R(Read) and W(Write) operations on
the data items ‘P’ and ‘Q’. In a VSG, the edges represent the ordering constraints between
transactions in the concurrent execution of a database system. It ensures that the transactions
are view serializable.
We must first identify the view equivalent schedules for the given concurrent execution to obtain
the edges in this graph. Two schedules are view equivalent if they produce the same set of
database states for any initial database state. The view serializability of the schedule can be
determined by constructing a VSG as follows:
T1 -> T2
T2 -> T3
T3 -> T1
UNIT 3 11
The VSG has a cycle (T1 -> T2 -> T3 -> T1), indicating that the schedule is not view serializable.
Functional Dependency
The functional dependency is a relationship that exists between two attributes. It typically exists
between the primary key and non-key attribute within a table.
1. X → Y
The left side of FD is known as a determinant, the right side of the production is known as a dependent.
For example:
Here Emp_Id attribute can uniquely identify the Emp_Name attribute of employee table because if we
know the Emp_Id, we can tell that employee name associated with it.
1. Emp_Id → Emp_Name
Example:
UNIT 3 12
Employee_Id is a subset of {Employee_Id, Employee_Name}.
Also, Employee_Id → Employee_Id and Employee_Name → Employee_Name are trivial dependencies too.
Example:
1. ID → Name,
2. Name → DOB
3. Transitive Dependency:
A transitive dependency exists when an attribute depends on another attribute through a third
attribute.
In this case, knowing the AccountID will also tell us the BranchLocation indirectly through the
BranchCode.
3. Multivalued Dependency:
A multivalued dependency exists when an attribute in a relation is dependent on another, but not in
a simple functional way. It occurs when one attribute determines a set of values for another
attribute.
Example: If an account has multiple contact numbers, the AccountID would determine multiple
contact numbers, but the relationship between AccountID and ContactNumbers is not a simple one-
to-one functional dependency.
Augmentation: If the A -> B dependency is valid, adding multiple elements to either side of the
dependency will not affect the dependency.
Transitivity: If the functions X → Y and Y → Z are both valid, then X → Z is also valid according to
the transitivity rule.
Decomposition rule Its reverse of Rule 4. If X → YZ, then X → Y, and X → Z are true
UNIT 3 13