0% found this document useful (0 votes)
7 views35 pages

3sembca Dbms Lab Programs 2024

The document outlines a practical course on Database Management Systems, specifically focusing on PL/SQL programs. It includes various exercises such as checking for palindromes, updating employee salaries, displaying employee records, and creating triggers and procedures for managing data. The document is prepared by S.H. Khaseem, a lecturer at K.V. Subba Reddy Degree College.

Uploaded by

kamalakar76
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)
7 views35 pages

3sembca Dbms Lab Programs 2024

The document outlines a practical course on Database Management Systems, specifically focusing on PL/SQL programs. It includes various exercises such as checking for palindromes, updating employee salaries, displaying employee records, and creating triggers and procedures for managing data. The document is prepared by S.H. Khaseem, a lecturer at K.V. Subba Reddy Degree College.

Uploaded by

kamalakar76
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/ 35

SEMESTER-III

COURSE 5: DATABASE MANAGEMENT SYSTEM


Practical Credits: 1 2 hrs/week
List of Experiments

PL/SQL PROGRAMS

1. Writea PL/SQL program to check the given string is palindrome o rnot.


2. The HRD manager has decide to raise the employee salary by 15% write a PL/SQL block to accept the
employee number and update the salary of that employee. Display appropriate message based on the
existence of the record in Emp table.
3. Write a PL/SQL program to display top 10rows in Emp table based on their job and salary.
4. Write a PL/SQL program to raise the employee salary by 10% for department number 30 people and also
maintain the raised details in the raise table.
5. Create a procedure to update the salaries of Employees by 20%, for those who are not getting commission
6. Write a PL/SQL procedure to prepare an electricity bill by using following table. Table used: Elect
Name Null? Type
MNNO NOT NULL NUMBER(3)
CNAME VARCHAR2(20)
CUR_READ NUMBER(5)
PREV_READ NUMBER(5)
NO_ UNITS NUMBER(5)
AMOUNT NUMBER(8,2)
SER_TAX NUMBER(8,2)
NET_AMT NUMBER(9,2)

7. Create a trigger to avoid any transactions(insert, update, delete) on EMP table on Saturday & Sunday.

Prepared by S.H.Khaseem,
MCA., MBA
Lecturer in Computer Science
K.V. Subba Reddy Degree College, Allagadda.
9010797739

1
PL/SQL PROGRAMS

1. Write a PL/SQL program to check the given string is palindrome or not.

DECLARE
STR VARCHAR2(20);
RSTR VARCHAR2(20);
I NUMBER(5);
TSTR VARCHAR2(20);
BEGIN
STR:=’&STR’;
I:=LENGTH(STR);
TSTR:=STR;
WHILE(I>=1)
LOOP
RSTR:=RSTR||SUBSTR(STR,I,1);
I:=I-1;
END LOOP;
IF(TSTR=RSTR) THEN
DBMS_OUTPUT.PUT_LINE(STR||' IS A PALINDROM');
ELSE
DBMS_OUTPUT.PUT_LINE(STR||' IS NOT A PALINDROM');
END IF;
END;
/

OUTPUT

Enter value for str: MADAM


old 7: STR:='&STR';
new 7: STR:='MADAM';
MADAM IS A PALINDROM

2
2. The HRD manager has decide to raise the employee salary by 15% write a PL/SQL block to accept
the employee number and update the salary of that employee. Display appropriate message based
on the existence of the record in Emp table.
DECLARE
eno varchar2(6);
BEGIN
dbms_output.put_line('Enter employee number');
eno:=’&eno’;
UPDATE EMP SET SAL=SAL+(FLOOR(SAL*15/100)) WHERE ENO=eno;
IF SQL%FOUND THEN
dbms_output.put_line('salary updated');
ELSE
dbms_output.put_line('salary not updated');
END IF;
END;
/
Note Create employee table with columns eno,ename,sal,job,deptno,commission and execute the
above program.

output

Enter value for eno: e102


old 5: eno:='&eno';
new 5: eno:='e102';
Enter employee number
salary updated

3
3. Write a PL/SQL program to display top 10rows in Emp table based on their job and salary.

DECLARE
CURSOR crsr_emp IS SELECT ENO,ENAME,SAL, JOB FROM EMP WHERE JOB='developer'
AND SAL>15000;
veno EMP.ENO%type;
vename EMP.ENAME%type;
vejob EMP.JOB%type;
vsal EMP.SAL%type;
BEGIN
OPEN crsr_emp;
IF crsr_emp%ISOPEN THEN
LOOP
FETCH crsr_emp INTO veno,vename,vsal,vejob;
EXIT WHEN crsr_emp%NOTFOUND;
dbms_output.put_line(veno || ' ' || vename || ' ' || vsal || ' ' || vejob );
END LOOP;
END IF;
END;
/
Output

e102 mahesh 19837 developer

