0% found this document useful (0 votes)
230 views16 pages

DBMS Question Bank

This document contains a question bank on database management systems (DBMS) concepts with questions ranging from triggers and stored procedures to normalization and relational algebra queries. Key points covered include: - The definition and syntax of triggers in SQL to monitor and take corrective actions on data changes. - How stored procedures contain reusable SQL code that can be invoked later, and the syntax for creating procedures with parameters. - Virtual tables called views that allow query operations but may not physically store data. - User-defined functions in SQL that perform calculations and are declared with parameters and a return type. - Examples of relational algebra queries on sample schemas involving operations like selection, projection, join, set difference

Uploaded by

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

DBMS Question Bank

This document contains a question bank on database management systems (DBMS) concepts with questions ranging from triggers and stored procedures to normalization and relational algebra queries. Key points covered include: - The definition and syntax of triggers in SQL to monitor and take corrective actions on data changes. - How stored procedures contain reusable SQL code that can be invoked later, and the syntax for creating procedures with parameters. - Virtual tables called views that allow query operations but may not physically store data. - User-defined functions in SQL that perform calculations and are declared with parameters and a return type. - Examples of relational algebra queries on sample schemas involving operations like selection, projection, join, set difference

Uploaded by

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

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)

c) Retrieve the names of employees who have no dependents.


ALL_EMPS ← Ssn(EMPLOYEE)
EMPS_WITH_DEPS(Ssn) ← Essn(DEPENDENT)
EMPS_WITHOUT_DEPS ← (ALL_EMPS – EMPS_WITH_DEPS)
RESULT ← Lname, Fname(EMPS_WITHOUT_DEPS * EMPLOYEE)
d) Retrieve the names of managers who have atleast one dependent.
MGRS(Ssn) ← Mgr_ssn(DEPARTMENT)
EMPS_WITH_DEPS(Ssn) ← Essn(DEPENDENT)
MGRS_WITH_DEPS ← (MGRS ∩ EMPS_WITH_DEPS)
RESULT ← Lname, Fname(MGRS_WITH_DEPS * 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))

6 Define Relational Tuple calculus.Give its general form.


7 Discuss the informal guidelines for the relational database design.
8 Discuss insert, delete and update anomalies
9 Define the following with examples
i)functional dependency
ii)Normalization
iii)Normal form
10 Discuss 1NF and 2NF with examples. Consider the relation
EMP-PRO = {SSn, Pnumber, Hours, Ename, Pname, Plocation}.
Assume {SSn, Pnumber}as primary key. The dependencies are
SSnPnumber ~ {Hours}
SSn ~ {Ename}
Pnumber ~ {Pname, Plocation}.
Normalize the above relation into 2NF.
11 Define the following with suitable examples (8Marks)
a. Key b. Super key
c. Candidate key d. Prime attribute
12 Define stored procedure. Explain the creation and calling of the stored Procedure with
suitable example.
13 Explain the informal design guidelines used as measure to determine the quality of
relation schema design.
14 How are triggers define in SQL? Explain with examples
15 Consider the below database and give the relational algebra notations for the
following queries

a. Find the movies made after 1997


b. Find the actor who has acted in some Coen’s movies

c. Find the movies made by Hanson after 1997


d. Find all movies and their ratings.

16 Briefly explain with appropriate example First Normal form


17 What are views? Explain with syntax and example
18 Explain following Relational algebra operations.
a. Union
b. Intersection
c. Set Difference
d. Cartesian product with example
19 Briefly explain with appropriate example Second Normal form.
20 What is functional dependencies explain with example
21 Define Stored procedure. Explain the creating and calling of stored procedure with
suitable example.

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:

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’
26 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:
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.

30 Define 2 NF. Consider the universal relation R = {A, B, C, D, E, F, G, H, I, J} and the


set
of functional dependencies F = {{A, B}→{C}, {A}→{D, E}, {B}→{F}, {F}→{G, H},
{D}→{I, J}}.
What is the key for R? Decompose R into 2NF.
31 With an example explain a Functional Dependency. 3
Functional Dependencies is the Constraint between 2 attributes

A functional dependency is a constraint between two sets of attributes from the


database. Suppose that our relational database schema has n attributes A1, A2, ..., An.
If we think of the whole database as being described by a single universal relation
schema R = {A1, A2, ... , An}.

A functional dependency, denoted by X -> Y, between two sets of attributes X and Y


