You are on page 1of 26

Practical –1:

Create table Employee with the following structure


Emp_id varchar2(20)
Ename varchar2(20)
Job varchar2(20)
Department varchar2(20)
Join date date
Salary number(10,2)
Using the following constraints
a) Employee id must start with “E”
b) The name of the employee should be entered as upper case letters
c) Salary starts from 2500.00 onwards
d) Job lies in the following category (Manager, clerk, accountant ,
teller).

SQL>

1 create table employee


2 (emp_id varchar2(20) constraint pk_emp primary key,
3 ename varchar2(20),
4 job varchar2(15),
5 department varchar2(20),
6 joindate date,
7 salary number(10,2),
8 constraint c1 check(emp_id like 'E%'),
9 constraint c2 check(ename =upper(ename)),
10 constraint c3 check(job in ('manager','clerk','accountant','teller')),
11* constraint c4 check(salary>=2500))
SQL> /

Table created.

SQL> desc employee


Name Null? Type
----------------------------------------- -------- ----------------------------

1
EMP_ID NOT NULL VARCHAR2(20)
ENAME VARCHAR2(20)
JOB VARCHAR2(15)
DEPARTMENT VARCHAR2(20)
JOINDATE DATE
SALARY NUMBER(10,2)

SQL>

Practical 2. Create two tables Employee and Department and perform


all types of join operations

a)Equijoin c)outer join

b)Non-Equi join d)selfjoin

SQL> select * from employee;

EMP_ID ENAME JOB DEPARTMENT


-------------------- -------------------- --------------- --------------------
JOINDATE SALARY DEPTNO
--------- ---------- ----------
E001 MANJEERABHARGAVI.V clerk accounts
12-JUN-07 9000 10

E002 MAMJANSHAWALI.SK teller commercial


12-JUL-07 9000 20

E003 K.NIRMALA manager accounts


12-JUN-00 15000 10

EMP_ID ENAME JOB DEPARTMENT


-------------------- -------------------- --------------- --------------------
JOINDATE SALARY DEPTNO
--------- ---------- ----------

2
E004 CH.GOPI accountant commercial
12-JUL-00 15000 20

SQL> select * from department;

DEPTNO DEPARTMENT LOCATION


---------- -------------------- --------------------
10 accounts Nellore
20 commercial Nellore
30 sales hyderabad
40 marketing hyderabad.

Equi join:

1 select employee.emp_id,employee.ename,employee.job,
2 employee.salary,department.deptno,department.department from
employee,department
3* where employee.deptno = department.deptno
SQL> /

EMP_ID ENAME JOB SALARY DEPTNO


-------------------- -------------------- --------------- ---------- ----------
DEPARTMENT
--------------------
E001 MANJEERABHARGAVI.V clerk 9000 10
accounts

E002 MAMJANSHAWALI.SK teller 9000 20


commercial

E003 K.NIRMALA manager 15000 10


accounts

EMP_ID ENAME JOB SALARY DEPTNO


-------------------- -------------------- --------------- ---------- ----------

3
DEPARTMENT
--------------------
E004 CH.GOPI accountant 15000 20
Commercial

Non Equi Join


SQL> select department.deptno,department.location from employee
,department
2 where
3 employee.deptno<>department.deptno
4 ;

DEPTNO LOCATION
---------- --------------------

30 hyderabad
40 hyderabad
2 rows selected.

Outer join:
SQL> select employee.emp_id,employee.ename ,employee.job
,department.deptno,department.location from
where
employee.deptno(+)=department.deptno
/

EMP_ID ENAME JOB DEPTNO


-------------------- -------------------- --------------- ----------
LOCATION
--------------------
E001 MANJEERABHARGAVI.V clerk 10
Nellore

4
E003 K.NIRMALA manager 10
Nellore

E002 MAMJANSHAWALI.SK teller 20


Nellore

EMP_ID ENAME JOB DEPTNO


-------------------- -------------------- --------------- ----------
LOCATION
--------------------
E004 CH.GOPI accountant 20
Nellore

30
hyderabad

40
hyderabad

6 rows selected.

Self join:

SQL> select a.emp_id,a.ename,a.job,a.salary,b.emp_id,b.department


2 from employee a,employee b
3 where a.emp_id = b.emp_id;

EMP_ID ENAME JOB SALARY


-------------------- -------------------- --------------- ----------
EMP_ID DEPARTMENT
-------------------- --------------------
E002 MAMJANSHAWALI.SK teller 9000
E001 accounts

E003 K.NIRMALA manager 15000


E001 accounts

5
E004 CH.GOPI accountant 15000
E001 accounts

EMP_ID ENAME JOB SALARY


-------------------- -------------------- --------------- ----------
EMP_ID DEPARTMENT
-------------------- --------------------
E001 MANJEERABHARGAVI.V clerk 9000
E002 commercial

