K L University Department of Computer Science & Engineering II/IV B.Tech Semester II Database Management Systems (13CS204) TEST-2 Key
K L University Department of Computer Science & Engineering II/IV B.Tech Semester II Database Management Systems (13CS204) TEST-2 Key
K L University Department of Computer Science & Engineering II/IV B.Tech Semester II Database Management Systems (13CS204) TEST-2 Key
(1a-1) “Find the makers who don’t make any desktop, and do make some laptop(s)”
(1a.2) “Find the makers who make all models with speed faster than 3.2” Answer:
1b)
1C)
1D)
i) {c.code|cϵcourse ᴧ ⱻRϵregistered(r.code=c.code)}
Two mathematical Query Languages form the basis for “real” languages (e.g. SQL), and for
implementation:
Relational Calculus: Lets users describe what they want, rather than how to compute it.
(Non-operational, declarative.)
Basic operations:
Additional operations:
example:
sid sname rating age
28 yuppy 9 35.0
58 rusty 10 35.0
sname,rating( rating 8(S 2))
Select sname, rating whose rating >8?
2) b) Write the following Queries using SQL Aggregate functions by assuming typical
employee and department schema.
i) Find the sum of the salaries of all employees, the maximum salary, the minimum salary,
and the average salary.
Ans:) Select sum(comm) from emp_master;
select avg(comm) from emp_master;
Select min(salary) from emp_master;
Select min(salary) from emp_master;
ii) Find the sum of the salaries of all employees of the ‘Research’ department, as well as the
maximum salary, the minimum salary, and the average salary in this department.
Ans:) select sum(sal),max(sal), min(sal), avg(sal) from employee group by emp.dno having
emp.dno=(select dno from dept where dname=’Research’) ;
iii) Retrieve the total number of employees in the ‘Research’ department
Ans: select count(*) from employee group by emp.dno having emp.dno=(select dno from
dept where dname=’Research’) ;
ii) Find the names of pilots whose salary is less than the price of the cheapest route from Los
Angeles to Honolulu.
Ans:Select ename from employee where salary< (select min (price) from flights where
from=’Los angles’ and to=’Honolulu’);
iii) For all aircraft with cruising range over 1000 miles, find the name of the aircraft and the
average salary of all pilots certified for this aircraft.
Ans:Select avg(sal) from employee where eid in (select eid from certified where aid in
(select aid from aircraft where cruise_range=1000));
2) e) Suppliers( sid: integer, sname: string, address: string)
Parts(pid: integer, pname: string, color: string)
Catalog( sid: integer, pid: integer, cost: real)
The Catalog relation lists the prices charged for parts by Suppliers. Write the following
queries in SQL:
1. For each part, find the sname of the supplier who charges the most for that part.
Ans: select pid,sid ,max(cost) from catalog group by pid,sid
2. Find the sids of suppliers who supply only red parts.
Ans:select sid from parts,catalog where catalog.pid=parts.pid and parts.color=’red’;
3. Find the sids of suppliers who supply a red part and a green part
Ans: select sid from parts,catalog where catalog.pid=parts.pid and parts.color=’red’
INTERSECTION
Select sid from parts,catalog where catalog.pid=parts.pid and parts.color=green’;
3 (a) Construct a stored procedure to demonstrate the use of if and while statement in mysql
Delimiter //
IF Syntax
WHILE Syntax
LOOP Syntax
[begin_label:] LOOP
statement_list
END LOOP [end_label]
3 (c) Construct a procedure demonstrating the use of cursor to build a email list of all employees
table.
DELIMITER //
CREATE PROCEDURE generate_email_list (INOUT elist varchar(4000))
BEGIN
DECLARE v_finished INTEGER DEFAULT 0;
DECLARE v_email varchar(100) DEFAULT "";
DEClARE email_cursor CURSOR FOR
SELECT email FROM employees;
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET v_finished = 1;
OPEN email_cursor;
get_email: LOOP
FETCH email_cursor INTO v_email;
IF v_finished = 1 THEN
LEAVE get_email;
END IF;
SET email_list = CONCAT(“elist”,”;”,v_email);
END LOOP get_email;
CLOSE email_cursor;
END
//
DELIMITER ;
3(d) Explain with example the possibility to define multiple triggers for a given table that have
the same trigger event and action time in mysql.
it is possible to define multiple triggers for a given table that have the same trigger event and
action time.
To affect trigger order, specify a clause after FOR EACH ROW that indicates FOLLOWS or
PRECEDES and the name of an existing trigger that also has the same trigger event and action
time.
With FOLLOWS, the new trigger activates after the existing trigger. With PRECEDES, the new
trigger activates before the existing trigger.
Consider two triggers ins_sum and ins_transaction both has event and action time as before
insert on
3(e) Examine the benefits of PL/SQL and explain the block structure of PL/SQL program.
PL/SQL Block:
[DECLARE]
Declaration statements;
BEGIN
Execution statements;
[EXCEPTION]
Exception handling statements;
END;
3(f) Define PL/SQL Exception. Illustrate different types of exception handlers in PL/SQL.
PL/SQL supports programmers to catch such conditions using EXCEPTION block in the program
and an appropriate action is taken against the error condition. There are two types of exceptions:
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling goes here >
WHEN exception1 THEN
exception1-handling-statements
WHEN exception2 THEN
exception2-handling-statements
WHEN exception3 THEN
exception3-handling-statements
........
WHEN others THEN
exception3-handling-statements
END;