Assignment No. 2 Database Management System
Assignment No. 2 Database Management System
2
Database Management System
Roll No. 20
SECTION: A1811
PART – A
(a) Find the name of all employees who have borrowed a book published by McGraw-Hill.
(b) Find the name of all employees who have borrowed all book published by McGraw-Hill.
(c) Find the names of employees who have borrowed more than five different books published
by McGraw-Hill.
(d) For each publisher, find the name of employees who have borrowed more than five books of
that publisher
ANS:
a. select name from employee e, books b, loan l where e.empno = l.empno and l.isbn = b.isbn and
b.publisher = ‘McGrawHill’
b. select name from employee e join loan l on e.empno=l.empno join (select isbn from books where
publisher = 'McGrawHill') x on l.isbn=x.isbn group by e.empno,name having count(*)= (select
count(*) from books where publisher=’McGrawHill’)
c. select name from employee,loan where employee.empno=loan.empno and isbn in ( select distinct
isbn from books where publisher='McGraw-Hill') group by employee.empno,name having
count(isbn) >=5
The Project Operation: The project operation is a unary operation that returns its argument relation,
with certain attributes left out. Since a relation is a set, any duplicate rows are eliminated.
Projection is denoted by the uppercase Greek letter pi (Π)
The Set Difference Operation: The set-difference operation, denoted by −, allows us to find tuples
that are in one relation but are not in another. The expression r − s produces a relation containing
those tuples in r but not in s.
The Cartesian-Product Operation: The Cartesian-product operation, denoted by a cross (×), allows
us to combine information from any two relations. We write the Cartesian product of relations r1 and
r2 as r1 × r2.
The Rename Operation: Unlike relations in the database, the results of relational-algebra
expressions do not have a name that we can use to refer to them. It is useful to be able to give them
names; the rename operator, denoted by the lowercase Greek letter rho (ρ)
The Set-Intersection Operation: The first additional-relational algebra operation that we shall
define is set intersection (∩). Suppose that we wish to find all customers who have both a loan and an
account.
The Natural-Join Operation: It is often desirable to simplify certain queries that require a Cartesian
product. Usually, a query that involves a Cartesian product includes a selection operation on the result
of the Cartesian product.
The Division Operation: The division operation, denoted by ÷, is suited to queries that include the
phrase “for all.”
PART – B
4. How join can be expressed in basic relational algebra operators? Justify with example
ANS:
JOIN Operator
In its simplest form the JOIN operator is just the cross product of the two relations.
As the join becomes more complex, tuples are removed within the cross product to make the result
of the join more meaningful.
JOIN allows you to evaluate a join condition between the attributes of the relations on which the
join is undertaken.
JOIN Example
Natural Join
Invariably the JOIN involves an equality test, and thus is often described as an equi-join. Such joins
result in two attributes in the resulting relation having exactly the same value. A `natural join' will
remove the duplicate attribute(s).
In most systems a natural join will require that the attributes have the same name to identify the
attribute(s) to be used in the join. This may require a renaming mechanism.
If you do use natural joins make sure that the relations do not have two attributes with the same
name by accident.
OUTER JOINs
Notice that much of the data is lost when applying a join to two relations. In some cases this lost data
might hold useful information. An outer join retains the information that would have been lost from
the tables, replacing missing data with nulls.
There are three forms of the outer join, depending on which data is to be kept.
Examples:
PEOPLE: MENU:
Name Age Food Food Day
Alice 21 Hamburger Pizza Monday
Bill 24 Pizza Hamburger Tuesday
Carl 23 Beer Chicken Wednesday
Dina 19 Shrimp Pasta Thursday
Tacos Friday
5. Write a PL/SQL code for generating the electricity bills of customers. Make use of functions
and cursors. Apply triggers also to automatically update the database to modify customer’s
total bill.
ANS:
Algorithm:
11 end if
13 stop
Input table
Program:
declare
cid number;
p1 number;
p2 number;
u number;
chrge varchar(5);
begin
cid := &customerno;
u:=p1-p2;
if u<20 then
chrge:='NIL';
chrge:=u*50/100;
chrge:= u*75/100;
chrge:= u*150/100;
else
chrge:=u* 225/100;
endif;
dbms_output.put_line('ELECTRICITY BILL');
dbms_output.put_line('customer No:'||cid);
dbms_output.put_line('Present Reading:'||p1);
dbms_output.put_line('Past Reading:'||p2);
dbms_output.put_line('Units Consumed:'||u);
dbms_output.put_line('Charge:'||chrge);
end;
Output:
ELECTRICITY BILL
Charge: 1305
ANS:
a. The SQL COUNT function returns the number of rows in a table satisfying the criteria specified
in the WHERE clause.
b. The SQL SUM function is used to select the sum of values from numeric column.
c. The SQL AVG function retrieves the average value for a numeric column.
d. The SQL MIN function selects the smallest number from a numeric column.
e. The SQL MAX function retrieves the maximum numeric value from a numeric column.
Table Name: A
Name Salary
Emil 5000
Chang 5000
Emily 4500
Nick 4000
AVG : Select AVG(Salary) FROM A
Value = 4675