Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

5.

Student Information Using PL/SQL


SQL> create table student5(rollno number(5) primary key,name varchar2(20),oracle number(3),brm
number(3),accounts number(3),gbe number(3),total number(3),average number(6,2),result varchar2(5));

Table created.

SQL> insert into student5 values(100,’ raja’, 70, 89, 73,90,0,’’);

SQL> insert into student5 values(101,’ abishek’, 60,95,90,50,0,’’);

SQL> insert into student5 values(102,’ karthi’,60, 42,65,55,0,’’);

SQL> insert into student5 values(103,’arun’, 90, 60,98, 83,0,’’);

PL/SQL Block

begin

update student5 set total=oracle+brm+accounts+gbe;

update student5 set average=total/4;

update student5 set result=case when oracle>=50 and brm>=50 and accounts>=50 and gbe>=50 then 'pass' else
'fail' end;

end;

SQL> set serveroutput on

PL/SQL procedure successfully completed.

SQL> select * from student5;

ROLLNO NAME ORACLE BRM ACCOUNTS GBE TOTAL AVERAGE RESULT

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

100 raja 70 89 73 90 322 80.5 pass

101 abishek 60 95 90 50 295 73.75 pass

102 karthi 60 42 65 55 222 55.5 fail

103 arun 90 60 98 83 331 82.75 pass

104 rani 95 85 75 65 320 80 pass


6. Electricity Bill
SQL> set serveroutput on
declare
conno number(3);
name varchar2(30);
p number(4);
c number(4);
nounit number(4);
amount number(7,2);
begin
conno := &consumerno;
name := '&name';
p := &previous;
c := &current;
nounit := c - p;
if nounit <= 100 then
amount := nounit * 1;
elsif nounit > 100 and nounit <= 200 then
amount := 100 * 1 + (nounit-100) * 2;
elsif nounit > 200 then
amount := 100 * 1 + 200 * 2 + (nounit-200) * 3;
end if;
dbms_output.put_line(' ELECTRICITY BILL ');
dbms_output.put_line(' ---------------- ');
dbms_output.put_line(' consumer Number : ' || conno);
dbms_output.put_line(' Name : ' || name);
dbms_output.put_line(' Previous Reading : ' || p);
dbms_output.put_line(' Current Reading : ' || c);
dbms_output.put_line(' Number of Unit : ' || nounit);
dbms_output.put_line(' Amount : ' || amount);
end;
PL/SQL procedure successfully completed.
Enter value for consumerno: 100
old 9: conno := &consumerno;
new 9: conno := 100;
Enter value for name: padma
old 10: name := '&name';
new 10: name := 'padma';
Enter value for previous: 500
old 11: p := &previous;
new 11: p := 500;
Enter value for current: 640
old 12: c := &current;
new 12: c := 640;

ELECTRICITY BILL
----------------
consumer Number : 100
Name : padma
Previous Reading : 500
Current Reading : 640
Number of Unit : 140
Amount : 180

Ex 7: Splitting Table
SQL> create table sristudent(rollno number(3),name varchar2(30),m1 number(3), m2 number(3), m3
number(3), total number(3),result varchar2(6));
Table created.
SQL> desc sristudent;
Name Null? Type
----------------------------------------- -------- --------------------
ROLLNO NUMBER(3)
NAME VARCHAR2(30)
M1 NUMBER(3)
M2 NUMBER(3)
M3 NUMBER(3)
TOTAL NUMBER(3)
RESULT VARCHAR2(6)
SQL> create table studentpass(rollno number(3),name varchar2(30),m1 number(3), m2 number(3), m3
number(3), total number(3),result varchar2(6));
Table created.
SQL> create table studentfail(rollno number(3),name varchar2(30),m1 number(3), m2 number(3), m3
number(3), total number(3),result varchar2(6));
Table created.
SQL> insert into sristudent values(100,’kavitha’,90,97,56,0,’’);

SQL> insert into sristudent values(101,’rani’,35,87,66,0,’’);

SQL> insert into sristudent values(102,’ramya’,45,80,60,0,’’);

SQL> insert into sristudent values(103,’ramya’,55,72,32,0,’’);

SQL> insert into sristudent values(104,’raja’,65,22,92,0,’’);

SQL> select * from sristudent;


ROLLNO NAME M1 M2 M3 TOTAL RESULT
---------- ------------------------ ---------- ---------- -------------- ----------- -----------
100 kavitha 90 97 56 243 pass
101 rani 35 87 66 188 fail
102 ramya 45 80 60 185 pass
103 ramya 55 72 32 159 fail
104 raja 65 22 92 179 fail
105 gowtham 65 82 94 241 pass
106 gowri 85 62 84 231 pass
PL/SQL
declare
cursor c1 is select * from sristudent for update of result,total;
begin
for s in c1 loop
s.total:=s.m1+s.m2+s.m3;
if s.m1 >= 40 and s.m2 >= 40 and s.m3 >=40 then
s.result:='pass';
insert into studentpass values(s.rollno,s.name,s.m1,s.m2,s.m3,s.total,s.result);
else
s.result:='fail';
insert into studentfail values(s.rollno,s.name,s.m1,s.m2,s.m3,s.total,s.result);
end if;
update sristudent set total=s.total,result=s.result where current of c1;
end loop;
end;
SQL> set serveroutput on
SQL>/
PL/SQL procedure successfully completed.
SQL> select * from studentpass;
ROLLNO NAME M1 M2 M3 TOTAL RESULT
---------- ------------------------ ---------- ---------- -------------- ----------- -----------
100 kavitha 90 97 56 243 pass
102 ramya 45 80 60 185 pass
105 gowtham 65 82 94 241 pass
106 gowri 85 62 84 231 pass
SQL> select * from studentfail;
ROLLNO NAME M1 M2 M3 TOTAL RESULT
---------- ------------------------ ---------- ---------- -------------- ----------- -----------
101 rani 35 87 66 188 fail
103 ramya 55 72 32 159 fail
104 raja 65 22 92 179 fail