E003 K.NIRMALA manager 15000


E002 commercial

E004 CH.GOPI accountant 15000


E002 commercial

EMP_ID ENAME JOB SALARY


-------------------- -------------------- --------------- ----------
EMP_ID DEPARTMENT
-------------------- --------------------
E001 MANJEERABHARGAVI.V clerk 9000
E003 accounts

E002 MAMJANSHAWALI.SK teller 9000


E003 accounts

E004 CH.GOPI accountant 15000


E003 accounts

EMP_ID ENAME JOB SALARY


-------------------- -------------------- --------------- ----------
EMP_ID DEPARTMENT
-------------------- --------------------
E001 MANJEERABHARGAVI.V clerk 9000
E004 commercial

E002 MAMJANSHAWALI.SK teller 9000

6
E004 commercial

E003 K.NIRMALA manager 15000


E004 commercial

12 rows selected.

SQL>

7
Practical -3) create a view to the student table “student_master_view”
and

a)insert records into the view


b)delete records from the view
c)update records of the view

SQL> create table student


2 (stno number(10) primary key,
3 stname varchar2(20),
4 joindate date,
5 course varchar2(15),
6 course_fee number(8,2),
7 no_of_installments number(4),
8 prev_percentage number(5,2),
9 section varchar2(6),
10 remarks varchar2(15),
11 lib_card_id varchar2(15),
12 no_of_books number(3),
13 book_issued varchar2(3));

Table created.

View Creation
SQL> ed
Wrote file afiedt.buf

1 create view student_master_view


2 as
3* select stno,stname,course,joindate from student
SQL> /

View created.

SQL> /*Insertion into View*/


1 insert into student_master_view
2 values

8
3* (&stno,'&stname','&course','&joindate')

SQL> /
SQL> select * from student_master_view;

STNO STNAME COURSE JOINDATE


---------- -------------------- --------------- ---------
1001 Rani.k Bsc 12-JUN-08
1002 Rajeswari.M Bcom 12-JUN-08
1003 Praveena.P BCA 12-AUG-08
1004 Vineela.L BA maths 12-JUL-08

SQL>
Updating the View

SQL> update student_master_view


2 set
3 course = 'BA' where stno = 1004;

1 row updated.

SQL> select * from student_master_view;

STNO STNAME COURSE JOINDATE


---------- -------------------- --------------- ---------
1001 Rani.k Bsc 12-JUN-08
1002 Rajeswari.M Bcom 12-JUN-08
1003 Praveena.P BCA 12-AUG-08
1004 Vineela.L BA 12-JUL-08

Deleting data from the View


SQL> delete from student_master_view
2 where stno = &stno;
Enter value for stno: 1001
old 2: where stno = &stno
new 2: where stno = 1001

1 row deleted.

9
SQL> select * from student_master_view;

STNO STNAME COURSE JOINDATE


---------- -------------------- --------------- ---------
1002 Rajeswari.M Bcom 12-JUN-08
1003 Praveena.P BCA 12-AUG-08
1004 Vineela.L BA 12-JUL-08

10
Practical -4. write a PL/SQL code to find the factorial of given number

SQL> ed

1 declare
2 n number(3):=&n;
3 fact number(10):=1;
4 begin
5 dbms_output.put_line('The factorial of the given number is');
6 while(n>0)
7 loop
8 fact:=fact*n;
9 n:=n-1;
10 end loop;
11 dbms_output.put_line(fact);
12* end;
SQL> /
Enter value for n: 5
old 2: n number(3):=&n;
new 2: n number(3):=5;
The factorial of the given number is
120

PL/SQL procedure successfully completed.

11
Practical – 5
Write a pl/sql code to find the biggest of thee numbers

/*Biggest of the three numbers */

declare
num1 number(4);
num2 number(4);
num3 number(4);
bignum number(4);
begin
dbms_output.put_line('Given three numbers are');
num1:=&num1;
num2:=&num2;
num3:=&num3;
if(num1>num2) and (num1>num3) then
bignum:=num1;
elsif(num2>num3) and (num2>num1) then
bignum:=num2;
else
bignum:=num3;
end if;
dbms_output.put_line('The biggest number is'||bignum);
end;
/
SQL> set serveroutput on;
SQL> /
Enter value for num1: 65
old 8: num1:=&num1;
new 8: num1:=65;
Enter value for num2: 67
old 9: num2:=&num2;
new 9: num2:=67;
Enter value for num3: 54
old 10: num3:=&num3;
new 10: num3:=54;
Given three numbers are
The biggest number is67
PL/SQL procedure successfully completed.

12
Practical – 6
Write a pl/sql code to find whether the given number is amstrong number or
not.