4
4. Write a PL/SQL program to raise the employee salary by 10% for department number 30 people
and also maintain the raised details in the raise table.
Creating RAISED TABLE
create table raised(eno varchar(6),ename char(25),sal number(10,2),
job char(10),deptno varchar2(6));
CREATING TRIGGER
CREATE OR REPLACE TRIGGER AUDIT_EMP AFTER UPDATE ON EMP FOR EACH
ROW
DECLARE
BEGIN
INSERT INTO RAISED VALUES
(:OLD.ENO,:OLD.ENAME,:OLD.SAL,:OLD.JOB,:OLD.DEPTNO);
END;
/
PLSQL PROGRAM
DECLARE
dno varchar2(6);
BEGIN
dno:='&dno';
UPDATE EMP SET SAL=SAL+(SAL*10/100) WHERE deptno=dno;
END;
/

output
Enter value for dno: d30
old 4: dno:='&dno';
new 4: dno:='d30';
PL/SQL procedure successfully completed.
RAISED TABLE DATA
ENO ENAME SAL JOB DEPTNO
------ ------------------------- ---------- ---------- ------
e101 rajesh 13225 developer d30

5
5. Create a procedure to update the salaries of Employees by 20%, for those who are not getting
Commission.
CREATING PROCEDURE
CREATING PROCEDURE
CREATE OR REPLACE PROCEDURE PRC_EMP
AS BEGIN
UPDATE EMP SET SAL=SAL+(SAL*(20/100)) WHERE COMMISSION=0;
END;
/
EXECUTING PROCEDURE
EXEC PRC_EMP;

DISPLAYING THE OUTPUT.


SELECT *FROM EMP;

output

Procedure created.
PL/SQL procedure successfully completed.

ENO ENAME SAL JOB DEPTNO COMMISSION


------ ------------------------- ---------- ---------- ------ ----------
e101 rajesh 18000 developer d30 0
e102 mahesh 19837 developer d20 10

6
6. Write a PL/SQL procedure to prepare an electricity bill by using following table. Table used: Elect
Name Null? Type
MNNO NOT NULL NUMBER(3)
CNAME VARCHAR2(20)
CUR_READ NUMBER(5)
PREV_READ NUMBER(5)
NO_ UNITS NUMBER(5)
AMOUNT NUMBER(8,2)
SER_TAX NUMBER(8,2)
NET_AMT NUMBER(9,2)

CREATING ELEC TABLE

CREATE TABLE ELECT(MNO NUMBER(3) PRIMARY KEY, CNAME CHAR(20) NOT NULL,
CUR_READ NUMBER(5), PREV_READ NUMBER(5), NO_UNITS NUMBER(5),
AMOUNT NUMBER(8,2), SER_TAX NUMBER(8,2), NET_AMT NUMBER(9,2));

INSERT THE FOLLOWING DATA USING INSERT INTO


QUERY:-

INSERT INTO ELECT(MNO,CNAME,CUR_READ,PREV_READ) VALUES(


&MNO,'&CNAME',&CUR_READ,&PREV_READ);
DATA:-
MNO CNAME CUR_READ PREV_READ
------ -------------------- --------- ---------
101 RAVI 800 700
102 RAJU 525 400
103 SURESH 365 200

CREATING PROCEDURE TO PREPARE ELECTRICITY BILL

CREATE OR REPLACE PROCEDURE PRC_ELECT


AS BEGIN
UPDATE ELECT SET NO_UNITS=CUR_READ-PREV_READ;
UPDATE ELECT SET AMOUNT=NO_UNITS*3;
UPDATE ELECT SET SER_TAX=AMOUNT*2/100;
UPDATE ELECT SET NET_AMT=AMOUNT+SER_TAX;
END;
/
EXECUTING PROCEDURE

EXEC PRC_ELECT;

DISPLAYING THE UPDATED DATA

SELECT *FROM ELECT;

7
OUTPUT

Procedure created.

PL/SQL procedure successfully completed.

MNO CNAME CUR_READ PREV_READ NO_UNITS AMOUNT


---------- -------------------- ---------- ---------- ---------- ----------
SER_TAX NET_AMT
---------- ----------
101 RAVI 800 700 100 300
6 306

102 RAJU 525 400 125 375


7.5 382.5

103 SURESH 365 200 165 495


9.9 504.9

8
7. Create a trigger to avoid any transactions(insert, update, delete) on EMP table on Saturday &
Sunday.
CREATING TRIGGER
CREATE OR REPLACE TRIGGER trg_sunday
BEFORE INSERT OR UPDATE OR DELETE ON emp
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN
IF TO_CHAR(SYSDATE, 'D') = '1' OR TO_CHAR(SYSDATE, 'D') = '7' THEN
RAISE_APPLICATION_ERROR(-20000, 'Cannot insert/update/delete records
on sunday or saturday');
END IF;
END;
/

output
Trigger created.

SQL> insert into emp values('e103','mahendra',25000,'developer','d40',0);


insert into emp values('e103','mahendra',25000,'developer','d40',0)
*
ERROR at line 1:
ORA-20000: Cannot insert/update/delete records on sunday or saturday
ORA-06512: at "SYSTEM.TRG_SUNDAY", line 3
ORA-04088: error during execution of trigger 'SYSTEM.TRG_SUNDAY'

You might also like