Ex 8 : Joining Table
SQL> create table studentmark(rollno number(3) primary key,name varchar2(15),total number(3));
Table created.
SQL> desc studentmark;
Name Null? Type
----------------------------------------- -------- ----------------------------
ROLLNO NOT NULL NUMBER(3)
NAME VARCHAR2(15)
TOTAL NUMBER(3)
SQL> create table personal(rollno number(3) references studentmark(rollno),address varchar2(40));
Table created.
SQL> desc personal;
Name Null? Type
------------------------------ -------- --------------------------
ROLLNO NUMBER(3)
ADDRESS VARCHAR2(40)
SQL> create table studentdetail(rollno number(3),name varchar2(15),total number(3),address varchar2(40));
Table created.
SQL> desc studentdetail;
Name Null? Type
----------------------------------------- -------- ------------------------
ROLLNO NUMBER(3)
NAME VARCHAR2(15)
TOTAL NUMBER(3)
ADDRESS VARCHAR2(40)
SQL> insert into studentmark values(100,'kavi',600);
SQL> insert into studentmark values(200,'kavin',500);
SQL> insert into studentmark values(201,'raji',450);
SQL> insert into studentmark values(202,'ramya',550);
SQL> insert into studentmark values(203,'rani',650);
SQL> insert into personal values(100,'coimbatore');
SQL> insert into personal values(200,'erode');
SQL> insert into personal values(201,'salem');
SQL> insert into personal values(202,'pollachi');
SQL> insert into personal values(203,'madurai');
SQL> select * from studentmark;
ROLLNO NAME TOTAL
---------- --------------- ----------
100 kavi 600
200 kavin 500
201 raji 450
202 ramya 550
203 rani 650
SQL> select * from personal;
ROLLNO ADDRESS
---------- - ---------------------------------------
100 coimbatore
200 erode
201 salem
202 pollachi
203 madurai
SQL> declare
cursor c1 is select studentmark.rollno,studentmark.name,studentmark.total,personal.address from
studentmark,personal where studentmark.rollno = personal.rollno;
begin
for s in c1 loop
insert into studentdetail values(s.rollno,s.name,s.total,s.address);
end loop;
end;

PL/SQL procedure successfully completed.


SQL> select * from studentdetail;
ROLLNO NAME TOTAL ADDRESS
---------- --------------- ---------- ----------------------------------------
100 kavi 600 coimbatore
200 kavin 500 erode
201 raji 450 salem
202 ramya 550 pollachi
203 rani 650 madurai

Ex 9(a) : Factorial Using Recursion


Creating function fact
create or replace function fact(n number) return number is
begin
if n=1 then
return 1;
else
return n*fact(n-1);
end if;
end fact;

SQL> set serveroutput on


SQL>/
Function Created
Calling function fact

declare
n number(4) := 5;
f number(8);
begin
f := fact(n);
dbms_output.put_line('Factorial = '||f);
end;
Enter value for n: 5

old 2: n number(4) := &n;


new 2: n number(4) := 5;
Factorial = 120
PL/SQL procedure successfully completed.

Ex 9(b) : Fibonacci Series Using Recursion


Creating function Fibonacci
create or replace function fibo(n number) return number is
begin
if n=0 then
return 0;
elsif n=1 then
return 1;
else
return fibo(n-1)+fibo(n-2);
end if;
end fibo;
/
SQL> set serveroutput on
SQL>/
Function Created
Calling function Fibonacci

declare
n number(4) := &n;
f number(8);
begin
dbms_output.put_line('Fibonacci Series');
for i in 0..n-1 loop
f := fibo(i);
dbms_output.put_line(f);
end loop;
end;
/

SQL> /
Enter value for n: 8
old 2: n number(4) := &n;
new 2: n number(4) := 8;
Fibonacci Series
0
1
1
2
3
5
8
13
PL/SQL procedure successfully completed.
Ex.10 Database Trigger

Creating Table Employee

SQl> create table emps(eid number(3),ename varchar2(30),hiredate date);

Creating Trigger

create or replace trigger emp_trigger before insert on emps for each row

begin

:new.ename:=UPPER(:new.ename);

:new.hiredate:=SYSDATE;

end;

Trigger Created

SQL> insert into emps(eid,ename) values(1,'ram');

1 row created.

SQL> insert into emps(eid,ename) values(10,'devi');

1 row created.

SQL> insert into emps(eid,ename) values(11,'kavitha');

1 row created.

SQL> select * from emps;

EID ENAME HIREDATE

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

1 RAM 07-MAR-19

10 DEVI 07-MAR-19

11 KAVITHA 07-MAR-19

3 rows selected.

You might also like