DBMS_Unit 5-1
DBMS_Unit 5-1
Atomicity:
• All transactions should be successful / All transactions should not be successful
• Either Successful or Failure applied to all transactions
Consistency:
• Before and after each transaction the database must be in a consistent state.
Isolation:
• Each transaction should be isolated from another one.
Durability:
• Once a transaction achieves a Committed state, it will remain committed.
• But if failure, then re-transact till getting success and after success must be committed.
ATOMICITY
Abort:
If a transaction aborts, then all the changes made are not visible.
Commit:
5. Aborted - If the transaction has failed at any point, the system will roll
back all the changes made during the transaction to maintain database
consistency.
Example: Suppose the payment went through, but the server crashed
before booking confirmation. The system will cancel the payment and
return your money, ensuring that either the entire transaction completes or
nothing changes at all. The transaction can be retried or canceled
completely.
Implementation of Atomicity
1.Undo Logging (Rollback Logs) : Before any data page is changed, the DBMS
writes an undo log record containing the “before image” of that change.
How it enforces atomicity: If a transaction aborts or the system crashes before its
commit record, the recovery process walks the log backwards, using the
before-images to undo any partial updates. Ensures that no part of an uncommitted
transaction “sticks.”
How it enforces atomicity: If the transaction fails, the DBMS simply discards the
modified in-memory map—no rollback of data pages is ever needed because the
on-disk shadow pages were never overwritten. Either the old state (shadow) or the
new state (updated map) becomes visible—nothing in between.
Implementation of Atomicity
C. Redundancy – Storage-Based
•Stores duplicate copies of data/logs across different disks or locations.
•Techniques include:
•RAID: Hardware-based redundancy (e.g., mirroring or parity) for disk failure tolerance.
•Replication: Sends live copies to standby servers or other sites.
•Backups: Periodic copies to off-site/cloud storage for disaster recovery.
•Ensures data remains safe even if hardware or entire site fails.
Single-User versus Multiuser Systems
• One criterion for classifying a database system is according to the
number of users who can use the system concurrently.
• A DBMS is single-user if at most one user at a time can use the
system, and it is multiuser if many users can use the system—and
hence access the database—concurrently. Single-user DBMSs are
mostly restricted to personal computer systems; most other DBMSs
are multiuser.
• For example, an airline reservations system is used by hundreds of
travel agents and reservation clerks concurrently. Database systems
used in banks, insurance agencies, stock exchanges, supermarkets, and
many other applications are multiuser systems. In these systems,
hundreds or thousands of users are typically operating on the data base
by submitting transactions concurrently to the system.
Concurrent execution of processes is actually
interleaved
S1 S2
T1 T2 T1 T2
Serial R(A) R(A)
Schedule W(A) W(A)
R(B)
Non-
Serial R(A)
W(B)
Always Schedule W(A)
gives R(A) R(B)
consistent W(A) W(B)
results R(B) R(B)
W(B) W(B)
Serial Schedule
• As the name says, all the transactions are executed serially one after
the other.
• In serial Schedule, a transaction does not start execution until the
currently running transaction finishes execution.
• This type of execution of the transaction is also known as non-
interleaved execution.
• Serial Schedules are always recoverable, cascades, strict, and
consistent. A serial schedule always gives the correct result.
S2
T1 T2
R(A) T1 T2
W(A)
R(A) NO CYCLE MADE, Therefore the schedule can be
transformed into a serial schedule by swapping non-
W(A)
conflicting operations.
R(B)
W(B) If the precedence graph (serialization graph) has
R(B)
no cycle, then the schedule is conflict
W(B)
serializable.
How to Convert -> Swapping of non-
conflicting operations
S2 S2
T1 T2 T1 T2
R(A) R(A) Swap only when:
W(A) W(A)
R(A) R(B) •Operations were on
W(A) W(B)
different data items.
R(B) R(A)
W(B) W(A)
•No read-write or
R(B) R(B) write-write conflict.
W(B) W(B)
Network failures (Internet connection lost), System crashes (Server or database software suddenly stops), Natural
disasters (Floods, earthquakes damaging servers), Human carelessness (Deleting important files by mistake), Sabotage
(Someone intentionally harming the database), Software errors (Bugs or mistakes in the database program)
Failure
Logical Errors
System Errors
Failure Classification
1. Transaction Failure - A transaction failure happens when a transaction cannot
continue or complete its task.
Reasons for transaction failure:
• Logical Error: Mistakes in the code or internal faults that stop the transaction.
• System Error: The database system itself stops the transaction due to issues like
deadlock or resource unavailability.(Example: System detects two transactions waiting
for each other forever.)
2. System Crash - A system crash occurs due to hardware or software breakdown or
external problems like: Transaction failure, Operating system errors, Power outages, Main
memory failure.
• These failures are called soft failures and mostly affect volatile memory (like
RAM).Usually, non-volatile memory (like hard disks) remains safe — this assumption is
called the fail-stop assumption
3. Data-Transfer Failure - A data-transfer failure happens when a problem occurs during
the transfer of data between memory and disk.
Reasons for data-transfer failure: Disk head crash, Read/write errors
For solution, Keep regular backups of your data on other storage devices to recover
quickly if a failure happens.
Storage
• Databases are stored in file formats, which contain records. At
physical level, the actual data is stored in electromagnetic format on
some device. These storage devices can be broadly categorized into
three types −
Storage
• Primary Storage - Primary storage is the fastest and closest memory
to the CPU (RAM, Cache)
Features:
Usage in DBMS:
• Running transactions
• Buffering database pages
• Executing queries
Storage
• Secondary Storage - Secondary storage is used to permanently store
data even if power is lost. (Hard Disk Drives (HDD), Solid State
Drives (SSD),CDs, DVDs, Flash Drives (Pen drives)
Features:
Usage in DBMS:
Usage in DBMS:
SQL is a single query that is used to perform PL/SQL is a block of codes that used to write the
DML and DDL operations. entire program blocks/ procedure/ function, etc.
It is declarative, that defines what needs to be PL/SQL is procedural that defines how the things
done, rather than how things need to be done. needs to be done.
2.Named Block - These are stored in the database for reuse and can
be:
• Procedures
• Functions
• Packages
•Triggers
Writing a basic program
Use Case : Addition of 2 numbers
DECLARE
n1 NUMBER(10);
n2 NUMBER (10);
BEGIN
n1 := 6;
n2:= 5;
END;
/
Writing a basic program
Use Case : Addition of 2 numbers
DECLARE
n1 NUMBER(10) := 10;
n2 NUMBER(10) := 20;
BEGIN
DBMS_OUTPUT.PUT_LINE('The addition of given numbers is ' || (n1 + n2));
END;
/
Writing a basic program
Use Case : Product of 2 numbers
DECLARE
n1 NUMBER(10) := 10;
n2 NUMBER(10) := 20;
BEGIN
DBMS_OUTPUT.PUT_LINE('The multiplication of given numbers is '
|| (n1*n2));
END;
/
Writing a basic program
Use Case : Show Product of 2 numbers. If results are greater than 100, print High
Result value and lower than 100 print Low Result value.
DECLARE
n1 NUMBER(10) := 10;
n2 NUMBER(10) := 20;
BEGIN
IF (n1 * n2) > 100 THEN
DBMS_OUTPUT.PUT_LINE(‘High Product Value: ' || (n1 * n2));
ELSE
DBMS_OUTPUT.PUT_LINE(‘Low Product Value: ' || (n1 * n2));
END IF;
END;
/
Writing a basic program
Use Case : Calculate the total cost of items by multiplying quantity and price. If
the total cost exceeds a budget of 1000, display ‘Budget exceeded’. Otherwise,
show ‘Total cost is within budget’.
DECLARE
quantity NUMBER := 15;
price NUMBER := 70;
BEGIN
IF (quantity * price) > 1000 THEN
DBMS_OUTPUT.PUT_LINE(‘Budget exceeded’);
ELSE
DBMS_OUTPUT.PUT_LINE(‘Total cost is within budget’);
END IF;
END;
/
Writing a basic program: Usage of SELECT
SELECT name, marks
FROM students_125 DECLARE
WHERE student_id = 101; v_name students_125.name%TYPE;
v_marks students_125.marks%TYPE;
BEGIN
SELECT name, marks
INTO v_name, v_marks
FROM students_125 WHERE student_id = 101;
DBMS_OUTPUT.PUT_LINE('Name: ' || v_name);
DBMS_OUTPUT.PUT_LINE('Marks: ' || v_marks);
END;
/
Writing a basic program: Usage of SELECT
SELECT department
DECLARE
FROM students_125 v_dept students_125.department%TYPE;
WHERE student_id = 102;
BEGIN
SELECT department
INTO v_dept
FROM students_125
WHERE student_id = 102;
END;
/
Control Structures
Control structures allow to control the flow of execution in PL/SQL
programs. PL/SQL has three categories of control statements:
1. Conditional selection statements : It runs different statements for
different data values.
The conditional statements are IF statement and the CASE statement.
2. Loop statements : It run the same statements with a series of
different data values.
The loop statements are the basic LOOP, FOR LOOP, and WHILE LOOP.
3. Sequential control statements : The sequential control statements are
GOTO, which goes to a specified statement, and NULL, which does
nothing.
Source : - https://docs.oracle.com/en/database/oracle/oracle-database/19/lnpls/plsql-control-
statements.html#GUID-18777904-23F6-4F6D-8B41-46BABF00BA03
Control Structures
1. Conditional selection statements
a. IF Statement : The IF statement is used to execute a block of code
based on certain conditions.
The IF statement has these forms:
• IF THEN
• IF THEN ELSE
• IF THEN ELSIF
IF condition THEN
--- statement----
END IF;
Source : - https://docs.oracle.com/en/database/oracle/oracle-database/19/lnpls/plsql-control-
statements.html#GUID-18777904-23F6-4F6D-8B41-46BABF00BA03
Control Structures
1. Conditional selection statements
b. CASE Statement
The CASE statement provides a way to handle multiple conditions more
efficiently than nested IF statements.
END;
-----To call-----
BEGIN
greet_user('Rahul');
END;
/
Functions
A FUNCTION in PL/SQL is a stored program that accepts parameters, performs
operations, and returns a single value.
Types of Functions
1. Pre-defined/ Built-in functions
A. Scalar - Scalar functions operate on single values and return a single value
of a specified type (e.g., string, number, date).
2. Explicit Cursor
• Created and controlled manually using DECLARE, OPEN, FETCH, and CLOSE.
• Used when the SELECT statement returns multiple rows.
• Gives more control over row-by-row processing.
SET SERVEROUTPUT ON;
DECLARE
CURSOR cur_students IS
SELECT Name, Age FROM Students_125;
Demo Program
v_name Students_125.Name%TYPE;
v_age Students_125.Age%TYPE;
BEGIN
USE CASE:
OPEN cur_students;
LOOP
FETCH cur_students INTO v_name, v_age;
Print the names of all
EXIT WHEN cur_students%NOTFOUND;
students from the
IF v_age > 20 THEN Students_125 table who
DBMS_OUTPUT.PUT_LINE(v_name || ' is above 20'); are older than 20, using a
END IF; row-by-row check with a
END LOOP; cursor.
CLOSE cur_students;
END;
/
THANK YOU!