1. Bahria University Islamabad
BSCS 4
DBMS Lab, Major Quiz 01 Solution
24th March 2011
Lab Instructor: Saad Hashmi
SECTION I
True/False(Negative marking applies) (0.5 marks)
Write T/F at the end of following statements. Correct answer .1 point, no attempt 0, incorrect -.05
1. A foreign key attribute can never contain a null value. F
2. Only Database Administrators can grant and revoke privileges to other users. T
3. DBMS Engine helps the user to create and maintain the database and define the structure of the files
in a database. F
4. DESCRIBE keyword is used to display the tuples of a table. F
5. Every primary key is also a superkey, but not vice versa. T
SECTION II (3 Marks)
Write SQL queries for the following. Refer to Figure 01 for attribute names.
a) Find the fName and dNo of employee whose ssn is 125. (.2)
Select fName, dNo from employee where ssn = 125;
b) Grant the delete and update operation privileges on EMPLOYEE table to guest. (.2)
Grant delete, update on employee to guest;
c) Find out the unique fName(s) of all the employees working in dNo 10. (.2)
Select DISTINCT fName from employee where dNo = 10;
d) Calculate the number of employees who earn salary of more than 5000. (.2)
Select Count(*) from employee where salary > 5000;
e) Add a new department with dName ‘Engineering’ and dNumber 25 in the Department table. (.3)
Insert into department (dName, dNumber) values (‘Engineering’, 25);
f) List the fName of employees who were hired after the date of 31st Dec ‘05 and before 01st Jan ‘07. (.3)
This was a trick question. Hire_date had to be between 1st Jan ’06 and 31st Dec’ 06 because after 31-DEC-05 and before
01-JAN-07 excludes these dates, and between keyword includes the end point dates.
Page 1
2. Select fName from employee where hire_date between ’01-JAN-06’ and ’31-DEC-06’;
Since hire_date attribute wasn’t mentioned in the employee table, any attribute name would be considered.
g) Retrieve the dNumber and dName of all the departments whose name begin with letter ‘S’ and has
letter ‘A’ as its third character. (.3)
Select dNumber, dName from department where dName like ‘S_A%’;
h) In the Employee relation, change the tuple values of the employee(s) to dNo = 15 and
salary = 14000 whose salary = 12000 and superssn = 67. (.3)
Update employee set dNo = 15, salary = 14000 where salary = 12000 and superssn = 67;
i) Delete the records of employees who work in dNo 15, 20 and 25. (.2)
Delete from employee where dNo in (15,20,25);
j) For each department in which more than two employees work, retrieve the dNo and the number of
employees who work in that department. (.3)
Select dNo, count(*) from employee group by dNo having count(*) > 2;
k) Create the EMPLOYEE table with the given constraints as shown in the figure. (.5)
CREATE TABLE EMPLOYEE
( FNAME varchar2(15) NOT NULL,
MINIT char, Not Null constraint doesn’t necessarily has to be
LNAME varchar2(15) NOT NULL, applied on the mentioned attributes.
SSN char(9) NOT NULL,
BDATE date,
ADDRESS varchar2(30),
SEX char,
SALARY number (10, 2),
SUPERSSN char(9),
DNO number (4) NOT NULL,
PRIMARY KEY (SSN) ,
FOREIGN KEY (SUPERSSN) REFERENCES EMPLOYEE(SSN) ,
FOREIGN KEY (DNO) REFERENCES DEPARTMENT(DNUMBER) ) ;
Figure 01
Page 2