Nothing Special   »   [go: up one dir, main page]

K L University Department of Computer Science & Engineering II/IV B.Tech Semester II Database Management Systems (13CS204) TEST-2 Key

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

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)”

Answer: (Computer − σcategory=“desktop”(Computer))) ∩ πmaker(categorσy=“laptop”(Computer)

(1a.2) “Find the makers who make all models with speed faster than 3.2” Answer:

πmaker, model (Computer) / (ρ model←num πnum(σspeed>3.2(Model))

(1a.3) “Find all laptop models and their makers

Answer: πmaker, model (σcategorσy=“laptop”(Computer))

1b)

i ){r.ssn|rϵregistered ᴧ ⱻcϵcourse(c.code=r.codeᴧc.code=’Database Systems’ v c.code=’Analysis of


Algorithms’)}

ii) ){r.ssn|rϵregistered ᴧ ⱻcϵcourse(c.code=r.codeᴧ c.code=’Database Systems’ᴧ c.code=’Analysis of


Algorithms’)}

iii){c.title|cϵcourse ᴧexists in(ⱻRϵ registered)}

1C)

i){<M>|<M,N,O>ϵ computer ᴧN=’laptops’}

ii)No direct DRC expression.

iii){<Z>|<X,Y,Z>ϵ maker ᴧ ⱻP,Q,R,S,T(<P,Q,R,S,T>ϵ computer ᴧ Q=3.2 ᴧ ⱻ M,N,O(<M,N,O>ϵ model ᴧ P=N


ᴧ X=M))}

1D)

i) {c.code|cϵcourse ᴧ ⱻRϵregistered(r.code=c.code)}

ii){c.title|cϵcourse ᴧ not exists in(ⱻRϵ registered)}

iii){s.name,c.title|sϵstudent ᴧc ϵcourse ᴧ ⱻrϵregistered(c.code=r.code and s.ssn=r.ssn)}


1e. Explain with example why relational algebra used is used by Data Base Systems to represent query?

 Query languages: Allow manipulation and retrieval of data from a database.

 Relational model supports simple, powerful QLs:

 Strong formal foundation based on logic.

 Allows for much optimization.

 Query Languages != programming languages!

 QLs not expected to be “Turing complete”.

 QLs not intended to be used for complex calculations.

 QLs support easy, efficient access to large data sets.

 Two mathematical Query Languages form the basis for “real” languages (e.g. SQL), and for
implementation:

 Relational Algebra: More operational(procedural), very useful for representing


execution plans.

 Relational Calculus: Lets users describe what they want, rather than how to compute it.
(Non-operational, declarative.)

 Basic operations:

 Selection ( ) Selects a subset of rows from relation.

 Projection ( ) Deletes unwanted columns from relation.

 Cross-product ( ) Allows us to combine two relations.

 Set-difference ( ) Tuples in reln. 1, but not in reln. 2.

 Union ( ) Tuples in reln. 1 and in reln. 2.

 Additional operations:

 Intersection, join, division, renaming: Not essential, but (very!) useful.

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?

1) f) Consider the following schema


Employees(empid: number ,empname: string ,deptid: number, managerid: number,
job_id: number, salary: real, hiredate: date)
Departments(deptid:number, deptname:string, locationid: number)
Jobs(jobid: number, jobtitle: string, minsalary: real, maxsalary: real)
Write SQL queries for the following
i) Write SQL queries to define tables for the given schemas with constraints.
Ans:) create table Employees(empid int ,empname: varchar ,deptid: int ,
managerid: int, job_id: int, salary: int, hiredate: date)
ii)Write SQL Queries to insert data in to employee, department, jobs.
Ans:) insert into employees values(10,’john’,20,15,33,10000,’12-02-2016’) etc.
iii) find the departments having maximum salary greater than 10000.
Ans:) select deptid from employees group by deptid having max(salary)>10000;
iv) Compute the average salary of each job type of each department.
Ans:) select job_id, avg(sal) from employees group by dept_id, job_id;
v) List all the employees names and the and experience in that company in weeks .
Ans:) select empname,datediff(systdate,hiredate)/7 from employees
2) a) Examine various SQL Aggregate functions with examples
Aggregate functions:
a. SUM()
Select sum(comm) from emp_master;
b. AVG()
select avg(comm) from emp_master;
c. MAX()
select max(salary) from emp_master;
d. MIN()
Select min(salary) from emp_master;
e. COUNT()
SELECT COUNT (*) FROM Emp_Master;

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’) ;

2) d) Consider the following Schema


Flights(flno: integer, from: string, to: string, distance: integer,
departs: time, arrives: time, price: real)
Aircraft(aid: integer, aname: string, cruisingrange: integer)
Certified(eid: integer, aid: integer)
Employees(eid: integer, ename: string, salary: integer)
Write SQL Queries for the following use nested queries and aggregation function where
ever necessary..
i) Find the names of aircraft such that all pilots certified to operate them have salaries more than
$80,000.
Ans: select aname from aircraft a,certified c where a.aid=c.aid and a.eid in (select
employees.eid from employees where salary>80000);

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 //

Create procedure ifwhile(count int)


Begin
Declare I int;
Set i=0;
While i< count do
If (i%2=1) then
Select “even”;
Else
Select “odd”;
End if;
Set i=i+1;
End while;
End
//
Delimiter ;
Call ifwhile(10);

(b) Explain flow control statements used in mysql stored procedures.

 IF Syntax

IF search_condition THEN statement_list


[ELSEIF search_condition THEN
statement_list] ...
[ELSE statement_list]
END IF

WHILE Syntax

[begin_label:] WHILE search_condition DO


statement_list
END WHILE [end_label]

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

CREATE TRIGGER ins_transaction BEFORE INSERT ON account


FOR EACH ROW PRECEDES ins_sum
SET
@deposits = @deposits + IF(NEW.amount>0,NEW.amount,0),
@withdrawals = @withdrawals + IF(NEW.amount<0,-NEW.amount,0);

3(e) Examine the benefits of PL/SQL and explain the block structure of PL/SQL program.

1) Integration of procedural constructs with SQL


SQL is a nonprocedural language. PL/SQL integrates control statements and conditional
statements with SQL.
2) Modularized program development
The basic unit in a PL/SQL program is a block. All PL/SQL programs consist of blocks. These
blocks can be thought of as modules and can be “modularized” in a sequence or nested in other
blocks.
3) Improved performance
PL/SQL allows you to logically combine multiple SQL statements as one unit or block. The
application can send the entire block to the database instead of sending the SQL statements one
at a time. This significantly reduces the number of database calls
4) Portability
PL/SQL programs can run anywhere a DBMS server runs, irrespective of the operating system
and the platform.
5) Exception Handling

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:

1) System-defined exceptions 2) User-defined exceptions

Syntax for Exception Handling

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;

You might also like