0% found this document useful (0 votes)
30 views

DBMS Prac Journal

The document is a certificate certifying that a student named Chirag Bhatia has successfully completed the necessary course experiments for the subject of Database Management Systems during the academic year 2021-2022. It includes the professor in charge's signature and the date, as well as the head of the department and college seal with date. The student's roll number is also provided. The journal includes the student's practical work completed for each experiment aim, with the corresponding date and page number. The experiments cover topics such as triggers, sequences, PL/SQL blocks, procedures, functions and transactions.

Uploaded by

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

DBMS Prac Journal

The document is a certificate certifying that a student named Chirag Bhatia has successfully completed the necessary course experiments for the subject of Database Management Systems during the academic year 2021-2022. It includes the professor in charge's signature and the date, as well as the head of the department and college seal with date. The student's roll number is also provided. The journal includes the student's practical work completed for each experiment aim, with the corresponding date and page number. The experiments cover topics such as triggers, sequences, PL/SQL blocks, procedures, functions and transactions.

Uploaded by

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

DBMS PRACTICAL JOURNAL

CHIRAG BHATIA (SCS2122013)


S.I.E.S College of Arts, Science and Commerce
Sion(W), Mumbai – 400 022.

CERTIFICATE

This is to certify that Mr. / Miss. ___CHIRAG BHATIA______________________________


Roll No.___SCS2122013____________ Has successfully completed the necessary course of
experiments in the subject of DATABASE MANAGEMENT SYSTEMS during the academic year
20201 – 2022complying with the requirements of University of Mumbai, for the course of
S.Y.BSc. Computer Science [Semester-3]

Prof. In-Charge
Mr. Abuzar Ansari
(Database Management Systems)

Examination Date:
Examiner’s Signature & Date:

Head of the Department


Prof. Manoj Singh
College Seal
And
Date

1
Sr. No. Aim Date Page no.
1 Creating and working 6 JULY 2021 4
with Insert / Update /
Delete Trigger using
Before / After clause

2 Writing PL/SQL 6 JULY 2021 10


Blocks with basic
programming
constructs by
including following:
a. Sequential
Statements
b. unconstrained
loop

3 Sequences: 12 JULY 2021 13

a. Creating
simple Sequences
with clauses like
START WITH,
INCREMENT BY,
MAXVALUE,
MINVALUE, CYCLE |
NOCYCLE, CACHE |
NOCACHE, ORDER |
NOORECER.
b. Creating and
using Sequences for
tables.

4 Writing PL/SQL 19 JULY 2021 15


Blocks with basic
programming
constructs by
including following:
a. If...then...Else,
IF...ELSIF...ELSE... END
IF
b. Case
statement

2
5 Write a PL/SQL block 26 JULY 2021 18
with CASE WHEN
Statement
6 Writing PL/SQL 6 AUGUST 2021 21
Blocks with basic
programming
constructs for
following Iterative
Structure:
a. While-loop
Statements
b. For-loop
Statements.
7 Writing PL/SQL 9 AUGUST 2021 24
Blocks with basic
programming
constructs by
including a GoTO to
jump out of a loop
and NULL as a
statement inside IF
8 Writing Procedures in 24 AUGUST 2021 26
PL/SQL Block
Create an empty
procedure, replace a
procedure and call
procedure
Create a stored
procedure and call it
Define procedure to
insert data
9 Writing Functions in 24 AUGUST 2021 31
PL/SQL Block.
Writing a recursive
Functions in PL/SQL
Block
10 Study of transactions 20 SEPTEMBER 2021 36
rollback, commit

3
Practical 01:

Aim:

Creating and working with Insert/Update/Delete Trigger using Before/After clause.


Questions: -
Create table
emp (eno, ename, hrs, pno, super_no) and project (pname, pno, thrs, head_no)
where thrs is the total hours and is the derived attribute. Its value is the sum of all employees
working on that project. eno and pno are primary keys, head_no is foreign key to emp
relation. Insert 10 tuples and write triggers to do the following:
Creating a trigger to insert new employee tuple and display the new total hours from project
table.
Creating a trigger to change the hrs of existing employee and display the new total hours
from project table.
Creating a trigger to change the project of an employee and display the new total hours from
project table.
Creating a trigger to delete the project of an employee.

