Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

NVQ LEVEL -5 WRITTEN EXAMINATION – Semester-2-2019 June

National Diploma in Information & Communication Technology


Database Systems-2 Three Hours
Instructions: Answer 4 (Four) questions

1) (i) What is meant by Nested sub queries (5 marks)


 SQL has an ability to nest queries within one another.
 A subquery is a SELECT statement that is nested within another
SELECT statement and which return intermediate results.
 SQL executes innermost subquery first, then next level.
(ii) What are common uses of sub queries. State 3 common usages (5 marks)
1. To perform tests for set membership
2. To perform tests for set comparisons
3. To perform tests for set cardinality
Etc… (Any other acceptable usage)
(iii) Consider 2 tables with sample data given below
Table: Employees
EMPLOYEE_id FIRST_NAME LAST_NAME JOB_ID SALARY DEPT_ID
500 SANDAMAL WIJESIRI CLERK 42,000 35
501 INDIKA HEWAGE SA_REP 46,000 45
502 KALANA PERERA SA_REP 37,000 45
510 ANURA MEDAGEDARA SA_REP 55,000 45
512 POORNIMA WIMALASENA CLERK 40,000 30
520 DEEPANI AHANGAMA IT_PROG 54,000 40

Table: Departments
DEPT_ID DEPT_NAME LOCATION GRADE
25 MARKETING Colombo 1
30 ACCOUNT Colombo 2
35 ADMIN Kandy 3
40 IT Kurunegala 3
45 SALES Colombo 1

Write a query including nested subquery to display,


a) All employees from the Employees table who works in Sales department (5 marks)

SELECT * FROM Employees WHERE DEPT_ID IN(SELECT DEPT_ID IN


Departments WHERE DEPT_NAME = “SALES”)
b) All employees from the Employees table who works in Colombo (5 marks)

SELECT * FROM Employees WHERE DEPT_ID IN(SELECT DEPT_ID IN


Departments WHERE LOCATION = “COLOMBO”)

c) All employees from the Employees table who works in a department which has more
Grade than 3 (That is grade 1 or grade 2)
(5 marks)
SELECT * FROM Employees WHERE DEPT_ID IN(SELECT DEPT_ID IN
Departments WHERE GRADE < 3)

2) (i) Why do we use Database Views? State 3 reasons (5 marks)


 A database view allows you to simplify complex queries
 A database view helps limit data access to specific users
 A database view enables computed columns
 A database view enables backward compatibility

(Any other acceptable reasons)

(ii) Compare and contrast Database tables and Database views. Point out at least 3 facts
(5 marks)
Database tables Database views
Table is a preliminary A view is a virtual table
storage for storing data and whose contents are defined by
information a query
Tables takes significant Views takes very little space
space to store, since they to store, since they do not
store actual data. store actual data.
A view can incorporate Several tables are used to
several tables into one store linked data and
virtual table records.

(Any other acceptable reasons)

(iii) Name 5 types of aggregate functions commonly used in database manipulations


(5 marks)
1. AVG 5. STDDEV
2. COUNT 6. SUM
3. MAX 7. VARIANCE
4. MIN
(iv) Use the sample table of Employee given above in question-1 and write SQL statements to
a) Create a view to find Job_ID and the highest salary given for each job categories
(5 marks)
CREATE VIEW MAX_SAL AS SELECT JOB_ID, MAX(SALARY) FROM
Employee GROUP BY JOB_ID
b) Create a view to find Job_ID and the no of employees for each job categories
(5 marks)
CREATE VIEW COUNT_EMP AS SELECT JOB_ID, COUNT(EMPLOYEE_ID)
FROM Employee GROUP BY JOB_ID

3) (i) Define Stored Procedures used in DBMS


