DBMS Question Bank
DBMS Question Bank
Sl.no Questions
1 What is a trigger?What are its advantages?Give the general syntax for creating a
TRIGGER.
To monitor a database and take a corrective action when a condition occurs
Examples:
Charge $10 overdraft fee if the balance of an account after a withdrawal transaction is
less than
$500
Limit the salary increase of an employee to no more than 5% raise
CREATE TRIGGER trigger-name
trigger-time trigger-event
ON table-name
FOR EACH ROW
trigger-action;
– trigger-time {BEFORE, AFTER}
– trigger-event {INSERT,DELETE,UPDATE}
2 What are Stored Procedures? Illustrate how to create and drop a procedure with
suitable
example.
A stored procedure contains a sequence of SQL commands stored in the database
catalog so that
it can be invoked later by a program
Stored procedures are declared using the following syntax:
Create Procedure <proc-name>
(param_spec 1 , param_spec 2 , …, param_spec n )
begin
-- execution code
end;
where each param_spec is of the form:
[in | out | inout] <param_name> <param_type>
– in mode: allows you to pass values into the procedure,
– out mode: allows you to pass value back from procedure to the calling program
3 Define virtual tables.How to create and drop a view. Explain with an example.
A view is a “virtual” table that is derived from other tables
Advantages:
i)Allows for limited update operations
ii)Since the table may not physically be stored
iii)Allows full query operations
CREATE VIEW MANAGER AS
SELECT FNAME, LNAME, DName, Dnumber, SALARY
FROM EMPLOYEE, DEPARTMENT
WHERE SSN=MGRSSN AND DNO=DNUMBER;
DROP VIEW MANAGER
4 Define functions in SQL. Illustrate how to create a function in sql with suitable example.
Functions in SQL are used to perform some caluculations.
They are declared using the following syntax:
function <function-name> (param_spec1, …, param_speck)
returns <return_type>
[not] deterministic allow optimization if same output
for the same input (use RAND not deterministic )
Begin
-- execution code
end;
where param_spec is:
[in | out | in out] <param_name> <param_type>
5 Specify the following queries on the COMPANY relational database schema given
below using the relational algebra operators.
EMPLOYEE (Fname ,Minit ,Lname, Ssn, Bdate, Address, Gender, Salary, Super_ssn,
Dno)
DEPARTMENT (Dname, Dnumber, MGRSSN, MGRSTART Date)
PROJECT (Pname, Pnumber, Plocation, Dnum)
WORKS_ON (Essn, Pno, Hours)
DEPENDENT (Essn, Dependent_name ,Gender ,Bdate ,Relationship)
Retrieve the name and address of all employees who work for the ‘Research’
department.
a) For every project located in ‘Stafford’, list the project number, the controlling
department number, and the department manager’s last name, address, and birth
date.
STAFFORD_PROJS ← Plocation=‘Stafford’(PROJECT)
CONTR_DEPTS ← (STAFFORD_PROJS Dnum=DnumberDEPARTMENT)
PROJ_DEPT_MGRS ← (CONTR_DEPTS Mgr_ssn=SsnEMPLOYEE)
RESULT ← Pnumber, Dnum, Lname, Address, Bdate(PROJ_DEPT_MGRS)
b) Find the names of employees who work on all the projects controlled by department
number 5.
DEPT5_PROJS ← (Pno)( Pnumber( Dnum=5(PROJECT)))
EMP_PROJ ← (Ssn, Pno)( Essn, Pno(WORKS_ON))
RESULT_EMP_SSNS ← EMP_PROJ ÷ DEPT5_PROJS
RESULT ← Lname, Fname(RESULT_EMP_SSNS * EMPLOYEE)
e) Retrieve the names and address of all employees who works for Research
department.
RESEARCH_DEPT ← Dname=‘Research’(DEPARTMENT)
RESEARCH_EMPS ← (RESEARCH_DEPT Dnumber=DnoEMPLOYEE)
RESULT ← Fname, Lname, Address(RESEARCH_EMPS)
As a single in-line expression, this query becomes:
Fname, Lname, Address ( Dname=‘Research’(DEPARTMENT
Dnumber=Dno(EMPLOYEE))
22 What is a function in sql and how it is different from procedure. Explain with an
example how to create and call a function.
23 Explain the informal design guidelines used as measures to determine the quality of
relation schema design.
24 Discuss insertion, deletion, and modification anomalies. Why are they considered bad?
Illustrate with examples.
25 Specify the following queries in relational algebra on the given database schema
Consider the following relations for a database that keeps track of business trips of
salespersons in a sales office:
Consider the following relations for a database that keeps track of business trips
of salespersons in a sales office:
SALESPERSON(Ssn, Name, Start_year, Dept_no)
TRIP(Ssn, From_city, To_city, Departure_date, Return_date, Trip_id)
EXPENSE(Trip_id, Account#, Amount)
a. Give the details (all attributes of trip relation) for trips that exceeded
$2,000 in expenses.
b. Print the Ssns of salespeople who took trips to Honolulu.
c. Print the total trip expenses incurred by the salesperson with SSN = ‘234-56-7890’
27 Write the following queries in tuple relational calculus for the company schema
Employee (Fname, Lname, SSN, address, dno)
Department (dnumber,dname)
a. Retrieve the birth date and address of the employee (or employees) whose name is
John B. Smith.
b. List the name and address of all employees who work for the ‘Research’ department.
c. To find the first and last names of all employees whose salary is above 50,000
28 Define trigger and explain the syntax of creating and executing trigger with an example
in sql.
29 Define 1 NF. Whether the following table is in 1 NF? If yes give the reason and if not
convert to 1NF.
answer :
Stored Procedure is a function consists of many SQL statement to access
the database system. Several SQL statements are consolidated
into a stored procedure and execute them whenever and wherever required.
Stored procedure can be
used as a modular programming – means create once, store and call for
several times whenever required. This supports faster execution
instead of executing multiple queries. This reduces network traffic
and provides better security to the data.
Disadvantage is that it can be executed only in the Database and utilizes more
memory in the database server.
OPEN cur_emp;
REPEAT
FETCH cur_emp INTO did,sal;
IF did = 10 THEN
new_sal = sal * 1.10;
elsif did = 20 THEN
new_sal = sal * 1.20;
END IF;
UPDATE employees
SET salary = new_sal
WHERE CURRENT OF cur_emp;
UNTIL done
END REPEAT;
CLOSE cur_emp;
END;
37 What is a function? How it differs from procedure.
create a function to return the name of department where particular
employee is working.
answer:
The function must return a value but in Stored Procedure it is optional.
Even a procedure can return zero or n values.
Functions can have only input parameters
for it whereas Procedures can have input or output parameters.
Functions can be called from Procedure whereas Procedures cannot be called from a
Function.
create function xyz(id int) returns varchar(10)
begin
declare name int;
select ENAME into name from employee where EID=id;
return name;
end //
38 What is a Database Trigger. Explain with example.
Answer:
A database trigger is a stored PL/SQL program unit associated with a
specific database table. ORACLE executes (fires) a database trigger automatically
when a given SQL operation (like INSERT, UPDATE or DELETE) affects the table.
Unlike a procedure, or a function, which
must be invoked explicitly, database triggers are invoked implicitly.
43 Write a stored procedure in SQL, to retrieve the information of all the employees who
lives in “Bangalore”.
44 Define a trigger with the syntax. Give a sample trigger creation in SQL.
46 List out informal guidelines to design a good relational schema. Explain each.