Code:

CREATE TABLE EMP7(ENO NUMBER(8) PRIMARY KEY, ENAME VARCHAR(20),HRS


NUMBER(8),PNO NUMBER(8),SUPER_NO NUMBER(8) UNIQUE);

DESC EMP7;

CREATE TABLE PROJECT(PNO NUMBER(8) PRIMARY KEY,PNAME VARCHAR(20), THRS


NUMBER(8), SUPER_NO NUMBER(8) CONSTRAINT SUPFK REFERENCES EMP7(SUPER_NO) );

DESC PROJECT;

CREATE OR REPLACE TRIGGER THRS

4
AFTER INSERT ON EMP7
FOR EACH ROW
WHEN (NEW.PNO IS NOT NULL)
BEGIN
UPDATE PROJECT SET THRS = THRS + NEW.HRS WHERE PNO = NEW.PNO;
END;
/

CREATE TRIGGER THRS1


AFTER UPDATE OF HRS ON EMP7
FOR EACH ROW
WHEN(NEW.PNO IS NOT NULL)
BEGIN
UPDATE PROJECT SET THRS = THRS + NEW.HRS-:OLD.HRS WHERE PNO = NEW.PNO;
END;
/

CREATE TRIGGER THRS2


AFTER UPDATE OF PNO ON EMP7
FOR EACH ROW
WHEN(NEW.PNO IS NOT NULL)
BEGIN
UPDATE PROJECT SET THRS = THRS+:NEW.HRS-:OLD.HRS WHERE PNO = NEW.PNO;
END;
/

CREATE TRIGGER THRS4


AFTER DELETE ON EMP7
FOR EACH ROW

5
WHEN(OLD.PNO IS NOT NULL)
BEGIN
UPDATE PROJECT SET THRS = THRS -: OLD.HRS WHERE PNO = OLD.PNO;
END;
/

Output:

6
7
8
9
Practical 02:

Aim:

Writing PL/SQL Blocks with basic programming constructs by including following:


Sequential Statements
unconstrained loop
Question: - Write a PL/SQL block to insert 100 values in the table using loop [exit when] also
auto increment the supplier id with the help of sequence.
Table: Supplier {supid number(5) , suppname varchar2(15)}

Code:

CREATE SEQUENCE SUPPID


MINVALUE 1
MAXVALUE 99999
START WITH 10
INCREMENT BY 5
CACHE 20;

DECLARE
V_COUNTER NUMBER(5):=0;
V_SUPPID SUPPLIER.SUPPID%TYPE;
V_SUPPNAME SUPPLIER.SUPPNAME%TYPE:='TA';
BEGIN
LOOP
INSERT INTO SUPPLIER (SUPPNAME,SUPPID) VALUES
(V_SUPPNAME,SUPPID.NEXTVAL);
V_COUNTER:=V_COUNTER+1;
EXIT WHEN V_COUNTER=100;

10
END LOOP;
END;
/

Output:

11
12
Practical 03:

Aim:

Sequences:
Creating simple Sequences with clauses like START WITH, INCREMENT BY, MAXVALUE,
MINVALUE, CYCLE | NOCYCLE, CACHE | NOCACHE, ORDER | NOORECER.
Creating and using Sequences for tables.
Question: - Write PL/SQL block to insert 10 records in Employee table , empno should be
auto incremented , should begin with 10, the employee number should recycle after reaching
to empno 50. Empno should increment by 10. [use sequence for empno ]

Code:

CREATE SEQUENCE EMPNO


MINVALUE 10
MAXVALUE 99999
START WITH 10
INCREMENT BY 10
CYCLE
CACHE 5;

