0% found this document useful (0 votes)
14 views5 pages

Database Management System Lab

The document covers advanced SQL queries, including complex retrieval queries, transaction processing, and the handling of NULL values using three-valued logic. It explains how to use nested queries, tuples, and various comparison operators in SQL, as well as the characteristics of correlated nested queries. Additionally, it discusses the updatability of views based on their defining tables and constraints.

Uploaded by

doctorcyning
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Database Management System Lab

The document covers advanced SQL queries, including complex retrieval queries, transaction processing, and the handling of NULL values using three-valued logic. It explains how to use nested queries, tuples, and various comparison operators in SQL, as well as the characteristics of correlated nested queries. Additionally, it discusses the updatability of views based on their defining tables and constraints.

Uploaded by

doctorcyning
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Module4

CHAPTER 1: SQL: Advances Queries: More complex SQL retrieval queries, Specifying
constraints as assertions and action triggers, Views in SQL,

CHAPTER 2: Transaction Processing: Introduction to Transaction Processing, Transaction and


System concepts, Desirable properties of Transactions, Characterizing schedules based on
recoverability, Characterizing schedules based on Serializability, Transaction support in SQL.

CHAPTER 1

COMPARISONS INVOLVING NULL AND THREE-VALUED LOGIC

Consider the following examples to illustrate each of the meanings of NULL.


1. Unknown value. A person's date of birth is not known, so it is represented by NULL
in the database. An example of the other case of unknown would be NULL for a
person's home phone because it is not known whether or not the person has a home
phone.
2. Unavailable or withheld value. A person has a home phone but does not want it to
be listed, so it is withheld and represented as NULL in the database.
3. Not applicable attribute. An attribute LastCollegeDegree would be NULL for
aperson who has no college degrees because it does not apply to that person.

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

attribute value is NULL.


SQL allows queries that check whether an
attribute value to NULL, SQL uses the
Rather than using= or <> to compare an
comparison operators IS or IS NOT.
value as being distinct from every other
This is because SQL considers each NULL
follows that when a join
NULL value, so equality comparison is not appropriate. It
attributes are not included
condition is specified, tuples with NULL values for the join
in the result.
supervisors.
Example: Retrieve the names of all employees who do not have
SELECT Fname, Lname
FROM EMPLOYEE
WHERE Super_ssn IS NULL;

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

SELECT DISTINCT Pnumber


FROM PROJECT
WHERE Pnumber IN ( SELECT Pnumber
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE Dnum = Dnumber AND Mgr_ssn = Ssn AND Lname = "Smith')
OR
Pnumber IN (SELECT Pno
FROM WORKS_ON, EMPLOYEE
WHERE Essn = Ssn AND Lname = 'Smith' );

" 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.

SELECT DISTINCT Essn


FROM WORKS_ON
WHERE (Pno, Hours) IN ( SELECT Pno, Hours
FROM WORKS_ON
WHERE Essn =123456789'):

In addition to the IN operator, a number of other comparison operators can be used


to compare a single value v (typically an attribute name) a set or multiset v
(typically a nested query).
The = ANY (or = SOME) operator returns TRUE if the value v is equal to some value in
the set Vand is hence equivalent to IN.The two keywords ANY and SOME have the
same effect.
Other operators that can be combined with ANY (or SOME) include >, >=, <,<=, and
<>. The keyword ALL can also be combined with each of these operators.
For example, the comparison condition (v > ALL V) returns TRUE if the value v is
greater than all the values in the set (or multiset) V.
Example 3: Find names of employees whose salary is greater than the salary of all
the employees in department 5:
SELECT Lname, Fname
FROM EMPLOYEE
WHERE Salary >ALL (SELECT Salary
FROM EMPLOYEE
WHERE Dno = 5);

" 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.

You might also like