Professional Documents
Culture Documents
SQL>
Table created.
1
EMP_ID NOT NULL VARCHAR2(20)
ENAME VARCHAR2(20)
JOB VARCHAR2(15)
DEPARTMENT VARCHAR2(20)
JOINDATE DATE
SALARY NUMBER(10,2)
SQL>
2
E004 CH.GOPI accountant commercial
12-JUL-00 15000 20
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> /
3
DEPARTMENT
--------------------
E004 CH.GOPI accountant 15000 20
Commercial
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
/
4
E003 K.NIRMALA manager 10
Nellore
30
hyderabad
40
hyderabad
6 rows selected.
Self join:
5
E004 CH.GOPI accountant 15000
E001 accounts
6
E004 commercial
12 rows selected.
SQL>
7
Practical -3) create a view to the student table “student_master_view”
and
Table created.
View Creation
SQL> ed
Wrote file afiedt.buf
View created.
8
3* (&stno,'&stname','&course','&joindate')
SQL> /
SQL> select * from student_master_view;
SQL>
Updating the View
1 row updated.
1 row deleted.
9
SQL> select * from student_master_view;
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
11
Practical – 5
Write a pl/sql code to find the biggest of thee 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:=#
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:=#
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:=#
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:=#
new 6: num:=3456;
Sum of the digits of the number 3456 is 18
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:=#
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:=#
new 8: num:=6;
17
34
55
89
144
233
377
610
987
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 /
19
PL/SQL procedure successfully completed.
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;
21
Practical –12
Write a pl/sql code to generate electricity bill using the following table
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> /
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
Trigger created.
Practical - 14
24
Create a user defined function to check whether the given number is prime
or not
25
26