DECLARE
V_COUNTER NUMBER(5):=0;
V_EMPNO EMP.EMPNO%TYPE;
V_EMPNAME EMP.ENAME%TYPE:='RAM';
BEGIN
LOOP
INSERT INTO EMP(ENAME,EMPNO) VALUES (V_EMPNAME,EMPNO.NEXTVAL);
V_COUNTER:=V_COUNTER+1;

13
EXIT WHEN V_COUNTER=10;
END LOOP;
END;
/

Output:

14
Practical 04:

Aim:

Writing PL/SQL Blocks with basic programming constructs by including following:


If...then...Else, IF...ELSIF...ELSE... END IF
Case statement
Question:-. Write a PL/SQL block to increase the salary of the employee by 15% whose date
of joining is 12 December 2000 otherwise increase salary by 5%.
Table : Employee { empno number(10), ename varchar2(10), salary number(10)}
PL/SQL:-
About ‘If...then...Else, IF...ELSIF...ELSE... END IF”

Code:

CREATE TABLE EMP5(ENO NUMBER(3) PRIMARY KEY, ENAME VARCHAR(25),SALARY


NUMBER(6),JOIN_DATE DATE);

INSERT INTO EMP5 VALUES(1,'DRAKE',15000,TO_DATE('19990505','YYYYMMDD'));


INSERT INTO EMP5 VALUES(2,'JOSH',15000,TO_DATE('20001212','YYYYMMDD'));
INSERT INTO EMP5 VALUES(3,'JOHN',20000,TO_DATE('20020805','YYYYMMDD'));
INSERT INTO EMP5 VALUES(4,'LUKE',10000,TO_DATE('20020806','YYYYMMDD'));

DECLARE
CURSOR EMP_CURSOR IS SELECT ENO, SALARY, JOIN_DATE FROM EMP5;
V_ENO EMP5.ENO%TYPE;
V_SALARY EMP5.SALARY%TYPE;
V_JOIN_DATE EMP5.JOIN_DATE%TYPE;
BEGIN
OPEN EMP_CURSOR;

15
LOOP
FETCH EMP_CURSOR INTO V_ENO, V_SALARY, V_JOIN_DATE;
IF EMP_CURSOR%NOTFOUND THEN
EXIT;
ELSIF V_JOIN_DATE = TO_DATE('20001212','YYYYMMDD') THEN
UPDATE EMP5 SET SALARY = V_SALARY*1.15 WHERE ENO = V_ENO;
ELSE
UPDATE EMP5 SET SALARY = V_SALARY*1.05 WHERE ENO = V_ENO;
END IF;
END LOOP;
CLOSE EMP_CURSOR;
END;
/

Output:

16
17
Practical 05

Aim:

Create a table lecturer with one of the attribute as major subject. Write a PL/SQL block with
CASE WHEN statement with variable which print the course name depending upon the major
subject for the specified lecturer id.
Table : Lecturer { lid NUMBER(4), lname varchar2(14), majorsubject varchar2(10),
coursename varchar2(15)}

Code:

CREATE TABLE LECTURER (LID NUMBER(4),LNAME VARCHAR2(14),MAJORSUBJECT


VARCHAR2(10),COURSENAME VARCHAR2(15));

INSERT INTO LECTURER VALUES(1,'RAJESH','CS','COMPUTERSCIENCE');


INSERT INTO LECTURER VALUES(2,'JAYESH','IT','INFOTECH');
INSERT INTO LECTURER VALUES(3,'SURESH','CS','COMPUTERSCIENCE');
INSERT INTO LECTURER VALUES(4,'RAMESH','BT','BIOTECHNOLOGY');

DECLARE
V_LID LECTURER.LID%TYPE := &V_LID;
V_LNAME LECTURER.LNAME%TYPE;
V_MAJORSUBJECT LECTURER.MAJORSUBJECT%TYPE;
V_COURSENAME LECTURER.COURSENAME%TYPE;
BEGIN
SELECT LNAME, MAJORSUBJECT,COURSENAME INTO V_LNAME, V_MAJORSUBJECT,
V_COURSENAME FROM LECTURER WHERE LID = V_LID;
CASE V_MAJORSUBJECT
WHEN 'CS' THEN DBMS_OUTPUT.PUT_LINE(V_LNAME || ' : ' ||
V_COURSENAME);

