DBMS Unit 3
DBMS Unit 3
Unit 3:-
11Q)
Triggers are useful for enforcing business rules, maintaining data integrity,
and automating tasks within a database.
id : Book ID
DBMS Unit 3 1
Trigger Example:
1. Insert Trigger:
Goal: When a new book is added, we want it to be automatically marked as
available.
2. Update Trigger:
Goal: We don't want to allow changing the author of a borrowed book.
3. Delete Trigger:
Goal: We want to log when a book is deleted from the database.
DBMS Unit 3 2
BEGIN
INSERT INTO book_deletion_log (book_id) VALUES (OLD.i
d);
END;
b)
Row-Level Triggers:
1. What They Do:
Row-level triggers execute once for each row affected by the triggering
event.
They can access and modify the data of the specific row that caused
the trigger to fire.
Fired once for every row affected by the triggering event. For example,
if an UPDATE statement affects 5 rows, a row-level trigger associated
with that UPDATE statement will be fired 5 times, once for each row.
3. Use Cases:
Often used for tasks like data validation, auditing changes to specific
rows, or enforcing row-level security policies.
4. Example:
Statement-Level Triggers:
1. What They Do:
DBMS Unit 3 3
Statement-level triggers execute once for each triggering event,
regardless of the number of rows affected by the event.
They do not have direct access to individual row data but can still
perform actions based on the overall outcome of the triggering event.
Fired once for each triggering SQL statement, regardless of the number
of rows affected by that statement.
3. Use Cases:
Useful for tasks that need to be performed once for a batch of rows or
for the overall outcome of an operation.
Often used for tasks like logging high-level actions, enforcing global
constraints, or performing calculations across multiple rows.
4. Example:
Summary:
Row-Level Triggers operate on each row affected by the triggering event,
allowing for row-specific actions.
12Q)
DBMS Unit 3 4
Aggregate operators are functions used in databases and data analysis to
perform calculations on groups of rows and return a single result.
They are commonly used with the SQL language for querying databases
Examples:
Example:
Example:
Example:
•
Min(salary): Minimum value in the salary column except NULL i.e., 40
Example:
•
Max(salary): Maximum value in the salary i.e., 80.
Understanding with an example:
Id Name Salary
1 A 802
2 B 403
DBMS Unit 3 5
3 C 604
4 D 705
5 E 606
6 F NULL
Output:
TotalEmployees
6
TotalSalary
3120
AverageSalary
624
HighestSalary
802
LowestSalary
403
b)
UNION Operator:
DBMS Unit 3 6
The UNION operator could be used to find the result set or combination of two
or more tables.
Each table used within UNION must have the same number of columns.
Syntax:
Example:
Emp1 Table
Emp2 Table
SELECT Country
FROM Emp1
DBMS Unit 3 7
UNION
SELECT Country
FROM Emp2
ORDER BY Country;
Output:
INTERSECT Operator:
The SQL Server INTERSECT operator is a powerful tool that allows database
developers and analysts to retrieve the common elements from two or more
result sets.
Syntax:
Example:
Table 1
DBMS Unit 3 8
Table 2
Output:
DBMS Unit 3 9
The output gives common data of column CustomerId in both table.
EXCEPT Operator:
EXCEPT keyword is an operator that is used to retrieve distinct rows from the
left query result set that are not present in the right query result set in a given
query. It can be useful in finding the difference between two sets of data.
The syntax for the EXCEPT operator is given as follows
Syntax:
Example Tables:
CustomerID CustomerName
1 Alice
2 Bob
3 Charlie
PreferredCustomerID CustomerName
1 Alice
2 Bob
DBMS Unit 3 10
Now the task is two find the customers who are not Preferred Customers. To
be more specific we need customers in the first table except the customers in
the second table. It can be done using the following query.
Output:
DBMS Unit 3 11