Dbms Lab Manual

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 51

MCS -043 DBMS LAB

DBMS LAB

Lab Manual MCSL-45

Page 1

MCS -043 DBMS LAB

Session I

1) Please attempt the following problems for the


teacher, class and pay scale relations given in this
section.
Table CreationLab Manual MCSL-45

Page 2

MCS -043 DBMS LAB


Create table TEACHER
(
T_NO
VARCHAR2(3) not null,
F_NAME VARCHAR2(50),
L_NAME VARCHAR2(25),
SALARY
NUMBER,
SUPERVISIOR VARCHAR2(3),
JOININGDATE DATE,
BIRTHDATE
DATE,
TITLE
VARCHAR2(3),
INC
NUMBER

IN
C

T_n F_na
o
me
101 111 Anga
d
10 112 shya
2
m
10 113 naufil

3
10
4

114 rohit

L_na
Sala Supervi Join_Dat
me
ry
sor
e
kamat 20
khan
04/06/20
12
lanke 5
Rahul
06/06/20
10
siddiq 10
sharma 11/03/20
ui
09
kada
15
dubey
11/03/20
m
09

DOB

Qualificati
on
11/03/199 BSc-It
2
11/12/198 MCA
0
23/05/198 BCA
5
11/03/198 Ph.D
9

Create/Recreate primary, unique and foreign key constraints


alter table TEACHER
add constraint TEACHER_PK primary key (T_NO)

Create table
create table CLA
(
CLASS_NO VARCHAR2(3) not null,
T_NO
VARCHAR2(3),
ROOM_NO VARCHAR2(3)
)
Create/Recreate primary, unique and foreign key constraints
alter table CLASS
add constraint CLASS_PK primary key (CLASS_NO)
Lab Manual MCSL-45

Page 3

MCS -043 DBMS LAB


alter table CLASS
add constraint CLASS_FK foreign key (T_NO)
references TEACHER (T_NO);

Class
_no
1001

T_no

Room_no

111

1002

112

1003

113

1004
1005

114
115

4
5

Create table
create table PAYSCALE
(
MIN_LIMIT NUMBER,
MAX_LIMIT NUMBER,
GRADE
VARCHAR2(5) not null
)
Create/Recreate primary, unique and foreign key constraints
alter table PAYSCALE
add constraint PAYSCALE_PK primary key (GRADE).

Min_limi
t

Max_limit Grade

Lab Manual MCSL-45

Page 4

MCS -043 DBMS LAB


Number

Number

10

11

60

45

60

34

55

76

a) Display the name of the teacher(s) who is (are) the youngest


among all the teachers.
Ans. Select * from teacher

Where birthdate = (select max (birthdate) from teacher);

b) Display details of all the teachers who have


the same job title as that of Angad
Ans. Select * from teacher where title = (select title from teacher
where
upper(f_name)=Angad;

c) Display the list of all the teachers who have joined after 10-Jul2009 and whose salary is equal to that of any of the teachers who
joined before 10-Jul-2012.
Ans. Select * from teacher

where join_date > 10-jul-2009


and salary in (select salary from teacher where joining date < 10jul-2012;
d) Use a correlated query to determine the
teachers who are not class teachers.
Lab Manual MCSL-45

Page 5

MCS -043 DBMS LAB


Ans. Select t.* from teacher t, payscale s where t.salary between
s.min_limit
and s.max_limit and s.grade = B.
e) Identify all those teachers who are in grade B.
Ans. Select t.* from teacher t , payscale s where exists (select * from
class c where t.t_no
= c.t_no) and (t.salary between s.min_limit and
s.max_limit) and t.grade=C;

f) Display the names and numbers of all teachers


who are class teachers and are in grade C.
Ans. Select t.* from teacher t, payscale s where t.salary between
s.min_limit
and s.max_limit and s.grade = B ;
g) Display the names of all teachers who are supervisors.
Ans. Select * from teacher where supervisor =YES;
h) Display the teacher id and salaries of all those
teachers who are in grade A or C and who
have at least two Ls in their names.
Ans. Select t.*, s.* from teacher t, payscale s
where (t.salary between s.min_limit and s.max_limit)
and (s.grade C or s.grade =B) and (t.f_name like %L%L%);
i) Display details of all those teachers who are class teachers of
classes 1 to 5.
Ans. Select t.*, c.* from teacher t, class c where t.t_no=c.t_no and
c.class_no between 1 and 5;
j) Display the names of all teachers along with their dates of birth
whose birthday is in the current month.
Ans. Select * from teacher

Lab Manual MCSL-45

Page 6

MCS -043 DBMS LAB


where to_char(to_date(birthdate,MM)) =
to_char(to_date(sysdate,MM));

2) In an Open University a students data is to be maintained


using relations. The university maintains data of all the
students, their batches, Regional Centers and study center
details. Each batch of students has one or more representative
students. The batches are taught by same or different faculty.
(a)

Design and implement the suitable relations for the University. Make
and state suitable assumptions.
Ans. Create table Student
STDID VARCHAR2(10) PRIMARY KEY,
NAME VARCHAR2(20),
ADDRESS VARCHAR2(40),
COURSEID VARCHAR2(10),
BATCHID VARCHAR2(10),
CONSTRAINTS STUDENT_FK FOREIGN KEY(COURSEID) REFERENCES
COURSE(COURSEID));

STD_ID

Name

Address

Course
_ID

Batch
_ID

5001

Angad kamat

Dombivali

0001

7001

5002

Shyam Lanke

Thane

0002

7002

5003

Naufil
Siddiqui

Bhiwandi

0003

7003

5004

Sandeep B

Badalapur

0004

7004

5005

Ajinkya

Pune

0005

7005

Lab Manual MCSL-45

Page 7

MCS -043 DBMS LAB

CREATE TABLE BATCH


BATCHID VARCHAR2(10),
RCID VARCHAR2(10),
SDYID VARCHAR2(10),
COURSEID VARCHAR2(10),
REPSTDID VARCHAR2(10),
CONSTRAINT BATCH_FK FOREIGN KEY(RCID) REFERENCES
REGCENT(RCID),
FOREIGN KEY(SDYID) REFERENCES SDYCENT(SDYID),
FOREIGN KEY(COURSEID) REFERENCES COURSE(COURSEID),
* FOREIGN KEY(REPSTDID) REFERENCES STUDENT(STDID));

Batch_ID

RC_ID

Course_ID

SDY_ID

Repstd_ID

111

49

101

009

112

50

102

010

113

51

103

011

114

52

104

012

115

53

105

013

Lab Manual MCSL-45

Page 8

MCS -043 DBMS LAB


ALTER TABLE BATCH ADD CONSTRAINT BATCH_PK PRIMARY
KEY(BATCHID);

CREATE TABLE TEACHERS


TCHID VARCHAR2(10) primary key,
TNAME VARCHAR2(20),
COURSEID VARCHAR2(10),
BATCHID VARCHAR2(10),
CONSTRAINTS TEACHER_FK FOREIGN KEY(COURSEID) REFERENCES
OURSE(COURSEID),
FOREIGN KEY(BATCHID) REFERENCES BATCH(BATCHID));
T_ID

T_Name

