Thursday, 25 September 2025
Database Connectivity (Frontend + Middleware + Backend)
Tuesday, 16 September 2025
Database Trigger (All Types: Row level and Statement level triggers, Before and After
Database Trigger (All Types: Row level and Statement level triggers, Before and After Triggers)
Problem Statement:
Write a database trigger on Library table. The System should keep track of the records that are being updated or deleted. The old value of updated or deleted records should be added in Library_Audit table.
Note: Instructor will Frame the problem statement for writing PL/SQL block for all types of Triggers in line with above statement.
"Write a database trigger on the Library table that maintains an audit log. Whenever a record is updated or deleted, the old values of that record should be inserted into the Library_Audit table. The trigger should differentiate between update and delete operations and store additional details like the type of operation and the timestamp."
Let's Study First What is Trigger? Why do we need Trigger? How to implement Trigger?
Note: Instructor will Frame the problem statement for writing PL/SQL block for all types of Triggers in line with above statement.
"Write a database trigger on the Library table that maintains an audit log. Whenever a record is updated or deleted, the old values of that record should be inserted into the Library_Audit table. The trigger should differentiate between update and delete operations and store additional details like the type of operation and the timestamp."
let's understand what we want to do here in this.
You have a Library table in a database.
Whenever a record in this table is UPDATED or DELETED, you don’t want to lose the old information.
So, you need to:
Write a database trigger that automatically captures the old values before they are changed or removed.
Insert those old values into another table called Library_Audit (this act like a log/history table).
This way:
If someone updates a book’s details → the previous details are saved in Library_Audit.
If someone deletes a book record → the deleted record is also saved in Library_Audit.
But why this is needed?
Audit trail: Helps to keep track of who changed or removed data.
Data recovery: If someone deletes a row by mistake, you still have the backup in Library_Audit.
Accountability: Ensures transparency in changes to critical data.
Let's start the Implementation part:
Since we want all types of triggers, we’ll cover:
1. Row-level trigger
2. Statement-level trigger
3. BEFORE trigger
4. AFTER trigger
5. INSTEAD OF trigger (used for views)
First, we have to create two tables 1. Library and 2. Library_Audit common for all types of triggers.
Thursday, 11 September 2025
What is Cursor? Why do we need cursor? Example.
Q. What is a Cursor?
Ans:
A cursor is a pointer/handle to the result set of a query in PL/SQL.
It lets us process rows one by one (row-by-row iteration).
Think of it like:
👉 SQL gives you a whole table of results.
👉 A cursor lets you pick each row, work with it, then move to the next.
Q. Why Do We Need Cursors?
Ans:
SQL by itself works in sets (all rows at once).
Example:
UPDATE students SET grade='A' WHERE marks > 90.
→ These updates all qualifying rows in one go.
But PL/SQL is procedural.
Sometimes you need to:
1. Fetch one row, process it, make a decision
2. Then move to next row, apply a different logic
👉 In such cases, we require cursors.
Example Without Cursor (Problem Case)
Suppose we have a table Students (roll_no, name, marks)
Now we want to give grades like:
Marks ≥ 90 → Grade 'A'
Marks 75–89 → Grade 'B'
Marks < 75 → Grade 'C'
We cannot write one simple SQL UPDATE because logic is conditional per row.
Notes in Detail:
Problem Statement:
Cursors: (All types: Implicit, Explicit, Cursor FOR Loop, Parameterized Cursor)
Write a PL/SQL block of code using parameterized Cursor that will merge the data available in the newly created table N_RollCall with the data available in the table O_RollCall. If the data in the first table already exist in the second table, then that data should be skipped.
Note: Instructor will frame the problem statement for writing PL/SQL block using all types of Cursors in line with above statement.
Algorithm:
Let's start the Implementation of above problem statement as follows:
1. We need to create two tables N_RollCall (Rno, Sname, Per) and O_RollCall (Rno, Sname, Per) with same attributes.
2. Insert the 3-4 records inside the O_RollCall table after that Insert 4-8 records inside the N_RollCall table out of which 3 records keep same as it is inside the O_RollCall tables.
3. First try to understand what Implicit Cursor is and how it works?
4. Second try to implement Explicit Cursor.
5. Third try to implement Cursor FOR Loop example.
6. Forth try to implement Parameterized Cursor that will merge the data available in the newly created table N_RollCall with the data available in the table O_RollCall.
6.1 Apply the logic: If the data in the first table already exist in the second table, then that data should be skipped.
Implementation:
1. We need to create two tables N_RollCall (Rno, Sname, Per) and O_RollCall (Rno, Sname, Per) with same attributes.
2. Insert the 3-4 records inside the O_RollCall table after that Insert 4-8 records inside the N_RollCall table out of which 3 records keep same as it is inside the O_RollCall tables.
Here, if we observe carefully in N_RollCall table two records 101 and 103 already present inside the O_RollCall table. so, by skipping these two records we want to merge remaining two records 102 and 104 inside the O_RollCall table.
3. First try to understand what Implicit Cursor is and how it works?
4. Second try to implement Explicit Cursor.
How to find candidate key before Normalization?
Finding candidate keys is the foundation before we normalize a relation (2NF, 3NF, BCNF)
Q. What is a Candidate Key?
Ans: A candidate key is a minimal set of attributes that can uniquely identify every tuple (row) in a relation. "Minimal" means: if you remove any attribute from it, it will no longer uniquely identify.
Q. What are the steps to find candidate key?
Steps to Find Candidate Keys
1. List all attributes in the relation.
2. List given Functional Dependencies (FDs).
3. Find closure of attributes (denoted as X⁺ = all attributes X can determine).
4. If X⁺ = all attributes in relation → X is a superkey.
5. Check minimality: if no proper subset of X is a superkey → X is a candidate key.
Let's try to understand this with simple example
Example:
Given:
Relation with attributes and its functional dependencies are given here in case study.
Relation R (A, B, C, D, E)
Functional Dependencies (FDs):
A → B
B → C
CD → E
E → A
Now apply above steps to find out Candidate Key from the above relation.
Step 1: Check closures
A⁺ = {A, B, C} (from A→B, then B→C) → not all attributes.
B⁺ = {B, C} → not all.
C⁺ = {C} → not all.
D⁺ = {D} → not all.
E⁺ = {E, A, B, C} (E→A, then A→B, B→C) → missing D → not all.
Step 2: Try combinations
AE⁺ = {A, B, C, E} → still missing D.
AD⁺ = {A, B, C, D} → missing E.
ED⁺ = {E, A, B, C, D} → this gives all attributes → superkey.
Step 3: Check minimality
From ED,
Remove E: D⁺ = {D} → not all.
Remove D: E⁺ = {E, A, B, C} → not all.
→ So, ED is minimal → Candidate Key.
Step 4: Verify if there are others
Try CD⁺ = {C, D, E, A, B} → also gives all → Candidate Key.
So, candidate keys are:
{ED}, {CD}
Q. Why Candidate Key helps in Normalization?
Ans:
Candidate keys help us identify prime attributes (those that are part of any key).
In higher normalization (2NF, 3NF, BCNF), we check FDs based on candidate keys and prime attributes
Monday, 8 September 2025
PL / SQL Unnamed PL SQL code block Assignment
Problem Statement:
Now the first question comes to our mind is how to implement this code?
1. Which Software I have to use for implementation, if I don't have that software how to install it in my computer so that I can implement the same.
2. How do we use this software for implementation of above PL/ SQL assignment.
2.2 In the above command prompt if we carefully observe then we come to know that we have successfully connected the Oracle free pluggable database container CDS$ROOT. as a system database administrator so we have all rights which administrator have.