Module III DBMS
Module III DBMS
The basic form of an SQL query, specifically when retrieving data, is composed of a
combination of clauses. The most elementary form of an SQL query for data retrieval
can be represented as
Syntax
1. SELECT Clause: This is where you specify the columns you want to retrieve. Use an
asterisk (*) to retrieve all columns.
2. FROM Clause: This specifies from which table or tables you want to retrieve the data.
3. WHERE Clause (optional): This allows you to filter the results based on a condition.
4. DISTINCT Clause (optional): is an optional keyword indicating that the answer should
not contain duplicates. Normally if we write the SQL without DISTINCT operator then it
does not eliminate the duplicates.
UNION in DBMS
The UNION operator in DBMS is used to combine the result sets of two or
more SELECT statements. However, it will only select distinct values. The UNION
operator selects only distinct values by default. If you want to allow duplicate values,
you can use UNION ALL.
Here's the basic syntax:
Syntax
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
For instance, let's assume we have two tables, Customers and Suppliers, and we
want to find all cities where we have either a customer or a supplier. If the Customers
table has a column City and the Suppliers table also has a column City, we can use a
UNION to get a list of all cities:
Example:
This would return a list of cities, with each city listed only once, even if it appears in both
the Customers and Suppliers tables.
Remember, the number and order of the columns, as well as the data types of the
corresponding columns, must be the same in all the SELECT statements that you're
combining with UNION.
If you wanted to include duplicates, you would use UNION ALL:
Example:
In this case, a city would be listed once for every time it appears in either the Customers
or Suppliers table.
INTERSECT in DBMS
he INTERSECT operator in a DBMS is used to combine two SELECT statements and
return only the records that are common to both.
The basic syntax of the INTERSECT clause in SQL is:
Syntax
This would return a list of order_ids that appear in both the Orders and Deliveries
tables.
The number and order of columns, and the data types in both the SELECT statements
should be the same.
It removes duplicate rows from the result set.
It returns records that are common to both the SELECT statement queries.
However, not all DBMSs support the INTERSECT operator. For example, MySQL does
not have a built-in INTERSECT operator, you can achieve the same result using a
combination of INNER JOIN, UNION, or EXISTS.
Here is an example of how to emulate INTERSECT using INNER JOIN:
Syntax
This would also return ids that exist in both table1 and table2.
For example, if you have two tables, Orders and Deliveries, and you want to find all
orders that have been delivered (assuming order_id is a common column), you could
write:
Example:
This would return a list of order_ids that appear in both the Orders and Deliveries
tables.
EXCEPT in DBMS
The EXCEPT operator in a DBMS is used to return the difference between
two SELECT statements. It returns the records from the first SELECT statement that
are not present in the second SELECT statement.
Here is the basic syntax of the EXCEPT clause in SQL:
Syntax
For example, if you have two tables, Orders and Deliveries, and you want to find all
orders that have not been delivered yet (assuming order_id is a common column), you
could write:
Example:
This would return a list of order_ids that appear in the Orders table but not in the
Deliveries table.
The number and order of columns, and the data types in both SELECT statements
should match.
It removes duplicate rows from the result set.
It only returns records from the first SELECT statement that are not in the second
SELECT statement.
Just like the INTERSECT operator, all DBMSs does not support the EXCEPT operator.
MySQL doesn't support the EXCEPT operator directly, you can simulate EXCEPT using
a combination of LEFT JOIN or NOT EXISTS.
Here's how you might do it with LEFT JOIN:
Syntax
This query will return the rows from table1 where there is no matching row in table2 for
the specified column.
For example, if you have two tables, Orders and Deliveries, and you want to find all
orders that have not been delivered yet (assuming order_id is a common column), you
could write:
Example:
This would return a list of OrderIDs that appear in the Orders table but not in the
Deliveries table.
COUNT
Returns the number of rows that matches a specified criterion.
Syntax
COUNT(expression)
Example:
This query would return the total number of rows in the Employees table.
SUM
Returns the total sum of a numeric column.
Syntax
SUM(expression)
Example:
This query would return the total sum of the salary column values in the Employees
table.
AVG
Returns the average value of a numeric column.
Syntax
AVG(expression)
Example:
This query would return the average salary from the Employees table.
MIN
Returns the smallest value of the selected column.
Syntax
MIN(expression)
Example:
This query would return the lowest salary from the Employees table.
MAX
Returns the largest value of the selected column.
Syntax
MAX(expression)
Example:
SELECT MAX(salary) FROM Employees;
This query would return the highest salary from the Employees table.
These aggregation operators are often used with the GROUP BY clause to group the
result-set by one or more columns. For example, to find the highest salary in each
department, you could write:
Example:
This query would return the highest salary for each department in the Employees table.
4. Using TRIGGERS
A trigger is a procedural code in a database that automatically executes in response to
certain events on a particular table or view. Essentially, triggers are special types of
stored procedures that run automatically when an INSERT, UPDATE, or DELETE
operation occurs.
A trigger is a predefined action that the database automatically executes in response to
certain events on a particular table or view. Triggers are typically used to maintain the
integrity of the data, automate data-related tasks, and extend the database
functionalities.
When implementing complex constraints, it's crucial to strike a balance. While they can
ensure data integrity, they can also add overhead to the database system and increase
the complexity of the schema and the operations performed on it. Proper documentation
and understanding of each constraint's purpose are essential.
Triggers and Active data bases in DBMS
Triggers and active databases are closely related concepts in the domain of DBMS.
Let's delve into what each of them means and how they are interconnected.
Triggers
A trigger is a predefined action that the database automatically executes in response to
certain events on a particular table or view. Triggers are typically used to maintain the
integrity of the data, automate data-related tasks, and extend the database
functionalities.
There are various types of triggers based on when they are executed:
BEFORE: Trigger is executed before the triggering event.
AFTER: Trigger is executed after the triggering event.
INSTEAD OF: Trigger is used to override the triggering event, primarily for views.
Example of a Trigger
AuditLog Table
Now, let's create a trigger that automatically inserts a record into the `AuditLog` table
whenever there's an update to the `Salary` column in the `Employees` table.
Trigger
mysql> DELIMITER //
mysql> CREATE TRIGGER AfterSalaryUpdate
AFTER UPDATE ON Employees
FOR EACH ROW
BEGIN
IF OLD.Salary != NEW.Salary THEN
INSERT INTO AuditLog (EmployeeID, OldSalary, NewSalary, ChangeDate)
VALUES (OLD.EmployeeID, OLD.Salary, NEW.Salary, NOW());
END IF;
END;
//
mysql> DELIMITER ;
Active Databases
An active database is a database that uses triggers and other event-driven
functionalities. The term "active" signifies that the DBMS reacts automatically to
changes in data and predefined events. Triggers are a primary mechanism that makes
a database "active."
Integrity Maintenance: Active databases can enforce more complex business rules that
can't be enforced using standard integrity constraints.
Automation: They can automate certain tasks, reducing manual interventions.
Alerts: They can notify users or applications when specific conditions are met.
Triggers are what give an active database its "active" nature. The ability of the database
to react to events automatically is primarily because of triggers that execute in response
to these events.
In essence, while "trigger" refers to the specific procedural code blocks that run in
response to events, "active database" refers to the broader capability of a DBMS to
support and use such event-driven functionalities.