Module 4_ Database Management Systems - Exam Notes
Module 4_ Database Management Systems - Exam Notes
○ Example (Q18):
SELECT Fname, Lname
FROM EMPLOYEE
WHERE Super_ssn IS NULL;
○ Example:
SELECT DISTINCT Essn
FROM WORKS_ON
WHERE (Pno, Hours) IN (SELECT Pno, Hours FROM WORKS_ON WHERE Essn =
'123456789');
● Set/Multiset Comparison Operators: = ANY, = SOME, >, >=, <, <=, <> combined
with ANY or SOME, and ALL.
○ Syntax:
SELECT column_name(s)
FROM table_name
WHERE value comparison_operator ANY (SELECT column_name FROM
another_table WHERE condition);
SELECT column_name(s)
FROM table_name
WHERE value comparison_operator ALL (SELECT column_name FROM
another_table WHERE condition);
○ Example:
SELECT Lname, Fname
FROM EMPLOYEE
WHERE Salary > ALL (SELECT Salary FROM EMPLOYEE WHERE Dno = 5);
● WHERE filters individual tuples before grouping; HAVING filters groups after
grouping and aggregation.
Other SQL Constructs: WITH and CASE
● WITH Clause (Common Table Expressions - CTEs): Defines temporary, named
result sets usable within a single query.
○ Syntax:
WITH temporary_table_name (column1, column2, ...) AS (
SELECT ...
FROM ...
WHERE ...
GROUP BY ...
HAVING ...
)
SELECT ...
FROM temporary_table_name, another_table
WHERE ...;
● Assertions are given a name and a CHECK condition (similar to a WHERE clause).
● The DBMS is responsible for enforcing the assertion.
● Often written using EXISTS and NOT EXISTS to check for violating tuples.
● Constraints can also be specified using CHECK on attributes, domains, and tuples
(best for constraints violated only by insertion/update).
Introduction to Triggers in SQL
● Specify actions to be taken automatically when specific events occur and
conditions are met.
● ECA Rule: Event, Condition, Action.
○ Event: Database update operations (INSERT, UPDATE, DELETE).
○ Condition: Optional; evaluated after the event. Action runs only if true.
○ Action: Sequence of SQL statements, transaction, or external program.
● CREATE TRIGGER:
○ Syntax:
CREATE TRIGGER trigger_name
[BEFORE | AFTER] {INSERT | UPDATE [OF column1, column2, ...] | DELETE}
ON table_name
[FOR EACH ROW | FOR EACH STATEMENT] -- FOR EACH ROW is more
common
[WHEN (condition)] -- Optional condition
trigger_body; -- (Can be a single statement or a block using BEGIN...END)
● BEFORE or AFTER specifies when the trigger fires relative to the event.
● FOR EACH ROW fires for each row affected by the event; FOR EACH STATEMENT
fires once per statement.
● OLD and NEW keywords (within FOR EACH ROW triggers) refer to the row's state
before (OLD) and after (NEW) the modification.
○ Example structures from document:
-- After Insert Trigger (MySQL syntax using DELIMITER)
DELIMITER //
CREATE TRIGGER after_insert_salary_difference
AFTER INSERT ON CUSTOMERS
FOR EACH ROW
BEGIN
SET @my_sal_diff = CONCAT ('salary inserted is ', NEW.SALARY);
END;//
DELIMITER ;
-- After Update Trigger (MySQL syntax using DELIMITER)
DELIMITER //
CREATE TRIGGER after_update_salary_difference
AFTER UPDATE ON CUSTOMERS
FOR EACH ROW
BEGIN
DECLARE old_salary DECIMAL (10, 2);
DECLARE new_salary DECIMAL (10, 2);
SET old_salary = OLD.SALARY;
SET new_salary = NEW.SALARY;
SET @my_sal_diff = CONCAT ('salary difference after update is ',
NEW.SALARY - OLD.SALARY);
END;//
DELIMITER ;
● Access Mode: READ ONLY (retrieval) or READ WRITE (retrieval and modification).
Default is READ WRITE (unless READ UNCOMMITTED is set, then READ ONLY is
assumed).
● Diagnostic Area Size: Number of conditions (errors/exceptions) stored.
● Isolation Level: Controls the degree to which a transaction is isolated from
others. Higher levels prevent more concurrency problems but may reduce
performance.
○ READ UNCOMMITTED: Lowest isolation. Allows dirty reads, nonrepeatable
reads, phantoms.
○ READ COMMITTED: Prevents dirty reads. Allows nonrepeatable reads,
phantoms.
○ REPEATABLE READ: Prevents dirty reads and nonrepeatable reads. Allows
phantoms.
○ SERIALIZABLE: Highest isolation. Prevents dirty reads, nonrepeatable reads,
and phantoms. Equivalent to serial execution. (Often the default, but some
systems use READ COMMITTED).
● Concurrency Problems Allowed at Lower Isolation Levels:
○ Dirty Read: Reading data written by an uncommitted transaction.
○ Nonrepeatable Read: Reading the same data item multiple times within a
transaction and getting different values.
○ Phantoms: A transaction reads a set of rows, and another transaction inserts
new rows that match the read condition, which the first transaction might
then see (or not see consistently). Solution: Table locks or predicate locking.