18
WHEN 'IT' THEN DBMS_OUTPUT.PUT_LINE(V_LNAME || ' : ' ||
V_COURSENAME);
WHEN 'BT' THEN DBMS_OUTPUT.PUT_LINE(V_LNAME || ' : ' ||
V_COURSENAME);
END CASE;
END;
/

Output:

19
20
Practical 06

Aim:

Writing PL/SQL Blocks with basic programming constructs for following Iterative Structure:
While-loop Statements
For-loop Statements.

Question: - Write a PL/SQL block to print department wise total number of employees along
the total salary paid to that department. [ USE : 1)While loop 2)For loop ]

Table(s) :
Employee {empno number(4) primary key, ename varchar2(10), salary number(10), deptno
number(4) reference key}
Department {deptno number(4) primary key , dname varchar2(4)}

Code:

DECLARE
CURSOR DEPARTMENT_CURSOR IS SELECT DEPTNO, DNAME FROM DEPT;
V_DEPTNO DEPT.DEPTNO%TYPE;
V_DNAME DEPT.DNAME%TYPE;
V_SALARY_SUM NUMBER(10);
V_EMPLOYEE_COUNT NUMBER(3);
BEGIN
OPEN DEPARTMENT_CURSOR;
FETCH DEPARTMENT_CURSOR INTO V_DEPTNO, V_DNAME;
WHILE DEPARTMENT_CURSOR%FOUND LOOP
SELECT COUNT(EMPNO), SUM(SAL) INTO V_EMPLOYEE_COUNT,
V_SALARY_SUM FROM EMP WHERE DEPTNO = V_DEPTNO;

21
DBMS_OUTPUT.PUT_LINE(V_DEPTNO || ' ' || V_DNAME || ' ' || ' TOTAL SALARY: ' ||
V_SALARY_SUM || ' EMPLOYEE COUNT: ' || V_EMPLOYEE_COUNT );
FETCH DEPARTMENT_CURSOR INTO V_DEPTNO, V_DNAME;
END LOOP;
CLOSE DEPARTMENT_CURSOR;
END;
/

Output:

Code:

DECLARE
CURSOR DEPARTMENT_CURSOR IS SELECT * FROM DEPT;
V_SALARY_SUM NUMBER(10);
V_EMPLOYEE_COUNT NUMBER(3);
V_DEPARTMENT_ROW DEPT%ROWTYPE;
BEGIN

