Database Management System Lab
Database Management System Lab
CHAPTER 1: SQL: Advances Queries: More complex SQL retrieval queries, Specifying
constraints as assertions and action triggers, Views in SQL,
CHAPTER 1
In general, each individual NULL value is considered to be different from every other
NULL value in the various database records.
When a record with NULL in one of its attributes is involved in a comparison operation,
the result is considered to beUNKNOWN (it may be TRUE or it may be FALSE).
Hence, SQL uses a three-valued logic with values TRUE, FALSE, and UNKNOWN
instead of the standard two-valued (Boolean) logic with values TRUE or FALSE.
It is therefore necessary to define the results (or truth values) of three-valued logical
expressions when the logical connectives AND, OR, and NOT are used.
FALSE UNKNOWN
(a) AND TRUE
UNKNOWN
TRUE FALSE
TRUE FALSE
FALSE FALSE
FALSE UNKNOWN
UNKNOWN UNKNOWN FALSE
UNKNOWN
TRUE FALSE
(b) OR TRUE
TRUE TRUE
TRUE UNKNOWN
TRUE FALSE
FALSE UNKNOWN
UNKNOWN
UNKNOWN TRUE
(c) NOT
TRUE FALSE
FALSE TRUE
UNKNOWN UNKNOWN
COMPARISONS
NESTED QUERIES, TUPLES AND SET/MULTISET
then used in
Some queries require that existing values in the database be fetched and
by using
a comparison condition. Such queries can be conveniently formulated
another SQL
nested queries, which are complete select-from-where blocks within
query. That other query is called the outer query.
These nested queries can also appear in the WHERE clause or the FROM clause or the
SELECT clause or other SQL clauses as needed.
The comparison operator IN, which compares a value v with a set (or multiset) of
values Vand evaluates to TRUE if v is one of the elements in V.
If anested query returns a single attribute anda single tuple, the query result will be
a single (scalar) value.
In such cases, it is permissible to use =instead of IN for the comparison operator.
In general, the nested query will return a table (relation), which is a set or multiset
of tuples.
Example 1: selects the project numbers of projects that have an employee with last
name 'Smith' involved as manager or as a employee
" SQL allows the use of tuples of values in comparisons by placing them within
parentheses.
" Example 2: 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.
" Examnple 4:Retrieve the name of each employee who has a dependent with the same
first name and is the same sex as the employee.
SELECTE.Fname, E.Lname
FROM EMPLOYEE AS E
WHERE E.Ssn IN ( SELECT D.Essn
FROM DEPENDENT AS D
WHERE E.Fname = D.Dependent_name AND E.Sex = D.Sex );
CORRELATED NESTED QUERIES
Whenever a condition in the WHERE clause of a nested
of a relation declared in the outer query, query references some attribute
the two queries are said to be
" Example 4: Retrieve the name of correlated.
each employee who has a dependent with the
same first name and is the same sex as the employee.
ANS1: SELECT E.Fname, E.Lname
FROM EMPLOYEE AS E
WHERE E.Ssn IN (SELECT D.Essn
FROM DEPENDENT AS D
WHERE E.Fname=D.Dependent_name AND
E.Sex = D.Sex ):
ANS 2: SELECT E.Fname,
E.Lname
FROM EMPLOYEE AS E,
WHERE E.Ssn = D.Essn AND E.Sex =
DEPENDENT
AS D
D.Sex
AND E.Fname D.Dependent_name;
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.
a. UPDATE WORKS_ON
SET Pno = ( SELECT Pnumber
FROM PROJECT
WHERE Pname = 'ProductY)
WHERE Essn IN ( SELECT Ssn
FROM EMPLOYEE
WHERE Lname = 'Smith' AND Fname ="John')
AND
Pno = (SELECT Pnumber
FROM PROJECT
WHERE Pname ='ProductX' );
b. UPDATE PROJECT
SET Pname = 'ProductY"
WHERE Pname ="ProductX';
observations:
a. Aview with a single defining table is updatable if the view attributes contain t
primary key of the base relation, as well as all attributes with the NOT Nä
constraint that do not have default values specified.
b. Views defined on multiple tables using joins are generally not updatable.
c. Views defined using grouping and aggregate functions are not updatable.