13
SQL> declare
2 num number(5);
3
4 rem number(5);
5 s number(5);
6 num1 number(5);
7 begin
8 num:=&num;
9 num1:=num;
10 while(num>0)
11 loop
12 rem:=mod(num,10);
13 s:=s+power(rem,3);
14 num:=trunc(num/10);
15 end loop;
16 if(s=num1) then
17 dbms_output.put_line('Given number is amstrong');
18 else
19 dbms_output.put_line('Given number is not amstrong');
20 end if;
21 end;
22 /
Enter value for num: 153
old 8: num:=&num;
new 8: num:=153;
Given number is amstrong number
PL/SQL procedure successfully completed.

Practical – 7
Write a pl/sql program to find the sum of the digits of a number

14
SQL> ed
Wrote file afiedt.buf

1 declare
2 num number(5);
3 rem number(5);
4 sm number(5):=0;
5 begin
6 num:=&num;
7 while(num>0)
8 loop
9 rem:=mod(num,10);
10 sm:=sm+rem;
11 num:=trunc(num/10);
12 end loop;
13 dbms_output.put_line('Sum of the digits of the number'||num|| 'is'||
sm);
14* end;
SQL> /
Enter value for num: 3456
old 6: num:=&num;
new 6: num:=3456;
Sum of the digits of the number 3456 is 18

PL/SQL procedure successfully completed.


SQL>

Practical – 8

15
Write a pl/sql program to check whether the given string is palindrome or
not

SQL> ed
Wrote file afiedt.buf

1 declare
2 given_string varchar2(20);
3 rev_string varchar2(20);
4 len number(5);
5 begin
6 given_string:='&given_string';
7 len:=length(given_string);
8 while(len>0)
9 loop
10 rev_string:= rev_string||substr(given_string,len,1);
11 len:=len-1;
12 end loop;
13 dbms_output.put_line('reverse of the given string is'||rev_string);
14 if(given_string=rev_string)
15 then
16 dbms_output.put_line('Given string is palindrome');
17 else
18 dbms_output.put_line('Given string is not palindrome');
19 end if;
20* end;
SQL> /
Enter value for given_string: madam
old 6: given_string:='&given_string';
new 6: given_string:='madam';
reverse of the given string is madam
Given string is palindrome
PL/SQL procedure successfully completed.

Practical - 9
Write a pl/sql program to find the fibonacci series of the given number

16
SQL> declare
2 num number(5);
3 f1 number(5):=0;
4 f2 number(5):=1;
5 f3 number(5);
6 i number(5):=3;
7 begin
8 num:=&num;
9 dbms_output.put_line('The fibonacci series');
10 dbms_output.put_line(f1);
11 dbms_output.put_line(f2);
12 while(i<=num)
13 loop
14 f3:=f1+f2;
15 dbms_output.put_line(f3);
16 f1:=f2;
17 f2:=f3;
18 i:=i+1;
19 end loop;
20 end;
21 /
Enter value for num: 6
old 8: num:=&num;
new 8: num:=6;

PL/SQL procedure successfully completed.


SQL> /
Enter value for num: 17
old 8: num:=&num;
new 8: num:=17;
The fibonacci series
0
1
1
2
3
5
8
13
21

17
34
55
89
144
233
377
610
987

PL/SQL procedure successfully completed.

SQL>

Practical –10
Write a PL/SQL block to evaluate HRA, IncomeTax , Gross salary,Net
Salary based on the Basic .

18
DA is 40% of basic
HRA is 20% of basic
IT is 10% of basic
Gross salary = Basic + HRA+DA
Net Salary = Gross-IT.

SQL> ed
Wrote file afiedt.buf

1 declare
2 cursor cr_emp is
3 select * from emp_pay;
4 samp_da emp_pay.da%type;
5 samp_hra emp_pay.hra%type;
6 samp_tax emp_pay.tax%type;
7 samp_net emp_pay.net%type;
8 samp_gross emp_pay.gross%type;
9 pay_rec emp_pay%rowtype;
10 begin
11 open cr_emp;
12 loop
13 fetch cr_emp into pay_rec;
14 exit when cr_emp%notfound;
15 samp_da := 40*pay_rec.basic/100;
16 samp_hra := 20*pay_rec.basic/100;
17 samp_tax := 10*pay_rec.basic/100;
18 samp_gross := samp_da+samp_hra;
19 samp_net:=samp_net-samp_tax;
20 update emp_pay
21 set da=samp_da,hra=samp_hra,tax=samp_tax,
22 gross=samp_gross,net=samp_net;
23 end loop;
24* end;
25 /

PL/SQL procedure successfully completed.

SQL> set serveroutput on;


SQL> /

19
PL/SQL procedure successfully completed.

SQL> select * from emp_pay;

EMPNO ENAME BASIC DA HRA TAX


---------- -------------------- ---------- ---------- ---------- ----------
NET GROSS
---------- ----------
1 Vijaya.B 6000 2000 1000 500
3000

