Module-4.1 DBMS
Module-4.1 DBMS
Module - 4
BCS403-Database Management System
Advances Quieries
Because of the generality and expressive power of the language, there are many additional
features that allow users to specify more complex retrievals from the database.
THARANI R Page 1
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
THARANI R Page 2
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
In the outer query, we use the OR logical connective to retrieve a PROJECT tuple if
the PNUMBER value of that tuple is in the result of either nested query.
This query will select the Essns of all employees who work the same (project, hours)
combination on some project that employee ‘John Smith’ (whose Ssn =
‘123456789’) works on.
The keyword ALL can also be combined with each of these operators.
An example is the following query, which returns the names of employees whose salary
is greater than the salary of all the employees in department 5:
THARANI R Page 3
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
THARANI R Page 4
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
We illustrate the use of EXISTS—and NOT EXISTS—with some examples.
THARANI R Page 5
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
THARANI R Page 6
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
It corresponds to the way we will write this query in tuple relation calculus.
There is another SQL function, UNIQUE(Q), which returns TRUE if there are
no duplicate tuples in the result of query Q; otherwise, it returns FALSE.
This can be used to test whether the result of a nested query is a set (no duplicates) or
a multiset (duplicates exist).
THARANI R Page 7
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
In SQL, it is possible to rename any attribute that appears in the result of a query
by adding the qualifier AS followed by the desired new name.
Hence, the AS construct can be used to alias both attribute and relation names in
general, and it can be used in appropriate parts of a query.
For example, Q8A shows how query Q8 from Section 4.3.2 can be slightly changed
to retrieve the last name of each employee and his or her supervisor while renaming
the resulting attribute names as Employee_name and Supervisor_name.
The new names will appear as column headers for the query result.
If the user requires that all employees be included, a different type of join called
OUTER JOIN must be used explicitly.
There are several variations of OUTER JOIN, as we shall see.
THARANI R Page 8
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
In the SQL standard, this is handled by explicitly specifying the keyword OUTER
JOIN in a joined table, as illustrated in Q8B:
In SQL, the options available for specifying joined tables include INNER JOIN
(only pairs of tuples that match the join condition are retrieved, same as JOIN).
LEFT OUTER JOIN (every tuple in the left table must appear in the result; if it does not
have a matching tuple, it is padded with NULL values for the attributes of the right
table).
RIGHT OUTER JOIN (every tuple in the right table must appear in the result; if it
does not have a matching tuple, it is padded with NULL values for the attributes of the
left table), and FULL OUTER JOIN.
THARANI R Page 9
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
average (mean) of those values.
THARANI R Page 10
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
These functions can be used in the SELECT clause or in a HAVING clause (which
we introduce later).
The functions MAX and MIN can also be used with attributes that have
nonnumeric domains if the domain values have a total ordering among one another.
We illustrate the use of these functions with several queries.
THARANI R Page 11
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
THARANI R Page 12
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
Sometimes we want to retrieve the values of these functions only for groups
that satisfy certain conditions.
For example, suppose that we want to modify Query 25 so that only projects with
more than two employees appear in the result.
SQL provides a HAVING clause, which can appear in conjunction with a GROUP
BY clause, for this purpose.
THARANI R Page 13
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
THARANI R Page 14
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
For example, suppose that we want to count the total number of employees whose
salaries exceed Rs.40,000 in each department, but only for departments where more than
five employees work.
Here, the condition (SALARY > 40000) applies only to the COUNT function in
the SELECT clause.
Suppose that we write the following incorrect query:
This is incorrect because it will select only departments that have more than
five employees who each earn more than $.40,000.
The rule is that the WHERE clause is executed first, to select individual tuples or
joined tuples; the HAVING clause is applied later, to select individual groups of tuples.
In the incorrect query, the tuples are already restricted to employees who earn more than
$.40,000 before the function in the HAVING clause is applied.
One way to write this query correctly is to use a nested query, as shown in Query 28.
THARANI R Page 15
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
THARANI R Page 16
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
The view is initially empty. It is first loaded with the first level (supervisor,
supervisee) Ssn combinations via the first part (SELECT SupervisorSss, Ssn
FROMEMPLOYEE), which is called the base query.
At this point, the result of the recursive query is in the view SUP_EMP.
THARANI R Page 17
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
THARANI R Page 18
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
THARANI R Page 19
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
The event(s):
These are usually database update operations that are explicitly applied to the database.
In this example the events are: inserting a new employee record, changing an
employee’s salary, or changing an employee’s supervisor.
The person who writes the trigger must make sure that all possible events are
accounted for. In some cases, it may be necessary to write
more than one trigger to cover all possible cases.
THARANI R Page 20
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
These events are specified after the keyword BEFORE in our example, which means
that the trigger
should be executed before the triggering operation is executed.
An alternative is to use the keyword AFTER, which specifies that the trigger should be
executed after the operation specified in the event is completed.
We show how views are specified, and then we discuss the problem of updating views and
how views can be implemented by the DBMS.
THARANI R Page 21
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
Then we can issue queries on the view, which are specified as single table
retrievals rather than as retrievals involving two joins on three tables.
We call the EMPLOYEE, WORKS_ON, and PROJECT tables the defining tables of
the view.
In V1, we did not specify any new attribute names for the view WORKS_ON1 (although
we could have); in this case, WORKS_ON1 inherits the names of the view attributes
from the defining tables EMPLOYEE, PROJECT, and WORKS_ON.
THARANI R Page 22
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
View V2 explicitly specifies new attribute names for the view DEPT_INFO, using a one-
to-one correspondence between the attributes specified in the CREATE VIEW clause
and those specified in the SELECT clause of the query that defines the view.
We can now specify SQL queries on a view—or virtual table—in the same way
we specify queries involving base tables.
For example, to retrieve the last name and first name of all employees who work on
the ‘ProductX’ project, we can utilize the WORKS_ON1 view and specify the query as
in QV1:
The disadvantage of this approach is that it is inefficient for views defined via complex
queries that are time-consuming to execute, especially if multiple view queries are
going to be applied to the same view within a short period of time.
This view update is shown in UV1:
THARANI R Page 23
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
This query can be mapped into several updates on the base relations to give the
desired update effect on the view.
In addition, some of these updates will create additional side effects that affect the
result of other queries.
For example, here are two possible updates, (a) and (b), on the base
relations corresponding to the view update operation in UV1:
Update (a) relates ‘John Smith’ to the ‘ProductY’ PROJECT tuple instead of
the ‘ProductX’ PROJECT tuple and is the most likely desired update.
A view with a single defining table is updatable if the view attributes contain the
primary key of the base relation, as well as all attributes with the NOT NULL
constraint that do not have default values specified.
Views defined on multiple tables using joins are generally not updatable.
Views defined using grouping and aggregate functions are not updatable.
THARANI R Page 24
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
In SQL, the clause WITH CHECK OPTION should be added at the end of the view
definition if a view is to be updated by INSERT, DELETE, or UPDATE
statements.
This allows the system to reject operations that violate the SQL rules for view updates.
The full set of SQL rules for when a view may be modified by the user are more
complex than the rules stated earlier.
It is also possible to define a view table in the FROM clause of an SQL query.
This is known as an in-line view. In this case, the view is defined within the query itself.
Views as Authorization Mechanisms
We describe SQL query authorization statements (GRANT and REVOKE) when
we present database security and authorization mechanisms.
Here, we will just give a couple of simple examples to illustrate how views can be
used to hide certain attributes or tuples from unauthorized users.
Suppose a certain user is only allowed to see employee information for employees who
work for department 5; then we can create the following view DEPT5EMP and grant
the user the privilege to query the view but not the base table EMPLOYEE itself.
This user
will only be able to retrieve employee information for employee tuples whose
Dno = 5, and will not be able to see other employee tuples when the view is queried.
Module - 4
BCS403-Database Management System
This can be done while the database is operational and does not require
recompilation of the database schema.
Certain checks must be done by the DBMS to ensure that the changes do not
affect the rest of the database and make it inconsistent.
If the RESTRICT option is chosen in place of CASCADE, the schema is dropped only
if it has no elements in it;
otherwise, the DROP command will not be executed. To use the RESTRICT option, the
user must first individually drop each element in the schema, then drop the schema
itself.
If a base relation within a schema is no longer needed, the relation and its definition
can be deleted by using the DROP TABLE command.
For example, if we no longer wish to keep track of dependents of employees in the
COMPANY database of Figure 6.1, we can get rid of the DEPENDENT relation
by issuing the following command:
The DROP command can also be used to drop other types of named schema
elements, such as constraints or domains.
THARANI R Page 26
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
For example, to add an attribute for keeping track of jobs of employees to the
EMPLOYEE base relation in the COMPANY schema (see Figure 6.1), we can use
the command
THARANI R Page 27
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
THARANI R Page 28
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
THARANI R Page 29
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
THARANI R Page 30
Asst. Professor, Dept. of AI & ML
SCHEME - 2022
Module - 4
BCS403-Database Management System
THARANI R Page 31
Asst. Professor, Dept. of AI & ML