that are subsets of R, such that any two tuples t1 and t2 in r that have t1[X] = t2[X],
they must also have t1[Y] = t2[Y].
32 What are the relational algebra operations supported in SQL? Write an
an example for each operation. 8
Basic relational algebra operations
It is a procedural query language
 Select operation (σ) eg σ sal>1000(employee)
 Project operation (π) eg π name, salary(σ eno = 5(employee))
 Rename (ρ) eg ρs(R) – rename the relation
 Set theoretic operation
o Union R Ụ S
o Intersection R S
o Set difference R - S
 Cross join (X) or Cartesian product
 Join operation( )
Relational algebra
 select clause, from, where clauses
 rename
 string(%)
 order by
 arithmetic operations
 set operations (union, intersect, except)
 aggregate function (avg, min, max, sum, count)
 nested query
 join
33 What is decomposition and how does it address redundancy? 3
Decomposition is the process of breaking down in parts or elements.
It replaces a relation with a collection of smaller relations.
It breaks the table into multiple tables in a database.
It should always be lossless, because it confirms that the information in the original
relation can be accurately reconstructed based on the decomposed relations.
If there is no proper decomposition of the relation, then it may lead to problems like
loss of information.
34 What is tuple relational Calculus? Give example.
Tuple relational Calculus – based on specifying a number of tuple variables.
{ t / Condn(t)}
t – tuple variable
Condn(t) – conditional expression
Eg) { t / employee(t) and t. salary>50,000}
35 What is Domain Relational Calculus? Give example.
Domain Relational Calculus:
- differs in the type of variable used in tuple relational calculus
- having variable range over tuples
{x1, x2, x3…/condn(x1, x2, x3..xn, xn+1,…xn+m}
x1…Doamin variables
eg) {c,d / employee(‘ABC’, b,c,d}
36 what is a Stored Procedure? What are the advantages and disadvantages and
disadvantages of a procedure. Create a procedure to hike
salary of employee for dept no 10,by 10% .
salary of employee for dept no 20, by 20%.

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.

create procedure hike_salary()


begin
declare done int default 0;
declare CURSOR cur_emp IS
SELECT department_id, salary
FROM employees
WHERE department_id in (40,70)
FOR UPDATE OF salary;

declare did int;


declare sal double;
declare new_sal double;
new_sal number(12,2);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

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.

CREATE OR REPLACE TRIGGER mytrig2


AFTER DELETE OR INSERT OR UPDATE ON employee
FOR EACH ROW
BEGIN
IF DELETING THEN
INSERT INTO xemployee (emp_ssn, emp_last_name,emp_first_name, deldate)
VALUES (:old.emp_ssn, :old.emp_last_name,:old.emp_first_name, sysdate);
ELSIF INSERTING THEN
INSERT INTO nemployee (emp_ssn, emp_last_name,emp_first_name, adddate)
VALUES (:new.emp_ssn, :new.emp_last_name,:new.emp_first_name, sysdate);
ELSIF UPDATING('emp_salary') THEN
INSERT INTO cemployee (emp_ssn, oldsalary, newsalary, up_date)
VALUES (:old.emp_ssn,:old.emp_salary, :new.emp_salary, sysdate); ELSE
INSERT INTO uemployee (emp_ssn, emp_address, up_date)
VALUES (:old.emp_ssn, :new.emp_address, sysdate);
END IF;
END;
/
39 What is a view? How it differs from table?
answer:
A view consists of rows and columns just like a table.
The difference between a view and a table is that views are
definitions built on top of other tables (or views),
and do not hold data themselves. ...
It can also be built on top of another view.
CREATE VIEW [Brazil Customers] AS
SELECT CustomerName, ContactName
FROM Customers
WHERE Country = "Brazil";

40 Explain inner joins, outer joins with


suitable examples?
answer:inner join:
1.Theta Join(Equi and Non equi)
2.Natural Join
3.Self Join
Outer Join
Left,right and full
41 What is a view? Explain with the syntax.

B) Write a view creation to include details of employees with department


information.
(Consider the relating attribute as dept_no).

42 List out differences between stored procedure and function in SQL.

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.

45 Define relational calculus. Explain types with example.

46 List out informal guidelines to design a good relational schema. Explain each.

47 What you mean by update anomaly? Explain the types.

48 What is normalization? State 1 NF. Explain with example.

49 State 2 NF. Explain with example.

50 What is a view? How it differs from table?


answer:
A view consists of rows and columns just like a table.
The difference between a view and a table is that views are
definitions built on top of other tables (or views),
and do not hold data themselves. ...
It can also be built on top of another view.
CREATE VIEW [Brazil Customers] AS
SELECT CustomerName, ContactName
FROM Customers
WHERE Country = "Brazil";

You might also like