DBMS4
DBMS4
Because regular comparison operators don't work with NULL, SQL provides:
A nested query (also called a subquery) is a query inside another SQL query.
2. Correlated Subquery
● The UNIQUE keyword checks whether all rows in a subquery are different (no duplicates).
● It returns:
Important Notes:
● Two rows are considered duplicates only if all values are the same (including NULLs).
● If any value is different, the rows are unique.
● If NULL is present, SQL treats NULL ≠ NULL → so rows with NULLs are usually not
considered duplicates.
● GROUP BY groups rows with the same value in one or more columns.
An assertion is a constraint that is used to ensure a particular condition holds true across multiple rows or
even multiple tables in a database. It is generally used to enforce business rules or logical conditions that
span across the dataset.
CREATE ASSERTION c1
CHECK (NOT EXISTS (SELECT * FROM customer WHERE balance < 1000));
● In this example, we are ensuring that no customer has a balance less than 1000 in the customer
table.
● Condition: If there is any row in the customer table where the balance is less than 1000, the
assertion is violated, and the INSERT or UPDATE operation is stopped.
How it works:
1. When a change is made to the database (via INSERT or UPDATE), the system checks the
assertion condition.
2. If the query results in violating the assertion (i.e., creating a balance less than 1000 in the
customer table), the operation is aborted.
3. The assertion ensures that only valid data is allowed in the database based on the condition
defined.
Working of Trigger:
A trigger is a set of SQL statements that automatically execute (or "fire") when a specific event occurs on
a table, such as an INSERT, UPDATE, or DELETE action.
● Trigger Workflow:
1. Event: The trigger is activated by an event, such as data modification (INSERT,
UPDATE, DELETE).
2. Condition: Optionally, a condition is evaluated. If the condition is true, the trigger's
action is executed. If no condition is specified, the action is executed unconditionally.
3. Action: The action is the SQL statements or procedure that will be executed
automatically when the event occurs and the condition (if any) is met.
AFTER INSERT
ON student
BEGIN
UPDATE student
WHERE id = NEW.id;
END;
Event: This trigger is set to fire AFTER an INSERT operation on the student table.
Condition: There’s no condition here, so it will fire every time an insert happens on the student table.
Action: After inserting a new row into the student table, the trigger will automatically concatenate the
first_name and last_name columns to update the full_name column for the newly inserted row.
Assertion:
● Ensures a condition holds true across the database before modifying data (via INSERT or
UPDATE).
● Can span multiple rows or tables.
● Checks conditions globally.
● Example: Ensuring no customer has a balance below 1000.
Trigger:
● Executes a predefined set of SQL statements automatically when a data modification event
occurs.
● Can be associated with specific tables.
● The action is fired on a row-by-row basis.
● Example: Automatically populating a full_name field after inserting a new student record.
6. Views
A view in SQL is a virtual table based on the result of a SQL SELECT query. It doesn't store data
physically; instead, it fetches data from one or more underlying base tables. Views help in simplifying
queries, restricting access to specific data, and providing a level of abstraction.
2. View Creation:
To create a view, use the CREATE VIEW statement. A view can be based on one or multiple tables.
FROM table_name
WHERE condition;
● Query Modification: Translates the view query into an equivalent query on base tables each time
it's accessed.
● View Materialization: Physically stores the view's result temporarily and updates it periodically
or on demand.
● It does not contain DISTINCT, GROUP BY, ORDER BY, or aggregate functions.
Ensures that any inserted or updated rows remain valid for the view. Prevents inserting data that violates
the view’s condition.
SELECT ...
FROM ...
WHERE condition
6. DROP VIEW:
Syntax:
In a multi-user database system, multiple transactions may access the same data simultaneously.
Without concurrency control, this can lead to:
Expected result:
Final value of X = 80 - 5 - 4 = 71
But due to interleaving, errors may occur as explained below.
2. Temporary Update (Dirty Read) Problem:
Occurs when a transaction reads data written by another uncommitted transaction. If the writing
transaction fails, the reading one has read invalid (dirty) data.
🧠 Example:
● T1 updates balance to ₹5000.
● T2 reads the balance = ₹5000.
● T1 crashes → the update should not persist.
● But T2 has already used the incorrect data.
Occurs when a transaction is calculating a summary (like SUM) while other transactions are
modifying the data.
🧠 Example:
● T1 is computing total reserved seats.
● T2 reserves/cancels seats simultaneously.
● T1 reads partial old/new data → wrong total.
4. Unrepeatable Read Problem:
Occurs when a transaction reads the same data twice but gets different results due to updates from
other transactions in between.
🧠 Example:
● T1 reads X = 80.
● T2 reserves 5 seats → updates X = 75.
● T1 reads X again = 75.
T1 gets different values for the same item during its execution.
Q. Explain Transaction State Diagram in DBMS with all transaction states and operations.
Definition of a Transaction:
A transaction is a sequence of operations performed as a single logical unit of work. It must follow the
ACID properties:
● Atomicity
● Consistency
● Isolation
● Durability
1. Active State:
3. Committed State:
4. Failed State:
5. Terminated State:
Q.ACID Property
The ACID properties ensure that database transactions are reliable, accurate, and recoverable, even in
the presence of failures or concurrent transactions.
1. Atomicity:
● Definition: A transaction is treated as a single indivisible unit. Either all operations are
performed or none at all.
● Example:
Suppose you transfer ₹500 from Account A to Account B.
2. Consistency:
● Definition: A transaction must take the database from one valid state to another, maintaining
data integrity and constraints.
● Example:
If the total money in the bank before the transaction is ₹10,000, and we transfer ₹500 from A to
B, the total should still be ₹10,000 after the transaction.
If integrity rules (e.g., balance ≥ 0) are violated, the transaction is rolled back to maintain
consistency.
3. Isolation:
4. Durability:
● Definition: Once a transaction commits, all its changes are permanently saved, even in case of
system failure.
● Example:
After transferring ₹500 from A to B and getting a "Transaction Successful" message, a power
cut occurs.
When the system restarts, the transaction must still be reflected in the database.
Q. Conflict Serializability – Explained
🔶 2. What is a Conflict?
Two operations conflict if:
3. Conflict Serializability
Q.