22
FOR V_DEPARTMENT_ROW IN DEPARTMENT_CURSOR LOOP
SELECT COUNT(EMPNO), SUM(SAL) INTO V_EMPLOYEE_COUNT,
V_SALARY_SUM FROM EMP WHERE DEPTNO = V_DEPARTMENT_ROW.DEPTNO;
DBMS_OUTPUT.PUT_LINE(V_DEPARTMENT_ROW.DEPTNO || ' '||
V_DEPARTMENT_ROW.DNAME || ' ' || ' TOTAL SALARY: ' || V_SALARY_SUM || ' EMPLOYEE
COUNT: ' || V_EMPLOYEE_COUNT);
END LOOP;
CLOSE DEPARTMENT_CURSOR;
END;
/

Output:

23
Practical 07

Aim:

Writing PL/SQL Blocks with basic programming constructs by including a GoTO to jump out of
a loop and NULL as a statement inside IF
Question: -Write a PL/SQL block to print employees name and number excluding empno 10
and 15 along their department name. [USE : Goto statement]
Table employee {empno number(5) primary key , ename varchar2(15), deptno number(4)
reference key}
Department {deptno number(4) primary key , dname varchar2(4)}

Code:

DECLARE
CURSOR EMPLOYEE_CURSOR IS SELECT * FROM EMP;
V_EMPLOYEE_ROW EMP%ROWTYPE;
V_DNAME DEPT.DNAME%TYPE;
BEGIN
FOR V_EMPLOYEE_ROW IN EMPLOYEE_CURSOR LOOP
IF V_EMPLOYEE_ROW.EMPNO = 7369 OR V_EMPLOYEE_ROW.EMPNO = 7499
THEN
GOTO SKIP;
END IF;
SELECT DNAME INTO V_DNAME FROM DEPT WHERE DEPTNO =
V_EMPLOYEE_ROW.DEPTNO;
DBMS_OUTPUT.PUT_LINE(V_EMPLOYEE_ROW.EMPNO || ' ' ||
V_EMPLOYEE_ROW.ENAME || ' ' || V_DNAME);
<<SKIP>>
NULL;
END LOOP;

24
END;
/

Output;

25
Practical 08

Aim:

Writing Procedures in PL/SQL Block


a. Create an empty procedure, replace a procedure and call procedure
b. Create a stored procedure and call it
c. Define procedure to insert data

Question: - The HRD manager has decided to rise the salary of employee by 0.15. write PL/SQL block to
accept the employee number and update the salary of that employee through procedure. Display appropriate
message based on the existence of the record in employee table.
Table: employee {empno number(5) primary key , ename varchar2(15)}

Question: Write PL/SQL procedure to accept the employee number and display employees
name along salary and his department name.

Table employee {empno number(5) primary key , ename varchar2(15), deptno number(4)
reference key}
Department {deptno number (4) primary key , dname varchar2(4)}

Code:

CREATE OR REPLACE PROCEDURE INCREASE_SALARY (P_EMPNO EMP.EMPNO%TYPE)


IS
V_SALARY EMP.SAL%TYPE;
V_ENAME EMP.ENAME%TYPE;
BEGIN
SELECT ENAME, SAL INTO V_ENAME, V_SALARY FROM EMP WHERE EMPNO =
P_EMPNO;
UPDATE EMP SET SAL = V_SALARY*1.15 WHERE EMPNO = P_EMPNO;
DBMS_OUTPUT.PUT_LINE('INCREMENTED SALARY FOR ' || V_ENAME || ' FROM ' ||
V_SALARY || ' TO ' || V_SALARY*1.15);
EXCEPTION
WHEN NO_DATA_FOUND THEN
26
DBMS_OUTPUT.PUT_LINE('RECORD NOT FOUND');
END;
/

DECLARE
V_EMPNO EMP.EMPNO%TYPE := &V_EMPNO;
BEGIN
INCREASE_SALARY(V_EMPNO);
END;
/

Output:

27
Code:

CREATE OR REPLACE PROCEDURE DISPLAY_EMPLOYEE (P_EMPNO EMP.EMPNO%TYPE)


IS
V_ENAME EMP.ENAME%TYPE;
V_SALARY EMP.SAL%TYPE;
V_DEPTNO EMP.DEPTNO%TYPE;
V_DNAME DEPT.DNAME%TYPE;
BEGIN
SELECT ENAME, SAL, DEPTNO INTO V_ENAME, V_SALARY, V_DEPTNO FROM EMP
WHERE EMPNO = P_EMPNO;
SELECT DNAME INTO V_DNAME FROM DEPT WHERE DEPTNO = V_DEPTNO;
DBMS_OUTPUT.PUT_LINE(V_ENAME || ' ' || V_SALARY || ' ' || V_DNAME);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('RECORD NOT FOUND');
END;
/

28
DECLARE
V_EMPNO EMP.EMPNO%TYPE := &V_EMPNO;
BEGIN
DISPLAY_EMPLOYEE(V_EMPNO);
END;
/

Output:

29
30
Practical 09

Aim:

Writing Functions in PL/SQL Block.


Question: -Write PL/SQL function to accept department id and display total number of
employees working in that department along the maximum salary of that department.
Table employee {empno number(5) primary key , ename varchar2(15), deptno number(4)
reference key}
Department {deptno number (4) primary key , dname varchar2(4)}

Writing a recursive Functions in PL/SQL Block


Question: Write PL/SQL recursive function to list top ten employees name along their salary.
Table employee {empno number(5) primary key , ename varchar2(15)}

Code:

CREATE OR REPLACE FUNCTION PRACTICAL9A_FUNCTION(P_DEPTNO DEPT.DEPTNO%TYPE)


RETURN VARCHAR2
IS
V_EMPLOYEE_COUNT NUMBER(2);
V_MAX_SALARY EMP.SAL%TYPE;
BEGIN
SELECT COUNT(EMPNO),MAX(SAL) INTO V_EMPLOYEE_COUNT, V_MAX_SALARY
FROM EMP WHERE DEPTNO = P_DEPTNO;
IF V_EMPLOYEE_COUNT = 0 THEN
RETURN 'NO RECORDS';
ELSE
RETURN 'EMPLOYEE COUNT: ' || V_EMPLOYEE_COUNT || 'MAX SALARY: ' ||
V_MAX_SALARY;
END IF;

31
END;
/

DECLARE
V_DEPTNO DEPT.DEPTNO%TYPE := &V_DEPTNO;
BEGIN
DBMS_OUTPUT.PUT_LINE(PRACTICAL9A_FUNCTION(V_DEPTNO));
END;
/

Output:

32
Code:

CREATE OR REPLACE FUNCTION PRACTICAL9B_FUNCTION(COUNTER NUMBER)


RETURN VARCHAR2
IS
V_ENAME EMP4.ENAME%TYPE;

33
V_SALARY EMP4.SALARY%TYPE;
BEGIN
IF COUNTER = 11 THEN
RETURN 'COMPLETED';
END IF;
SELECT ENAME, SALARY INTO V_ENAME, V_SALARY FROM EMP4 WHERE EMPNO =
COUNTER;
DBMS_OUTPUT.PUT_LINE(V_ENAME || ' ' || V_SALARY);
RETURN PRACTICAL9B_FUNCTION(COUNTER + 1);
END;
/

BEGIN
DBMS_OUTPUT.PUT_LINE(PRACTICAL9B_FUNCTION(1));
END;
/

Output:

34
35
Practical 10

Aim:

Write a PL/SQL block to auto insert 10 records in an employee table.


Keep updating salary of Blake and Clark by Rs.2000 and Rs.1500 respectively. As long as total
salary does not exceed to 20000.
As soon as total salary reaches to more than 20000 then undo the Last salary updates of
Blake and Clark

Code:

CREATE TABLE EMP4 (EMPNO NUMBER(5) PRIMARY KEY , ENAME VARCHAR2(15), SALARY
NUMBER(10))
INSERT INTO EMP4 VALUES(001,’BLAKE’,900);
INSERT INTO EMP4 VALUES(002,’CLARK’,1000);
INSERT INTO EMP4 VALUES(003,’ADAM’,200);
INSERT INTO EMP4 VALUES(004,’STEVE’,250);
INSERT INTO EMP4 VALUES(005,’PETER’,350);
INSERT INTO EMP4 VALUES(006,’OTIS’,300);
INSERT INTO EMP4 VALUES(007,’AVA’,500);
INSERT INTO EMP4 VALUES(008,’SAM’,550);
INSERT INTO EMP4 VALUES(009,’JOHN’,600);
INSERT INTO EMP4 VALUES(010,’KEVIN’,650);

DECLARE
TOTAL_SAL NUMBER(9);
BEGIN
INSERT INTO EMP4 VALUES(011,'DAVID',1000);
SAVEPOINT NO_UPDATE;

36
UPDATE EMP4 SET SALARY = SALARY+2000 WHERE ENAME = 'BLAKE';
UPDATE EMP4 SET SALARY = SALARY + 1500 WHERE ENAME = 'CLARK';
SELECT SUM(SALARY) INTO TOTAL_SAL FROM EMP4;
IF TOTAL_SAL>2000 THEN
ROLLBACK TO SAVEPOINT NO_UPDATE;
END IF;
COMMIT;
END;
/

Output:

37
38
39

You might also like