Course_ID

Batch_ID

88001

Asin

001

1111

88002

preeti

002

112

88003

sonal

003

1131

88004

Prinyanka

004

1141

88005

Shruti

005

115

Teacher table
T_ID

T_Name

Batch_ID

101

preeti

1111

102

Niran

112

103

Shruti

113

Lab Manual MCSL-45

Page 9

MCS -043 DBMS LAB

Course Table
Course
_ID

Course_N
ame

Course_f
ees

1001

MBA

10000

10021

MCA

25000

1003

Msc-IT

15000

1004

BBA

7000

Regional Center Table


R_ID

R_Name

State

Address

mulund

Maharashtr N.M towers


a

Mumbai

Maharashtr Indira Towers


a

Pune

Maharashtr A.k Soceity


a

Bhopal

M.P

D.k road

b) Write at least 15 queries (they must include at least one query of

each type given in this section 2.2) for the database. Implement
these queries using SQL. Also explain the purpose of each query.
Lab Manual MCSL-45

Page 10

MCS -043 DBMS LAB


Ans.
1. Display the details of study centre where a student named

Sanzaya reads;
Ans.
SELECT * FROM SDYCENT WHERE
SDYID=(SELECT SDYID FROM BATCH
WHERE BATCHID=(SELECT BATCHID FROM STUDENT
WHERE UPPER(NAME) LIKE 'SANZAYA%'`
2. Display the course fee where makhunu singh is admitted.
Ans.
BATCHID FROM STUDENT WHERE UPPER(NAME)='MAKHUNU
SINGH'));
3. Display the list of all teachers who take classes for batch B004
Ans. SELECT TNAME FROM TEACHERS WHERE BATCHID='B004';
4. Display the name of teachers who were working in regional
centre BBSR
Ans. SELECT TNAME FROM TEACHERS WHERE BATCHID IN(SELECT
BATCHID
FROM BATCH WHERE RCID=(SELECT RCID FROM
REGCENT WHERE RCNAME='BBSR'));
5. Display the name of teachers who teach in delhi and have atleast
two a in their name.
Ans.
SELECT TNAME FROM TEACHERS WHERE
UPPER(TNAME) LIKE '%A%A%' AND
BATCHID IN(SELECT BATCHID FROM BATCH WHERE
RCID=(SELECT RCID FROM REGCENT WHERE
UPPER(RCNAME)='NDLS'));

6. Display the name of student whose teachers name starts with


S.
Ans. SELECT NAME FROM STUDENT WHERE
BATCHID IN (SELECT BATCHID FROM BATCH WHERE
BATCHID IN (SELECT BATCHID FROM TEACHERS WHERE
UPPER(TNAME) LIKE 'S%'));
7. Display the name of the teachers with the batch details.
Ans. SELECT TNAME,B.BATCHID,RCID,SDYID,COURSEID,REPSTDID
* FROM TEACHERS T,BATCH B WHERE B.BATCHID=T.BATCHID
Lab Manual MCSL-45

Page 11

MCS -043 DBMS LAB

8. Display the name of students along with course details.


Ans. SELECT NAME,C.COURSEID,COURSENAME,COURSEFEE FROM
STUDENT S,
COURSE C, BATCH B WHERE
S.BATCHID=B.BATCHID
AND
* B.COURSEID=C.COURSEID;
9. Display the teachers name along with the students they teach.
Ans. SELECT TNAME,NAME FROM TEACHERS T,STUDENT S
WHERE T.BATCHID=S.BATCHID;
10.
Display course and study centers that offer them
Ans.
SELECT COURSENAME,SDYNAME FROM COURSE C,SDYCENT
S,BATCH B
WHERE
B.SDYID=S.SDYID
AND
* B.COURSEID=C.COURSEID;
11.
Display students name who read under teacher whose last
name is sehgal.
Ans. ELECT NAME FROM STUDENT WHERE BATCHID IN(
SELECT BATCHID FROM TEACHERS WHERE
* UPPER(TNAME) LIKE '%SEHGAL%');
12 .Display students and related regional centers.
Ans. SELECT NAME, RCNAME FROM STUDENT S,REGCENT R,BATCH B
WHERE
R.RCID=B.RCID
AND
S.BATCHID=B.BATCHID;
13 Display STUDENTID of students and teachers id.
Ans. SELECT STDID,TCHID FROM STUDENT S, TEACHERS T
WHERE S.BATCHID=T.BATCHID;
14. Display student, teacher and rc.
Ans. SELECT NAME,TNAME,RCNAME FROM STUDENT S,TEACHERS
T,REGCENT
R,BATCH B WHERE
S.BATCHID=T.BATCHID
AND
T.BATCHID=B.BATCHID
Lab Manual MCSL-45

Page 12

MCS -043 DBMS LAB


AND
B.RCID=R.RCID;

3) Design a suitable database system for a bank along with


20 possible queries to the database (the queries should
be such that their solution involves subqueries or joins or
both). Implement the database and the queries in a
commercial DBMS using SQL.
Ans. Bank database
create table acco_master (accno number(10) primary key,name *
varchar2(10),balance number(10))
SQL>
insert into acco_master values(&accno,'&name',&balance)
Enter value for accno:111
Enter value for name:faraz
Enter value for balance:200000.
SQL>
create table acco_trans(accno number(10),trans_date date,deb_cre
varchar2(10),check(deb_cre IN('debit','credit')),amount number(10),
process varchar2(10)
check(process IN('yes','no')) foreign key (accno)references
acco_master);
SQL> insert into acco_trans
values(&accno,'sysdate,'&deb_cre',&amount, '&process');
Enter value for accno:111
Enter value for deb_cre:debit
Lab Manual MCSL-45

Page 13

MCS -043 DBMS LAB


Enter value for amount:1000
Enter value for process:yes.

Session II

Lab Manual MCSL-45

Page 14

MCS -043 DBMS LAB

1) Please attempt the following problems for the


teacher, class and pay scale relations given in this
section.
(a) Create a view named supervisor_details that stores the

names and numbers of all the supervisors.


Ans. CREATE VIEW SUPERVISOR_DETAIL AS
SELECT F_NAME ||' '|| L_NAME AS NAME FROM TEACHER WHERE
TITLE='SUPERVISOR';
SELECT * FROM SUPERVISOR_DETAIL.
b) Create a non-unique index on the foreign key column of the
class table.
Ans.
CREATE INDEX SCOTT.class_index ON SCOTT.CLASS
(CLASS_NO)
LOGGING
NOPARALLEL;
c) Modify the view created in (a) and add details like salary,
job title, joining date, birth date etc. of all supervisors.
Ans. CREATE OR REPLACE VIEW SCOTT.SUPERVISOR_DETAIL
(NAME,SALARY,JOBTITLE,BIRTHDATE)
AS
SELECT F_NAME || ' ' || L_NAME AS NAME ,SALARY AS SALARY
,TITLE AS JOBTITLE ,
BIRTHDATE AS BIRTHDATE
FROM TEACHER
WHERE TITLE = 'SUPERVISOR';
d) Using the view created in (c) display details of all supervisors

who have worked for more than 15 years.


Ans. SELECT * FROM SUPERVISOR_DETAIL WHERE
TO_CHAR(SYSDATE,'YYYY') _
CHAR(JOININGDATE,'YYYY') >=15

e) Using the view created in (c) display details of all supervisors


who have worked for more than 15 years.
Ans.
CREATE OR REPLACE VIEW SCOTT.TECHER_DETAIL
AS
Lab Manual MCSL-45

Page 15

MCS -043 DBMS LAB


SELECT * FROM teacher
WHERE title = 'TGT' and salary>12000;
f) Create a view that stores the details of all the teachers who are
TGT and earning more than Rs.12000/-.
Ans . CREATE OR REPLACE VIEW SCOTT.SUPERVISOR_DETAIL
(NAME,salary,jobtitle,birthdate)
AS
SELECT f_name || ' ' || l_name AS NAME ,salary as salary ,title as
jobtitle , birthdate as birthdate
FROM teacher
WHERE title = 'supervisor';
g) Drop the view created in (e).
Ans.
Drop view SCOTT.TECHER_DETAIL.
h) Create a non-unique index on the names of teachers in the
teachers table.
Ans.
CREATE INDEX T_NAME_INDEX ON TEACHER(F_NAME)
i) Create a view named teacher_info that only allows users to
view the teacher id, name, salary and grade of the teachers. It
should not allow any user to change/update any information.
Ans.
CREATE VIEW TEACHER_INFO AS
SELECT T_NO, F_NAME ||' '|| L_NAME AS NAME, SALARY FROM
TEACHER;
SELECT * FROM TEACHER_INFO;
GRANT SELECT
ON TEACHER_INFO TO SYSTEM;
j) Create a view that displays details of all teachers who are in
grade B and are more than 40 years old.
Ans.
CREATE OR REPLACE VIEW SCOTT.TECHER_DETAIL1
AS
SELECT T_NO ,F_NAME||' '||L_NAME AS NAME,TITLE,SALARY,
TRUNC(MONTHS_BETWEEN(SYSDATE,BIRTHDATE)/12,0) AGE ,
GRADE
FROM TEACHER,
PAYSCALE
WHERE SALARY BETWEEN MIN_LIMIT AND MAX_LIMIT ;
SELECT * FROM TECHER_DETAIL1 WHERE AGE>40.
2) Design suitable views and indexes for the Bank database system
Lab Manual MCSL-45

Page 16

MCS -043 DBMS LAB


(Exercise 1, question. Create at least 5 suitable queries on each
of the views. Explain how the indexes created by you would
enhance the performance of the database system. Implement the
above in a commercial DBMS using SQL.
Ans, EXPLICIT CURSOR
DECLARE
C_T_NO TEACHER.T_NO
%TYPE; C_F_NAME
TEACHER.F_NAME%TYPE;
C_L_NAME
TEACHER.L_NAME%TYPE;
C_SALARY TEACHER.SALARY
%TYPE; CURSOR C1 IS
SELECT T_NO,F _NAME,
L_NAME, SALARY FROM
TEACHER;

BEGIN OPEN
C1; LOOP
FETCH C1 INTO C_T_NO, C_F_NAME, C_L_NAME,
C_SALARY ; EXIT WHEN NOT C1%FOUND;
UPDATE TEACHER SET SALARY = SALARY * 1.10
WHERE SALARY > 20000; END LOOP;
CLOSE C1;
END;

Lab Manual MCSL-45

Page 17

MCS -043 DBMS LAB

Session III

Lab Manual MCSL-45

Page 18

MCS -043 DBMS LAB

1) Please perform the following using the following relations:


Teacher(t_no, f_name, l_name, salary, supervisor,
joiningdate, birthdate, title) Class(class_no, t_no,
room_no)
Payscale(Min_limit, Max_limit, grade).
a) Calculate the bonus amount to be given to a teacher
depending on the following conditions:
if salary > 10000 then bonus is 10% of the salary.
if salary is between 10000 and 20000 then bonus is 20% of
the salary.
if salary is between 20000 and 25000 then bonus is 25% of
the salary. IV. if salary exceeds 25000 then bonus is 30% of
the salary.
Ans.
CREATE OR REPLACE PROCEDURE
CALCULATE_BONUS(T_NO IN INT) IS
VBONUS INT;
VTNO INT :=0;
VSAL INT :=0;
BEGIN
VBONUS :=0;
VTNO :=T_NO;
SELECT SALARY INTO VSAL FROM TEACHER1 WHERE
T_NO=VTNO;
IF(VSAL<10000) THEN
VBONUS :=VSAL*(10/100);
ELSIF(VSAL BETWEEN 10000 AND 20000) THEN
VBONUS :=VSAL*(20/100);
ELSIF(VSAL BETWEEN 20000 AND 25000) THEN
VBONUS :=VSAL*(25/100);
ELSIF(VSAL >25000) THEN
VBONUS :=VSAL*(30/100);
END IF;
DBMS_OUTPUT.PUT_LINE(VBONUS);
-- BONUS :=VBONUS;
END CALCULATE_BONUS;
b) Using a simple LOOP structure, list the first 10 records of the
teachers table.
Lab Manual MCSL-45

Page 19

MCS -043 DBMS LAB


Ans.

FETCHTEN TEACHER%ROWTYPE;
CNT NUMBER :=0;
CURSOR CURR
IS
SELECT * FROM TEACHER;
BEGIN
OPEN CURR;
LOOP
EXIT WHEN CURR%NOTFOUND OR CNT=10;
FETCH CURR INTO FETCHTEN;
DBMS_OUTPUT.PUT_LINE(FETCHTEN.T_NO||' '||
FETCHTEN.F_NAME||' '||FETCHTEN.L_NAME);
CNT :=CNT+1;
END LOOP;
CLOSE CURR;
END;
c) Create a procedure that selects all teachers who get a salary of
Rs.20, 000 and if less than 5 teachers are getting Rs.20, 000 then
give an increment of 5%.
Ans. FETCHTEN TEACHER%ROWTYPE;
CNT NUMBER :=0;
CURSOR CURR
IS
SELECT * FROM TEACHER;
BEGIN
OPEN CURR;
LOOP
EXIT WHEN CURR%NOTFOUND ;
FETCH CURR INTO FETCHTEN;
IF FETCHTEN.SALARY>20000 THEN
DBMS_OUTPUT.PUT_LINE(FETCHTEN.T_NO||' '||
FETCHTEN.F_NAME||' '||FETCHTEN.L_NAME);
ELSE
CNT :=CNT+1;
END IF;
END LOOP;
IF CNT<5 THEN
UPDATE TEACHER SET SALARY=SALARY*1.05 WHERE
SALARY<20000;
DBMS_OUTPUT.PUT_LINE('UPDATED SALARY');
END IF;
Lab Manual MCSL-45

Page 20

MCS -043 DBMS LAB


CLOSE CURR;
END;
d) Create a procedure that finds whether a teacher given by user
exists or not and if not then display teacher id not exists.
Ans. create or replace procedure findteacher(teach in varchar2)
is
c_t_no teacher.t_no%type;
cnt number :=0;
cursor curr is
select t_no from teacher where f_name=teach;
begin
open curr;
fetch curr into c_t_no;
if curr%notfound then
dbms_output.put_line('The teacher you enquired does not
exist');
else
dbms_output.put_line('The teachers id you enquired is '||
c_t_no);
end if;
close curr;
end;
e) Using FOR loop, display name and id of all those teachers who

are more than 58 years old.


Ans. CREATE OR REPLACE PROCEDURE SHOWABOVEF8
IS
C_F_NAME TEACHER.F_NAME%TYPE;
C_L_NAME TEACHER.L_NAME%TYPE;
BEGIN
SELECT F_NAME,L_NAME INTO C_F_NAME,C_L_NAME FROM
TEACHER WHERE
MONTHS_BETWEEN(SYSDATE,BIRTHDAT
DBMS_OUTPUT.PUT_LINE(C_F_NAME||'
'||C_L_NAME);
* END;
f) Using while loop, display details of all those teachers who are in
grade A
Ans.
Lab Manual MCSL-45

Page 21

MCS -043 DBMS LAB


EXCEPTION
DECLARE
C_ID TEACHER.T_NO
%TYPE; C_F_NAME
TEACHER.F_NAME
%TYPE; WANT_ID
NUMBER := 110;
BEGIN
SELECT T_NO, F_NAME INTO C_T_NO,
C_F_NAME FROM TEACHER WHERE T_NO
= WANT_ID;
DBMS_OUTPUT.PUTLINE ( TEACHER : ||
C_T_NO || ||C_F_NAME) EXCEPTION
WHEN INVALID_NUMBER THEN
DBMS_OUTPUT.PUTLINE(WANT_ID || NOT A
VALID TEACHER ID);

g) Create a procedure that displays the names of all those


teachers whose supervisor is Suman.
Ans. CREATE OR REPLACE PROCEDURE STAFFUNDER(SUPERV IN
VARCHAR2)
IS
TEMPHOLD TEACHER%ROWTYPE;
CURSOR CURR IS
SELECT * FROM TEACHER WHERE SUPERVISOR=SUPERV;
BEGIN
OPEN CURR;
LOOP
FETCH CURR INTO TEMPHOLD;
EXIT WHEN CURR%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(TEMPHOLD.F_NAME);
END LOOP;
CLOSE CURR;
END;
h) Calculate the tax to be paid by all teachers depending on
following conditions:
I. IF ANNUAL SALARY > 1,00,000 THEN NO TAX.
AI.IF ANNUAL SALARY IS BETWEEN 1,00,001 AND 1,50,000 THEN
TAX IS 20% OF THE
ANNUAL SALARY.
Lab Manual MCSL-45

Page 22

MCS -043 DBMS LAB


BI.IF ANNUAL SALARY IS BETWEEN 1,50,001 AND 2,50,000 THEN
TAX IS 30% OF THE
ANNUAL SALARY.
IV. IF SALARY EXCEEDS 2,50,000 THEN TAX IS 40% OF THE
ANNUAL SALARY.
Ans.

CREATE OR REPLACE PROCEDURE TAXCALC


IS
T_F_NAME TEACHER.F_NAME%TYPE;
T_SALARY TEACHER.SALARY%TYPE;
TAX TEACHER.SALARY%TYPE;
CURSOR CURR IS
SELECT F_NAME,SALARY*12 FROM TEACHER;
BEGIN
OPEN CURR;
LOOP
FETCH CURR INTO T_F_NAME,T_SALARY ;
EXIT WHEN CURR%NOTFOUND;
IF T_SALARY<100000 THEN
TAX:=0;
ELSIF T_SALARY<150000 THEN
TAX:=(T_SALARY-100000)*0.20;
ELSIF T_SALARY<250000 THEN
TAX:=(T_SALARY-150000)*0.30+50000*0.20;
ELSE
TAX:=(T_SALARY250000)*0.40+100000*0.30+50000*0.20;
END IF;
DBMS_OUTPUT.PUT_LINE(T_F_NAME||' OWES '||TAX||' AS TAX
');
END LOOP;
CLOSE CURR;
END;
i) Create a procedure that finds the names of all teachers with the
job title PRT and if the number of teachers returned is more than
10 than change the job title to TGT for the top 3 PRT teachers
based on their hire date.
Ans.

CREATE OR REPLACE PROCEDURE CHECKTGTEXCEED IS


VCOUNT INT ;
VMAXDATE DATE ;

Lab Manual MCSL-45

Page 23

MCS -043 DBMS LAB


BEGIN
LOOP
SELECT COUNT(*) INTO VCOUNT FROM TEACHER1 WHERE
TITLE='TGT' ;
SELECT MAX(JOININGDATE) INTO VMAXDATE FROM
TEACHER1 WHERE TITLE='TGT';
IF(VCOUNT>2) THEN
UPDATE TEACHER1 SET TITLE='PRT' WHERE
JOININGDATE=VMAXDATE AND TITLE='TGT';
ELSE
EXIT;
END IF;
END LOOP;
END CHECKTGTEXCEED;
2) Identify the need of procedures for the University database

system; for example, you can create a procedure that awards


2% grace marks for those students who have got 48% marks.
Design at least 5 such procedures. Implement these
procedures using an embedded SQL.
Ans. DECLARE
QTY NUMBER := 1; SANCTIONED
_AMT NUMBER := 1000; UNIT_PRICE
NUMBER := 10; TOT_AMT NUMBER :=
0;
BEGIN
WHILE TOT_AMT <
SANCTIONED_AMT LOOP
TOT_AMT := UNIT _PRICE * QTY;
QTY := QTY + 1;
END LOOP; END;

3) Implement at least five procedures for the Bank Database


system using embedded SQL.
Ans. SQL>SET SERVEROUTPUT ON
Lab Manual MCSL-45

Page 24

MCS -043 DBMS LAB


SQL>DECLARE
CURSOR C_BANK IS SELECT * FROM ACCO_TRANS;
V_BANK C_BANK%ROWTYPE;
BALANCE NUMBER(5);
BEGIN
OPEN C_BANK;
LOOP
FETCH C_BANK INTO V_BANK;
EXIT WHEN C_BANK%NOTFOUND;
IF V_BANK.PROCESS='NO' THEN
UPDATE ACCO_TRANS SET PROCESS='YES' WHERE
ACCNO=V_BANK.ACCNO;
IF V_BANK .DEB_CRE='CREDIT' THEN
UPDATE ACCO_MASTER SET BALANCE=BALANCE+V_BANK.AMOUNT
WHERE V_BANK.ACCNO=ACCO_MASTER.ACCNO;
ELSIF V_BANK.DEB_CRE='DEBIT' THEN
UPDATE ACCO_MASTER SET BALANCE=BALANCE-V_BANK.AMOUNT
WHERE V_BANK.ACCNO=ACCO_MASTER.ACCNO;
ELSIF BALANCE<=0 THEN
DBMS_OUTPUT.PUT_LINE('TRANSACTION NOT POSSIBLE');
END IF;
END IF;
END LOOP;
CLOSE C_BANK;
END;

SQL>select * from acco_trans;


Acc
No
10121
2
10241
4
19870
4

Trans_D
ate
Jan-08
Feb-08
Dec-07
Mar-08

Lab Manual MCSL-45

Deb_Cr
e
debit
credit
credit
credit

Amt
5000
100
1000
20000

Page 25

Pro
ye
s
ye
s
ye
s

MCS -043 DBMS LAB


23451
7

ye
s

CURSOR FOR BANK DATABASE


SQL > CREATE TABLE DEPOSITOR (ACCNO PRIMARY KEY , CNAME
CHAR(10))
TABLE CREATED.
SQL > CREATE TABLE BORROWER (LOANNO NUMBER , CNAME
CHAR(10))
TABLE CREATED.
SQL > CREATE TABLE LOAN( LOANNO NUMBER , BRNAME
CHAR(10),AMT NUMBER)
TABLE CREATED.
SQL > CREATE TABLE ACCT-T( ACCTNO NUMBER , BAL NUMBER,
BRNAME CHAR(10), FOREIGN KEY (ACCTNO)
REFERENCES DEPOSITOR (ACCTNO)
TABLE CREATED.
SQL > INSERT INTO DEPOSITOR VALUES (&ACCNO , &CNAME );
ENTER VALUE FOR ACCNO: 101
ENTER THE VALUE FOR CNAME : ALAN
SQL > INSERT INTO ACCT-T VALUES( &ACCTNO , &BAL , '&BRNAME ');
ENTER VALUE FOR ACCNO: 101
ENTER THE VALUE FOR BAL: 20000
ENTER THE VALUE FOR BRNAME : TVM
SQL > SELECT * FROM DEPOSITOR;
ACCNOCNAME
101ALAN
102ANN
103BEN
SQL > INSERT INTO ACCT-T VALUES( &ACCTNO , &BAL , '&BRNAME ');
ENTER VALUE FOR ACCNO: 101
ENTER THE VALUE FOR BAL: 20000
ENTER THE VALUE FOR BRNAME : TVM
SQL > SELECT * FROM ACCT-T;
Lab Manual MCSL-45

Page 26

MCS -043 DBMS LAB


ACCNOBALBRNAME
------------- ------------------- ---------------10120000TVM
10210500EKM
1035000TCR
SQL > CREATE OR REPLACE TRIGGER
DECLARE
C VARCHAR2 (20)
BEGIN
IF (:NEW.BAL < :OLD.BAL) THEN
INSERT INTO LOAN VALUES (:NEW.ACCNO, :NEW.BRNAME ,
:OLD.BAL-:NEW.BAL);
SELECT CNAME INTO C FROM DEPOSITOR WHERE ACCNO =
NEW.ACCNO;
INSERT INTO BORROWER VALUES (:NEW.ACCNO,C);
ENDIF;
END;

SESSION IV

Lab Manual MCSL-45

Page 27

MCS -043 DBMS LAB

1) Please perform the following using the following relations:


Teacher(t_no, f_name, l_name, salary, supervisor,
joiningdate, birthdate,
title) Class(class_no, t_no,
room_no)
Payscale(Min_limit, Max_limit, grade).
a) Create a host language block to declare a cursor for displaying
teacher numbers and their names for all teachers having title
PGT.
Ans. CREATE OR REPLACE PROCEDURE E4A IS
VNO INT ;
VNAME VARCHAR2(30);
VREC VARCHAR2(100);
CURSOR C1 IS SELECT T_NO,F_NAME FROM TEACHER WHERE
TITLE='TGT';
BEGIN
--VNO :=0;
--VNAME :='';
VREC :='';
OPEN C1;
LOOP
EXIT WHEN C1%NOTFOUND;
FETCH C1 INTO VNO,VNAME;
VREC :=VNO ||' '|| VNAME;
DBMS_OUTPUT.PUT_LINE(VREC);
END LOOP;
CLOSE C1;
END E4A * ;
PROCEDURE CREATED.
SQL> EXEC E4A
6 JATIN
Lab Manual MCSL-45

Page 28

MCS -043 DBMS LAB


6 BBB
6 BBB
PL/SQL PROCEDURE SUCCESSFULLY COMPLETED.
b) Create a host language block using a cursor to calculate bonus
for teachers as 5% of their salary. Display on screen the teacher
details along with the bonus given.
Ans. DECLARE
VNO INT ;
VNAME VARCHAR2(30);
BONUS INT ;
CURSOR C1 IS SELECT F_NAME,SALARY FROM TEACHER;
BEGIN
VNO :=0;
VNAME :='';
OPEN C1;
DBMS_OUTPUT.PUT_LINE('NAME BONUS');
DBMS_OUTPUT.PUT_LINE('-------------');
LOOP
EXIT WHEN C1%NOTFOUND;
FETCH C1 INTO VNAME,VNO;
BONUS :=VNO*(5/100);
-- DBMS_OUTPUT.PUT_LINE(VNO||VNAME);
DBMS_OUTPUT.PUT_LINE(VNAME||', BONUS: '||BONUS);
END LOOP;
CLOSE C1;
* END;
c) Write a host language block to delete all the rows from the
teacher table where the salary is less than Rs.5000.
Ans. DECLARE
C_T_NO TEACHER.T_NO%TYPE;
C_F_NAME TEACHER.F_NAME%TYPE;
C_L_NAME TEACHER.L_NAME%TYPE;
C_SALARY TEACHER.SALARY%TYPE;
CURSOR C1 IS
SELECT t_no,f_name, l_name, salary
FROM teacher;
BEGIN
OPEN c1;
LOOP
FETCH c1 INTO c_t_no, c_f_name, c_l_name, c_salary ;
EXIT WHEN NOT c1%FOUND;
Lab Manual MCSL-45

Page 29

MCS -043 DBMS LAB


UPDATE teacher SET salary = salary * 1.10 WHERE salary <
5000;
END LOOP;
CLOSE c1;
END;
d) Write a host language code to insert the supervisor information
from teacher table to another
table called supervisor. The
new table should have only those records where the job title is
supervisor.
Ans.

DECLARE
CURSOR C2 IS
SELECT T_NO,F_NAME, L_NAME, SALARY
FROM TEACHER ;
TEACHER_REC C2%ROWTYPE;
BEGIN
OPEN C2;
FOR TEACHER_REC IN C2
LOOP
I F TEACHER_REC.SALARY > 20000
TEACHER_REC.TITLE = SUPERVISOR;
ENDIF;
END LOOP;
CLOSE C2;
END;

Lab Manual MCSL-45

Page 30

MCS -043 DBMS LAB

Session V

1) Write an embedded SQL block along with exceptions to select the


name of the teacher with a given salary. If more than one row is
returned than display more than one row retrieved. If no row is
returned then display no teacher with this salary.
Ans. CREATE OR REPLACE PROCEDURE E5A(TID IN INT) IS
VSAL INT;
VFNAME VARCHAR2(30);
VRC INT ;
BEGIN
SELECT F_NAME, SALARY,COUNT(*)AS SASA INTO
VFNAME,VSAL,VRC FROM
TEACHER WHERE T_NO=TID GROUP
BY F_NAME,SALARY;
IF(VRC>=1) THEN
DBMS_OUTPUT.PUT_LINE('RECOED FOUND');
ELSE
DBMS_OUTPUT.PUT_LINE('RECOED NOT FOUND');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20100,'NOT TID FOUND ENTER THE VALID
TEACHER ID ') ;
DBMS_OUTPUT.PUT_LINE('ENVALID TEACHER ID ');
WHEN NO_DATA_FOUND THEN
NULL;
Lab Manual MCSL-45

Page 31

MCS -043 DBMS LAB


WHEN OTHERS THEN
CONSIDER LOGGING THE ERROR AND THEN RE-RAISE
RAISE;
END E5A;

2) Create a program that updates a record into the teacher table.


Trap and handle the exception if the teacher id is not available in
the teacher table.
Ans. CREATE OR REPLACE PROCEDURE UPDATESAL(TEACHERNAME IN
VARCHAR2)
IS
CNT NUMBER:=0;
TF_NAME TEACHER.F_NAME%TYPE;
NOTEACHERFOUND EXCEPTION;
CURSOR CURR
IS
SELECT SALARY FROM TEACHER WHERE
F_NAME=TEACHERNAME;
BEGIN
OPEN CURR;
LOOP
FETCH CURR INTO TF_NAME;
EXIT WHEN CURR%NOTFOUND;
CNT:=CNT+1;
END LOOP;
CLOSE CURR;
IF CNT<1 THEN
RAISE NOTEACHERFOUND;
ELSE
UPDATE TEACHER SET SALARY=1.05*SALARY
WHERE F_NAME=TEACHERNAME;
END IF;
EXCEPTION
WHEN NOTEACHERFOUND THEN
RAISE_APPLICATION_ERROR('-20003','NO TEACHER WITH
THIS NAME');
END;

Lab Manual MCSL-45

Page 32

MCS -043 DBMS LAB

3) Write a program with exceptions that displays the details of all


those teachers who have reached maximum limit of their grade.
If no row is retrieved then raise the exception no teacher
reached max limit of grade.
Ans. DECLARE
FETCHTEN TEACHER%ROWTYPE;
CNT NUMBER :=0;
CURSOR CURR
IS
SELECT * FROM TEACHER;
BEGIN
OPEN CURR;
LOOP
EXIT WHEN CURR%NOTFOUND ;
FETCH CURR INTO FETCHTEN;
IF FETCHTEN.SALARY>20000 THEN
DBMS_OUTPUT.PUT_LINE(FETCHTEN.T_NO||' '||
FETCHTEN.F_NAME||' '||FETCHTEN.L_NAME);
ELSE
CNT :=CNT+1;
END IF;
END LOOP;
IF CNT<5 THEN
UPDATE TEACHER SET SALARY=SALARY*1.05 WHERE
SALARY<20000;
DBMS_OUTPUT.PUT_LINE('UPDATED SALARY');
END IF;
CLOSE CURR;
END;

4) Insert at least 5 new rows in the teacher table and then try to
rollback last 3 rows inserted to the table. (Here, you are required
to use save points).
Ans. create or replace procedure findteacher(teach in varchar2)
is
Lab Manual MCSL-45

Page 33

MCS -043 DBMS LAB


c_t_no teacher.t_no%type;
cnt number :=0;
cursor curr is
select t_no from teacher where f_name=teach;
begin
open curr;
fetch curr into c_t_no;
if curr%notfound then
dbms_output.put_line('The teacher you enquired does not
exist');
else
dbms_output.put_line('The teachers id you enquired is '||
c_t_no);
end if;
close curr;
end;
5) Write a program with exceptions that displays the names of all
teachers who will attain the age of 60 years in the current year.
If no row is retrieved than display suitable exception.
Ans. IF(VSAL<10000) THEN
VBONUS :=VSAL*(10/100);
ELSIF(VSAL BETWEEN 10000 AND 20000) THEN
VBONUS :=VSAL*(20/100);
ELSIF(VSAL BETWEEN 20000 AND 25000) THEN
VBONUS :=VSAL*(25/100);
ELSIF(VSAL >25000) THEN
VBONUS :=VSAL*(30/100);
END IF;
DBMS_OUTPUT.PUT_LINE(VBONUS);
-- BONUS :=VBONUS;
END CALCULATE_BONUS;
6) Write a PL/SQL block that displays all the rows from teacher
table if the teacher was hired for more than 10 years and still a
PRT. If no result than display suitable message.
Ans. CREATE OR REPLACE PROCEDURE TAXCALC
IS
T_F_NAME TEACHER.F_NAME%TYPE;
T_SALARY TEACHER.SALARY%TYPE;
TAX TEACHER.SALARY%TYPE;
CURSOR CURR IS
Lab Manual MCSL-45

Page 34

MCS -043 DBMS LAB


SELECT F_NAME,SALARY*12 FROM TEACHER;
BEGIN
OPEN CURR;
LOOP
FETCH CURR INTO T_F_NAME,T_SALARY ;
EXIT WHEN CURR%NOTFOUND;
IF T_SALARY<100000 THEN
TAX:=0;
ELSIF T_SALARY<150000 THEN
TAX:=(T_SALARY-100000)*0.20;
ELSIF T_SALARY<250000 THEN
TAX:=(T_SALARY-150000)*0.30+50000*0.20;
ELSE
TAX:=(T_SALARY250000)*0.40+100000*0.30+50000*0.20;
END IF;
DBMS_OUTPUT.PUT_LINE(T_F_NAME||' OWES '||TAX||' AS TAX
');
END LOOP;
CLOSE CURR;
END;
7) In all the embedded SQL program segments that you have created
so far for the University and Bank database system, create
suitable error handling features.
Ans. CREATE OR REPLACE PROCEDURE FINDTEACHER(TEACH IN
VARCHAR2)
IS
C_T_NO TEACHER.T_NO%TYPE;
CNT NUMBER :=0;
CURSOR CURR IS
SELECT T_NO FROM TEACHER WHERE F_NAME=TEACH;
BEGIN
OPEN CURR;
FETCH CURR INTO C_T_NO;
IF CURR%NOTFOUND THEN
DBMS_OUTPUT.PUT_LINE('THE TEACHER YOU ENQUIRED DOES
NOT EXIST');
ELSE
DBMS_OUTPUT.PUT_LINE('THE TEACHERS ID YOU ENQUIRED IS
'||C_T_NO);
END IF;
Lab Manual MCSL-45

Page 35

MCS -043 DBMS LAB


CLOSE CURR;
END;

Session VI

Lab Manual MCSL-45

Page 36

MCS -043 DBMS LAB

1) Write a trigger that is fired before the DML


statements execution on the TEACHER table.
The trigger checks the school timings based on
SYSDATE. Beyond the School working hours the
trigger raise an exception, which does not allow
any work to be happened.
Ans. CREATE OR REPLACE TRIGGER Q1
BEFORE INSERT OR UPDATE OR DELETE ON TEACHER
FOR EACH ROW
WHEN (TO_CHAR (SYSDATE,'HH24') BETWEEN 9 AND 23)
BEGIN
DBMS_OUTPUT.PUT_LINE('CANT DO ANY JOB OUTSIDE SCHOOL
TIMINGS');
END;
2) Write a trigger that is fired before an UPDATE
statement is executed for the teacher table.
The trigger should write the name of teacher,
user name and system date in an already
created table called UPDATE_TABLE
Ans.. CREATE TABLE UPDATE_TABLE( TEACHER_NAME
VARCHAR2(20),USER_NAME
VARCHAR2(20)
DEFAULT 'STUDENT', SYS_DATE DATE);
CREATE OR REPLACE TRIGGER :
BEFORE UPDATE ON TEACHER
FOR EACH ROW
DECLARE
BEGIN
INSERT INTO UPDATE_TABLE VALUES(:OLD.F_NAME,USER,SYSDATE);
END;
ON EXECUTING:
UPDATE TEACHER SET F_NAME=A WHERE F_NAME=PARAG;
UPDATE TEACHER SET L_NAME=A WHERE L_NAME=PATEL;
Lab Manual MCSL-45

Page 37

MCS -043 DBMS LAB

3) Write a trigger that is fired before any row is inserted in the


teacher table.
Ans. BEFORE UPDATE ON TEACHER
BEGIN
IF UPDATING THEN
DBMS_OUTPUT.PUT_LINE('YOU ARE GOING TO UPDATE THE TEACHER
TABLE');
END IF;
END;
4) Write a function and pass a job title to it. If
the TEACHER table does not contain any row
corresponding to that title then return false
otherwise true.
Ans. CREATE OR REPLACE FUNCTION CHK_FUCT(F_TITLE IN
VARCHAR(20))
IS
FF_NAME VARCHAR(20);
CNT INT;
BEGIN
SELECT F_NAME, COUNT(*) INTO FF_NAME,CNT FROM TEACHER
WHERE TITLE=F_TITLE;
IF (CNT>=1) THEN
RETURN 'SUCESS';
ELSE
RETURN 'NOT SUCESS';
END IF;
END CHK_FUCT;

5) Write a trigger that verifies the joining date


when a new row is inserted in the teacher
table. Joining date should be greater or equal
to current date.
Lab Manual MCSL-45

Page 38

MCS -043 DBMS LAB


Ans. ED
WROTE FILE AFIEDT.BUF
CREATE TRIGGER E6_5
AFTER INSERT ON TEACHER
FOR EACH ROW
DECLARE
JDATE TEACHER.JOININGDATE%TYPE;
CDATE DATE;
BEGIN
JDATE:=NEW.JOININGDATE;
CDATE:=SYSDATE;
IF JDATE>=CDATE THEN
DBMS_OUTPUT.PUT_LINE('JOINING DATE IS FINE');
COMMIT;
ELSE
ROLLBACK;
END IF;
END;
6) Write a function that gets the teacher id as
parameter and returns the class number
associated with that teacher. If the teacher is
not a class teacher then give suitable message
Ans. DECLARE
C_ID TEACHER.T_NO%TYPE;
C_F_NAME TEACHER.F_NAME%TYPE;
WANT_ID NUMBER := 110;
BEGIN
SELECT T_NO, F_NAME INTO C_T_NO, C_F_NAME FROM TEACHER
WHERE T_NO = WANT_ID;
DBMS_OUTPUT.PUTLINE ( TEACHER : || C_T_NO || ||C_F_NAME)
EXCEPTION
WHEN INVALID_NUMBER THEN
DBMS_OUTPUT.PUTLINE(WANT_ID || NOT A VALID TEACHER ID);
END;
CREATE OR REPLACE TRIGGER NEW_TEACHER _ID
AFTER INSERT ON TEACHER
FOR EACH ROW
DECLARE
O_T_NO TEACHER.T_NO%TYPE;
O_JOININGDATE TEACHER.JOININGDATE%TYPE;
BEGIN
Lab Manual MCSL-45

Page 39

MCS -043 DBMS LAB


SELECT T_NO_SEQUENCE.NEXTVAL
INTO O_T_NO
FROM DUAL;
:NEW.T_NO := O_T_NO;
:NEW.JOININGDATE := SYSDATE;
END;
7) Write a function and pass a teacher id to it. If
the TEACHER table does not contain that id
then return false otherwise true.
Ans. CREATE OR REPLACE PROCEDURE E4G(TID IN INT) IS
VID INT ;
VMSAL INT ;
VYSAL INT;
TAX INT ;
VTSAL INT ;
CURSOR C1 IS SELECT SALARY FROM TEACHER WHERE
T_NO=TID;
BEGIN
OPEN C1 ;
LOOP
EXIT WHEN C1%NOTFOUND;
FETCH C1 INTO VTSAL;
VYSAL :=VTSAL*12;
IF(VYSAL<100000) THEN
DBMS_OUTPUT.PUT_LINE('NO TAX');
ELSIF(VYSAL BETWEEN 100001 AND 150000) THEN
TAX := VYSAL*(20/100);
ELSIF(VYSAL BETWEEN 150001 AND 250000) THEN
TAX := VYSAL*(30/100);
ELSIF(VYSAL>250000) THEN
TAX := VYSAL*(40/100);
END IF;
END LOOP;
DBMS_OUTPUT.PUT_LINE(TAX);
CLOSE C1;
END E4G;
8) Write a function that takes teacher id as
parameter and returns back the name and
joining date of the teacher.
Lab Manual MCSL-45

Page 40

MCS -043 DBMS LAB


Ans. DECLARE
CURSOR C2 IS
SELECT T_NO,F_NAME, L_NAME, SALARY
FROM TEACHER ;
TEACHER_REC C2%ROWTYPE;
BEGIN
OPEN C2;
FOR TEACHER_REC IN C2
LOOP
I F TEACHER_REC.SALARY > 20000
TEACHER_REC.TITLE = SUPERVISOR;
ENDIF;
END LOOP;
CLOSE C2;
END;
9) Write appropriate triggers and functions for
the University and Bank database systems.
Ans. CREATE OR REPLACE TRIGGER E6_9
AFTER INSERT ON TEACHER ON UNIV
FOR EACH ROW
BEGIN
DBMS_OUTPUT.PUT_LINE(DATA INSERTED SUCESSFULLY);
END

Lab Manual MCSL-45

Page 41

MCS -043 DBMS LAB

Session VII

1) Assuming that in the teacher relation an attribute of object type


called dependent is added. Make dependent a type that may
consist of only one dependent. Add few records in the tables and

Lab Manual MCSL-45

Page 42

MCS -043 DBMS LAB


output them using a query on teacher name. Find if you can
search on a dependent name.
Ans. SQL> CREATE TYPE DEPENDENT AS OBJECT
(
NAME VARCHAR(20)
);
TYPE CREATED.
SQL> ED
WROTE FILE AFIEDT.BUF
CREATE TABLE TEACHER(
T_NO INT,
F_NAME VARCHAR(30),
L_NAME VARCHAR(30),
SALARY INT,
SUPERVISOR VARCHAR(50),
JOININGDATE DATE,
BIRTHDATE DATE,
TITLE VARCHAR(50)
D_NAME DEPENDENT
TABLE CREATED.
SQL> INSERT INTO TEACHER VALUES(
(10101, MP, JOSHI, 30000, MOHAN, 10-DEC-08, 10-FEB-07,
TGT,
DEPENDENT(SITA)
);
1 ROW CREATED.

2) Create at least two object types in both the University and Bank
database systems. Use these object types in few relations and
enter some data into these relations. Query these databases.
Ans. ED
WROTE FILE AFIEDT.BUF
CREATE TYPE TYPE4UNIV AS OBJECT
(
NAME VARCHAR(20),
PHONE NUMBER(10)
Lab Manual MCSL-45

Page 43

MCS -043 DBMS LAB


* );
TYPE CREATED.
SQL> CREATE TYPE TYPE4BANK AS OBJECT
(
BRANCH VARCHAR(20),
MANAGER VARCHAR(20)
);
TYPE CREATED.

Lab Manual MCSL-45

Page 44

MCS -043 DBMS LAB

Session VIII

1)

Add a nested table in the teacher relation. Do some queries


using nested tables?

Ans. CREATE TABLE STUDENT_CREDITS


(ROLLNO NUMBER(5), S_NAME VARCHAR2(25), SUBJECT_CREDITS
NEW_TYPE)
NESTED TABLE SUBJECT_CREDITS STORE AS NEW_TYPE_TABLE;
INSERT INTO STUDENT_CREDITS
VALUES (100, SUMAN , NEW_TABLE ( NEW_TYPE (ENGLISH , 30),
NEW_TABLE ( NEW_TYPE(HINDI, 35)));
SELECT S.CREDIT_HOURS FROM
THE (SELECT SUBJECTS_CREDIT FROM STUDENT_CREDITS WHERE
S_NAME = SUMAN) S
WHERE S.SUBJECT_NAME = ENGLISH;
2)

Create at least two nested tables for both the University and
Bank database systems. Use these tables and enter some data
into these relations. Query these databases

Ans.
CREATE TYPE ADDRESS_T AS OBJECT (
STREET VARCHAR2(30),
CITY VARCHAR2(20),
STATE CHAR(2),
ZIP CHAR(5) );
/
CREATE TYPE ADDRESS_TAB IS TABLE OF ADDRESS_T;
Lab Manual MCSL-45

Page 45

MCS -043 DBMS LAB


/
CREATE TABLE CUSTOMERS (
CUSTID NUMBER,
ADDRESS ADDRESS_TAB )
NESTED TABLE ADDRESS STORE AS CUSTOMER_ADDRESSES

Session IX

Lab Manual MCSL-45

Page 46

MCS -043 DBMS LAB


1) Identify the use of large object types in the teachers table. Do

some queries using these objects.


Ans. CREATE TABLE message (
msg_id NUMBER(8) NOT NULL PRIMARY KEY,
email_addVARCHAR(200),
nameVARCHAR (200),
messageCLOB,
posting_time DATE,
sort_keyVARCHAR (600));
DECLARE
Image10BLOB;
image_number INTEGER := 101;
BEGIN
SELECT item_blob INTO image10 FROM lob_table10
WHERE key_value = image_number;
DBMS_OUTPUT.PUT_LINE('Image size is:'||
DBMS_LOB.GETLENGTH(image10));
END;
2) Create at least two large objects for both the University and
Bank database systems. Enter some data into these relations.
Query these databases.
Ans. SQL> ED
WROTE FILE AFIEDT.BUF
DECLARE
IMAGE10 BLOB;
IMAGE_NO INTEGER:=101;
BEGIN
SELECT ITEM_BLOB INTO IMAGE10 FROM UNIVERSITIES
WHERE T_NO=IMAGE_NO;
DBMS_OUTPUT.PUT_LINE('IMAGE SIZE IS:'||
DBMS_LOB.GETLENGTH(IMAGE10);
END;
SQL> ED
WROTE FILE AFIEDT.BUF
DECLARE
IMAGE11 BLOB;
IMAGE_NUM INTEGER:=01;
BEGIN
SELECT ITEM_BLOB INTO IMAGE11 FROM BANK
WHERE B_NO=IMAGE_NUM;
DBMS_OUTPUT.PUT_LINE('IMAGE SIZE IS:'||
Lab Manual MCSL-45

Page 47

MCS -043 DBMS LAB


DBMS_LOB.GETLENGTH(IMAGE11);
END;

Lab Manual MCSL-45

Page 48

MCS -043 DBMS LAB

Session X

1) Create a user account class and give privileges related to


table/view creation, deletion, updating and dropping.
Ans. CREATE USER class
IDENTIFIED BY pass;
GRANT CREATE TABLE, DROP TABLE, CREATE VIEW, DROP VIEW
TO class;
2) Create a student account and give permission to this account for
only viewing the information on the relation Class (class_no, t_no,
room_no
Ans.. DENY UPDATE, DELETE, INSERT ON employee TO student
GO
CREATE USER student
@Eclass_no int,
@St_no money,
@room_no int
GRANT EXECUTE ON student TO Class
GO.
3) Create at least 3 to 4 different types of users for each of the
Lab Manual MCSL-45

Page 49

MCS -043 DBMS LAB


database systems: University and Bank. Design suitable access
privileges for the users. Grant these permissions to the users.
Ans. SQL> CREATE USER MURALI IDENTIFIED BY MPJOSHI;
USER CREATED.
SQL> CREATE USER BIKU IDENTIFIED BY DEEPAK;
SQL> CREATE USER SAMIR IDENTIFIED BY RAJAK;
USER CREATED.
USER CREATED
SQL> CREATE USER PRASHANT IDENTIFIED BY JAIN;
USER CREATED.
CREATING ACCESS PRIVILEGES:
T SELECT, INSERT, UPDATE TO MURALI
GRANT OPTIONS;
GRANT SUCCEEDED.
T SELECT, INSERT, UPDATE TO BIKU
GRANT OPTIONS;
GRANT SUCCEEDED
T SELECT, INSERT, UPDATE TO SAMIR
GRANT OPTIONS;
GRANT SUCCEEDED.
T SELECT, INSERT, UPDATE TO PRASHANT
GRANT OPTIONS;
GRANT SUCCEEDED.
.
4) Define different types of users for the Bank database and provide
them suitable access rights.
Ans. USER CREATION:
CREATE USER MANAGER IDENTIFIED BY MGR1;
USER CREATED.
CREATE USER CASHIER IDENTIFIED BY CASH1;
USER CREATED.
USER CREATED
CREATE USER GENERAL_MANAGER IDENTIFIED BY GM;
USER CREATED.
USER RIGHTS ASSIGNMENT
GRANT CREATE TABLE, UPDATE TABLE,
DROP TABLE, CREATE VIEW, UPDATE VIEW,
DROP VIEW TO GENERAL_MANAGER;
GRANT SUCCEEDED.
GRANT CREATE TABLE, UPDATE TABLE,
DROP TABLE, CREATE VIEW, UPDATE VIEW,
Lab Manual MCSL-45

Page 50

MCS -043 DBMS LAB


DROP VIEW TO MANAGER;
GRANT SUCCEEDED.
GRANT UPDATE TABLE,
CREATE VIEW, UPDATE VIEW, TO CLASS;
GRANT SUCCEEDED.
GRANT , UPDATE TABLE,
DROP TABLE, , UPDATE VIEW,TO CLERK;
GRANT SUCCEEDED.

Lab Manual MCSL-45

Page 51

You might also like