(5 marks)
A stored procedure is a segment of declarative SQL code which is
stored in the database catalog and can be invoked later by a
program, a trigger or even a stored procedure.
(ii) Point out 4 advantages of using Stored Procedures in database implementation
(10 marks)
1. Stored procedures are useful when multiple client
applications are written in different languages or work on
different platforms, but need to perform the same database
operations.
2. Stored procedure increases performance of application. Once
created, stored procedure is compiled and stored in the
database catalog. It runs faster than uncompiled SQL commands
which are sent from application
3. Stored procedure reduced the traffic between application and
database server because instead of sending multiple
uncompiled long SQL commands statement, application only has
to send the stored procedure name and get the result back.
4. Stored procedure is reusable and transparent to any
application which wants to use it. Stored procedure exposes
the database interface to all applications so developer
doesn’t have to program the functions which are already
supported in stored procedure in all programs.
5. Stored procedure is secured. Database administrator can grant
the right to application which to access which stored
procedures in database catalog without granting any
permission on the underlying database
(Any other acceptable reasons)
(iii) Write a stored procedure called “AddNewRecords” to implement following operations
 To insert following record to the Employees table
(515, Nishanthi, Gunathilaka, CLERK, 40,000, 35)
 To delete IT Department from Departments table
 To update the Salary of specifying employee by 10%

(10 marks)
CREATE PROCEDURE AddNewRecords()
BEGIN
INSERT INTO Employee VALUES (515, “Nishanthi”, “Gunathilaka”,
“CLERK”, 40,000, 35);
DELETE FROM Department WHERE DEPT_NAME = “IT”;
END

4) (i) Define a Database Trigger (6 marks)


A trigger is a named database object that is associated with a
table, and that activates when a particular event occurs for the
table.
(ii) Consider a sample Database Trigger given below
DELIMITER
$$
CREATE TRIGGER Employee_Del
AFTER DELETE ON Employee FOR EACH ROW
BEGIN
INSERT into Resigned_Employees VALUES (OLD.EMPLOYEE_id,
OLD.FIRST_NAME, OLD.LAST_NAME, OLD.JOB_ID, OLD.SALARY,
OLD.DEPT_ID);
END
$$
Identify and state following items from the above
a) Trigger event
DELETE
b) Trigger time
AFTER
c) Trigger statement
INSERT into Resigned_Employees VALUES (OLD.EMPLOYEE_id,
OLD.FIRST_NAME, OLD.LAST_NAME, OLD.JOB_ID, OLD.SALARY,
OLD.DEPT_ID); (3 marks for each)
(iii) What is a Cursor? . Explain Cursors used in databases (10 marks)
A cursor is a temporary work area created in system memory when
a SQL statement is executed.
A cursor is a set of rows together with a pointer that identifies
a current row. It is a database object to retrieve data from a
result set one row at a time.

5) (i) State 3 types of database connectivity methods and mention the specific application for each
(5 marks)
ADO- Used to access to a relational or non-relational database
from both Microsoft and other database providers.
ODBC- ODBC is used to make an independent access of database
systems and operating systems.
JDBC- Used to connect Java applications to external, relational
database management systems (RDBMS).

(ii) Point out 4 tasks about Database Administration (5 marks)


1. Implement Server Installation
2. Implement Client Installation
3. Create Users and manage Authorization
4. Get Database Backup and backup methods
5. Scheduling jobs
(Any other acceptable tasks)

(iii) Assuming that you are the “Root” (or Administrator) of a relevant DBMS, write SQL
statements to,
a) Create a user called “Amaya” and password “abcd” (5 marks)

CREATE USER 'Amaya'@'localhost' IDENTIFIED BY 'abcd';

b) Grant Select privilege only to the user “Amaya” for Employee and Department tables
(5 marks)
GRANT SELECT ON *. Employee, *.Department TO 'Amaya'@'localhost';

c) Revoke Update and Delete privileges from the user “Assistant” for Employee and
Department tables (5 marks)
(Assume that the user “Assistant” and relevant privileges already exist)

REVOKE UPDATE,DELETE ON *. Employee, *.Department FROM


‘Assistant’@‘localhost’;

You might also like