2 Prasad.V 9000 2000 1000 500


3000

3 Praveen.G 8000 2000 1000 500


3000
`

EMPNO ENAME BASIC DA HRA TAX


---------- -------------------- ---------- ---------- ---------- ----------
NET GROSS
---------- ----------
4 Padmaja.D 8000 2000 1000 500
3000

5 Usha.P 5000 2000 1000 500


3000

Practical – 11

20
An HRD manager decided to raise the salaries of all employees of
department number 20 by 0.5% . Write a PL/SQL code using cursors to
perform this increment in sample_emp table.
declare
cursor crsr_emp is select empno,salary from sample_emp
where deptcode=20;
sample_eno sample_emp.empno%type;
sample_sal sample_emp.salary%type;
begin
open crsr_emp;
if crsr_emp%isopen then
loop
fetch crsr_emp into sample_eno,sample_s
exit when crsr_emp%notfound;
if crsr_emp%found then
update sample_emp
set salary = sample_sal+(sample_sal*0.5)
where empno = sample_eno;
end if;
end loop;
commit;
else
dbms_output.put_line('unable to open cursor');
end if;
close crsr_emp;

PL/SQL procedure successfully completed.

SQL> select * from sample_emp;

EMPNO ENAME DEPTCODE SALARY


---------- -------------------- ---------- ----------
1001 manjeera 20 15000
1002 shawali 20 15000
1003 nirmala 10 12000
1004 Gopi 10 12000

21
Practical –12
Write a pl/sql code to generate electricity bill using the following table

Name null? Type


Meterno number (6)
Cname varchar2 (20)
City varchar2(20)
Curr_read number(6)
Prev_read number(6)
No_units number(6)
Amount number(8,2)
Ser_tax number(8,2)
Net_amt number(9,2)

1 declare
2 cursor elec is select * from elec_bill;
3 mcu number;
4 mrate number;
5 totcharge number;
6 erec elec%rowtype;
7 begin
8 open elec;
9 loop
10 fetch elec into erec;
11 exit when elec%notfound;
12 mcu:=erec.cur_read-erec.prev_read;
13 if erec.category = 'domestic' then
14 mrate:=3.00;
15 elsif(erec.category = 'agriculture')then
16 mrate:=5.00;
17 elsif(erec.category = 'industrial')then
18 mrate:=7.00;
19 end if;
20 totcharge:=mcu*mrate;
21 update elec_bill

22
22 set no_of_units = mcu, rate= mrate,total_amt = totcharge
23 where meterno = erec.meterno;
24 end loop;
25 close elec;
26* end;
SQL> /

PL/SQL procedure successfully completed.

SQL> select * from elec_bill;

METERNO CNAME CUR_READ PREV_READ


NO_OF_UNITS
---------- -------------------- ---------- ---------- -----------
CATEGORY RATE TOTAL_AMT
--------------- ---------- ----------
1001 ch.anitha 560 200 360
domestic 3 1080

1002 p.bhaskarprasad 1800 900 900


agriculture 5 4500

1003 mgbrothers 4000 2000 2000


industrial 7 14000

METERNO CNAME CUR_READ PREV_READ


NO_OF_UNITS
---------- -------------------- ---------- ---------- -----------
CATEGORY RATE TOTAL_AMT
--------------- ---------- ----------
1004 y.rajeswari 400 200 200
domestic 3 600

SQL>

Practical - 13
Create a trigger on the Employee table so that the data should not be
manipulated on the Sundays

23
SQL> ed
Wrote file afiedt.buf

1 create or replace trigger emp_trig after insert or delete or update


2 on sample_emp
3 declare
4 d1 varchar2(20);
5 begin
6 d1:=rtrim(to_char(sysdate,'day'));
7 if(d1='sunday)then
8 raise_application_error(-20046,'User defined trigger Dont
manipulate on sundays');
9 end if;
10* end;
SQL> /

Trigger created.

SQL> insert into sample_emp values(101,'Kumar',40,2000);


ERROR at line 1:
ORA-20046: User defined trigger Dont manipulate on sundays
ORA-06512: at "ABHIRAM.EMP_TRIG", line 6
ORA-04088: error during execution of trigger 'ABHIRAM.EMP_TRIG'

Practical - 14

24
Create a user defined function to check whether the given number is prime
or not

create or replace function primecheck(num number) return varchar2 is


count number(5):=0;
num1 number(5);
i number(5);
begin
num1:=num;
for i in 2..num1-1
loop
if mod(num1,i)=0 then
count1:= count1+1;
end if;
end loop;
if(count=0) then
return 'prime';
else
return 'not prime';
end if;
end;
/

SQL> select primecheck(7) from dual;


Prime(7)
Prime

25
26

You might also like