PL SQL prgs-1
PL SQL prgs-1
DDL COMMANDS
Connected.
Table created.
----------------------------------------- --------
----------------------------
ENAME VARCHAR2(10)
EMP_ID NUMBER(5)
GENDER CHAR(1)
DOB DATE
PHONE_NO NUMBER(10)
Table altered.
----------------------------------------- --------
----------------------------
ENAME VARCHAR2(10)
EMP_ID NUMBER(5)
GENDER CHAR(1)
DOB DATE
PHONE_NO NUMBER(10)
EXPERIENCE NUMBER(2)
Table altered.
----------------------------------------- --------
----------------------------
ENAME VARCHAR2(10)
EMP_ID VARCHAR2(5)
GENDER CHAR(1)
DOB DATE
PHONE_NO NUMBER(10)
EXPERIENCE NUMBER(2)
Table altered.
----------------------------------------- --------
----------------------------
ENAME VARCHAR2(10)
EMP_ID VARCHAR2(10)
GENDER CHAR(1)
DOB DATE
PHONE_NO NUMBER(10)
EXPERIENCE NUMBER(2)
Table altered.
SQL> DESC empdet
----------------------------------------- --------
----------------------------
ENAME VARCHAR2(10)
EMP_ID VARCHAR2(7)
GENDER CHAR(1)
DOB DATE
PHONE_NO NUMBER(10)
EXPERIENCE NUMBER(2)
Table altered.
----------------------------------------- --------
----------------------------
ENAME VARCHAR2(10)
EMP_ID VARCHAR2(7)
GENDER CHAR(1)
PHONE_NO NUMBER(10)
EXPERIENCE NUMBER(2)
Table dropped.
ERROR:
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
CHT
----------
naruto
baruto
itachi
kakshi
sauske
minato
6 rows selected.
SQL> select
cht,chr(45),ascii(cht),length(cht),lower(cht),upper(cht),lpad(cht,9,'#
'),rpad(cht,9,'$') from dept;
6 rows selected.
1 row created.
1 row created.
SQL> select
cht,ltrim(cht),rtrim(cht),replace(cht,'ar','od'),translate(cht,'ar','o
d') from dept;
8 rows selected.
5.DATE FUNCTIONS
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
6 rows selected.
SQL> select ddt,next_day(ddt,'sunday')weekend,last_day(ddt)diff from
dat;
6 rows selected.
6 rows selected.
SQL> select
ddt,round(ddt,'year')roundyear,round(ddt,'month')roundmonth,round(ddt,
'day')roundday from dat;
6 rows selected.
SQL> select
ddt,trunc(ddt,'year')truncyear,trunc(ddt,'month')truncmonth,trunc(ddt,
'day')truncday from dat;
6 rows selected.
6 rows selected.
2.DML COMMANDS
Connected.
SQL> create table Lib (BookId char(5),BName varchar(20),RDate
date,Pages number(4),Price number(6,2));
Table created.
----------------------------------------- --------
----------------------------
BOOKID CHAR(5)
BNAME VARCHAR2(20)
RDATE DATE
PAGES NUMBER(4)
PRICE NUMBER(6,2)
1 row created.
1 row created.
1 row created.
1 row created.
SQL> select * from Lib;
1 row created.
1 row updated.
1 row deleted.
4 rows deleted.
no rows selected
7.GROUP FUNCTIONS
Table created.
11 rows selected.
SUM(ROLLNO) AVG(ROLLNO)
----------- -----------
1704005 154909.545
COUNT(*)
----------
11
10 rows selected.
SQL> select stddev(rollno),median(rollno) from svth;
STDDEV(ROLLNO) MEDIAN(ROLLNO)
-------------- --------------
99357.5857 212904
8.SET FUNCTIONS
Table created.
SQL> create table ved2(sno number(10),name varchar2(15), doj date,dept
varchar2(12));
Table created.
6 rows selected.
6 rows selected.
8 rows selected.
12 rows selected.
TO_NUMBER('15000')
------------------
15000
SQL> select doj, to_char(doj,'dd/month/yyyy') from ved1;
DOJ TO_CHAR(DOJ,'DD/MONTH/YYYY')
--------- --------------------------------------------
11-APR-02 11/april /2002
11-SEP-22 11/september/2022
18-MAY-18 18/may /2018
01-JAN-13 01/january /2013
11-NOV-10 11/november /2010
03-AUG-05 03/august /2005
6 rows selected.
TO_DATE('
---------
11-NOV-03
PL/SQL
IF :
Declare
catresult number(3);
Begin
catresult:=&catresult;
if catresult>170 then
dbms_output.put_line('Congrats!! You are Qualified for MBA course');
End if;
End;
/
OUTPUT:
SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark1.1.sql;
Enter value for catresult: 180
old 4: catresult:=&catresult;
new 4: catresult:=180;
Congrats!! You are Qualified for MBA course
IF ELSE:
Declare
catresult number(3);
Begin
catresult:=&catresult;
if (catresult>150) then
dbms_output.put_line('Dear students You are qualified for BSC,BCA
course');
Else
dbms_output.put_line('Sorry, you are not selected in this particular
course');
End if;
End;
/
OUTPUT:
SQL> set serveroutput on
SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark2.sql;
Enter value for catresult: 200
old 4: catresult:=&catresult;
new 4: catresult:=200;
Dear students You are qualified for BSC,BCA
course
PL/SQL procedure successfully completed.
IF ELSE-IF :
Declare
no number(2);
Begin
dbms_output.put_line('I predict the numeric symbols...your input
number is : ');
no:=&no;
if(no>0) then
dbms_output.put_line('postive number');
elsif (no<0) then
dbms_output.put_line('negative number');
else
dbms_output.put_line(' number is 0 (neutral number)');
End if;
End;
/
OUTPUT :
SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark3.sql;
Enter value for no: 56
old 5: no:=&no;
new 5: no:=56;
I predict the numeric symbols...your input number is :
postive number
SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark3.sql;
Enter value for no: 0
old 5: no:=&no;
new 5: no:=0;
I predict the numeric symbols...your input number is :
number is 0 (neutral number)
CASE STATEMENT:
Declare
bp number(5):=10;
hra Number(5); da Number(5);
pf Number(5); lic Number(5);
ded Number(5); gross Number(5); net Number(5);
result varchar2(20);
begin
bp:=&Basic_Salary;
dbms_output.put_line('root Pay: '||bp);
case
when bp>=2000 and bp<=5000 then
hra := (1.2 / 100) * bp;
da := (1.3 / 100) * bp;
pf := (1.5 / 100) * bp;
lic := (1.6 / 100) * bp;
When(bp > 5000) And (bp <= 10000) Then
hra := (1.3 / 100) * bp;
da := (1.4 / 100) * bp;
pf := (1.6 / 100) * bp;
lic := (1.7 / 100) * bp;
else
hra := (2.1 / 100) * bp;
da := (2.2 / 100) * bp;
pf := (2.3 / 100) * bp;
lic := (2.4 / 100) * bp;
end Case;
ded := lic + pf;
gross := bp + hra + da;
net:= gross-ded;
dbms_output.put_line('Rebate: '||ded);
dbms_output.put_line('wages: '||gross);
dbms_output.put_line('Net income: '||net);
end;
/
OUTPUT:
SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark4.sql;
Enter value for basic_salary: 20000
old 8: bp:=&Basic_Salary;
new 8: bp:=20000;
root Pay: 20000
Rebate: 940
wages: 20860
Net income: 19920
LOOPING STATEMENTS
Basic loop:
Declare
initial number;
tot number;
tmp number;
Begin
initial:=&initial_no;
tmp:=initial;
tot:=0;
loop
tot:=tot+initial;
initial:=initial-1;
exit when initial=0;
End loop;
dbms_output.put_line('sum of 1st Number '||tmp||' total number ='||
tot);
End;
/
OUTPUT:
SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark5.sql;
Enter value for initial_no: 20
old 6: initial:=&initial_no;
new 6: initial:=20;
sum of 1st Number 20 total number =210
WHILE LOOP:
Declare
n number;
r number;
s number:=0;
Begin
n:=&n;
while(n>0)
Loop
r:=mod(n,10);
s:=s+r;
n:=trunc(n/10,0);
End loop;
dbms_output.put_line('sum of digits you have entered = '||s);
End;
/
OUTPUT:
SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark6.sql;
Enter value for n: 23461389
old 6: n:=&n;
new 6: n:=23461389;
sum of digits you have entered = 36
OUTPUT:
SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark7.sql;
Enter value for n: 9
old 8: n:=&n;
new 8: n:=9;
Fibonnaci series....
0
1
1
2
3
5
8
13
21
IMPICIT CURSOR:
Declare
No stud1.eno%type;
Sal stud1.salary%type;
Ins number(10,2);
Begin
no:=&empno;
select salary into Sal from stud1 where eno=no;
if sal>20000 then
ins:=(10/100)*Sal;
else
ins:=(5/100)*Sal;
End if;
update stud1 set incentive=ins where eno=no;
End;
/
OUTPUT:
SQL> select * from stud1;
SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark8.sql;
Enter value for empno: 3
old 6: no:=&empno;
new 6: no:=3;
SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark8.sql;
Enter value for empno: 2
old 6: no:=&empno;
new 6: no:=2;
SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark8.sql;
Enter value for empno: 4
old 6: no:=&empno;
new 6: no:=4;
EXPLICIT CURSOR:
Declare
cursor a is select sno,m1,m2,m3 from student;
tot student.total%type;
av student.average%type;
res student.result%type;
Begin
for i in a
Loop
tot:=i.m1+i.m2+i.m3; av:=tot/3;
if(i.m1<40 or i.m2<40 or i.m3<40) then
res:='Fail';
else
res:='Pass';
End if;
update student set total=tot,average=av,result=res where sno=i.sno;
End Loop;
End;
/
OUTPUT:
2 keer 90 90 90
3 muku 80 80 80
2 keer 90 90 90 270 90
Pass
3 muku 80 80 80 240 80
Pass
ACCESSING FUNCTION:
Declare
n number:=&n;
y number;
Begin
y:=Fun_Fact(n);
dbms_output.put_line('Factorial of the entered number: ' || n|| ' is=
' ||y);
End;
/
OUTPUT:
SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark10.sql;
Function created.
SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark11.sql;
Enter value for n: 4
old 2: n number:=&n;
new 2: n number:=4;
Factorial of the entered number: 4 is= 24
PROCEDURES:
Declare
vno number;
incr number;
c number;
procedure incrsal( n number, i number) is
Begin
update emp3 set sal=sal+i where eno=n;
End incrsal;
Begin
vno:=&eno;
select count(*) into c from emp3 where eno=vno;
if c=0 then
dbms_output.put_line('Employee with emp_no ' || vno ||' not found the
data');
else
incr:=&increment;
incrsal(vno,incr);
dbms_output.put_line('salary updated by user...!!!');
end if;
End;
/
OUTPUT:
SQL> select * from emp3;
SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark12.sql;
Enter value for eno: 2
old 10: vno:=&eno;
new 10: vno:=2;
Enter value for increment: 1000
old 15: incr:=&increment;
new 15: incr:=1000;
salary updated by user...!!!
SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark12.sql;
Enter value for eno: 3
old 10: vno:=&eno;
new 10: vno:=3;
Enter value for increment: 1500
old 15: incr:=&increment;
new 15: incr:=1500;
salary updated by user...!!!
SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark12.sql;
Enter value for eno: 4
old 10: vno:=&eno;
new 10: vno:=4;
Enter value for increment: 4000
old 15: incr:=&increment;
new 15: incr:=4000;
Employee with emp_no 4 not found the data
AFTER:
create or replace trigger triggerdel after delete on emp1 for each row
Begin
insert into emp2 values(:old.eno,:old.ename,:old.sal);
End;
/
OUTPUT:
SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark13.sql;
Trigger created.
SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark14.sql;
Trigger created.
EXECPTIONS:
Declare
cid customers. id %type:=&cid;
cname customers. name % type;
caddr customers. address %type;
ex_invalid_id exception;
Begin
if cid<=0 then
raise ex_invalid_id;
else
select name,address into cname,caddr from customers where id=cid;
dbms_output.put_line('name of the customer: '||cname);
dbms_output.put_line('address: '||caddr);
End if;
Exception
When ex_invalid_id Then
Raise_application_error(-20003,'Id must be greater than zero!');
When no_data_found Then
dbms_output.put_line('no such customers!');
when too_many_Rows Then
Raise_application_error(-20004,'Id must be unique !');
When others Then
dbms_output.put_line('error');
End;
/
OUTPUT:
SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark15.sql;
Enter value for cid: 0
old 2: cid customers. id %type:=&cid;
new 2: cid customers. id %type:=0;
Declare
*
ERROR at line 1:
ORA-20003: Id must be greater than zero!
ORA-06512: at line 16
USER-DEFINED:
SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark15.sql;
Enter value for cid: 4
old 2: cid customers. id %type:=&cid;
new 2: cid customers. id %type:=4;
no such customers!
SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark15.sql;
Enter value for cid: 2
old 2: cid customers. id %type:=&cid;
new 2: cid customers. id %type:=2;
name of the customer: mukeshwar
address: kolathur
SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark15.sql;
Enter value for cid: 3
old 2: cid customers. id %type:=&cid;
new 2: cid customers. id %type:=3;
Declare
*
ERROR at line 1:
ORA-20004: Id must be unique !
ORA-06512: at line 20
SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark15.sql;
Enter value for cid: 2
old 2: cid customers. id %type:=&cid;
new 2: cid customers. id %type:=2;
name of the customer: mukeshwar
address: kolathur
11.DBJ OBJECTS:
SQL> create table stud4(rno number(4),name varchar2(10),dept
varchar2(8),mark number(3),branch varchar(5));
Table created.
SQL> create view sam1 as select * from stud4 where branch = 'd1';
View created.
SQL> select * from sam1;
16 rows selected.
SQL> drop view sam4;
View dropped.
SQL> create sequence stud5 start with 210 increment by 5 maxvalue 250
minvalue 210 cache 2 cycle;
Sequence created.
SQL> 1
1* select samp1.nextval from dual
SQL> select samp1.nextval from dual;
NEXTVAL
----------
7
SQL> 1
1* select samp1.nextval from dual
SQL> select samp1.nextval from dual;
NEXTVAL
----------
13
SQL> 1
1* select samp1.nextval from dual
SQL> select samp1.nextval from dual;
NEXTVAL
----------
1
SNO NAME
---------- --------
10 laptop
20 monitor