0% found this document useful (0 votes)
41 views45 pages

PL SQL prgs-1

Uploaded by

Divya.k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views45 pages

PL SQL prgs-1

Uploaded by

Divya.k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 45

1.

DDL COMMANDS

SQL> Connect System/System

Connected.

SQL> create table empdet(Ename varchar(10),emp_ID number(5),gender


char(1),DOB date,phone_no number(10));

Table created.

SQL> DESC empdet

Name Null? Type

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

ENAME VARCHAR2(10)

EMP_ID NUMBER(5)

GENDER CHAR(1)

DOB DATE

PHONE_NO NUMBER(10)

SQL> alter table empdet add(experience number(2));

Table altered.

SQL> DESC empdet

Name Null? Type

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

ENAME VARCHAR2(10)

EMP_ID NUMBER(5)

GENDER CHAR(1)

DOB DATE

PHONE_NO NUMBER(10)
EXPERIENCE NUMBER(2)

SQL> alter table empdet modify(emp_id varchar2(5));

Table altered.

SQL> DESC empdet

Name Null? Type

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

ENAME VARCHAR2(10)

EMP_ID VARCHAR2(5)

GENDER CHAR(1)

DOB DATE

PHONE_NO NUMBER(10)

EXPERIENCE NUMBER(2)

SQL> alter table empdet modify(emp_id varchar2(10));

Table altered.

SQL> DESC empdet

Name Null? Type

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

ENAME VARCHAR2(10)

EMP_ID VARCHAR2(10)

GENDER CHAR(1)

DOB DATE

PHONE_NO NUMBER(10)

EXPERIENCE NUMBER(2)

SQL> alter table empdet modify(emp_id varchar2(7));

Table altered.
SQL> DESC empdet

Name Null? Type

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

ENAME VARCHAR2(10)

EMP_ID VARCHAR2(7)

GENDER CHAR(1)

DOB DATE

PHONE_NO NUMBER(10)

EXPERIENCE NUMBER(2)

SQL> alter table empdet drop(dob);

Table altered.

SQL> DESC empdet

Name Null? Type

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

ENAME VARCHAR2(10)

EMP_ID VARCHAR2(7)

GENDER CHAR(1)

PHONE_NO NUMBER(10)

EXPERIENCE NUMBER(2)

SQL> drop table empdet;

Table dropped.

SQL> DESC empdet

ERROR:

ORA-04043: object empdet does not exist


5.CHARACTER FUNCTIONS

SQL> Connect System/System


Connected.
SQL> create table dept(cht varchar(10));

Table created.

SQL> insert into dept values('naruto');

1 row created.

SQL> insert into dept values('baruto');

1 row created.

SQL> insert into dept values('itachi');

1 row created.

SQL> insert into dept values('kakshi');

1 row created.

SQL> insert into dept values('sauske');

1 row created.

SQL> insert into dept values('minato');

1 row created.

SQL> select * from dept;

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;

CHT C ASCII(CHT) LENGTH(CHT) LOWER(CHT) UPPER(CHT) LPAD(CHT,


RPAD(CHT,
---------- - ---------- ----------- ---------- ---------- ---------
---------
naruto - 110 6 naruto NARUTO ###naruto
naruto$$$
baruto - 98 6 baruto BARUTO ###baruto
baruto$$$
itachi - 105 6 itachi ITACHI ###itachi
itachi$$$
kakshi - 107 6 kakshi KAKSHI ###kakshi
kakshi$$$
sauske - 115 6 sauske SAUSKE ###sauske
sauske$$$
minato - 109 6 minato MINATO ###minato
minato$$$

6 rows selected.

SQL> insert into dept values(' hinata');

1 row created.

SQL> insert into dept values('hinata ');

1 row created.

SQL> select
cht,ltrim(cht),rtrim(cht),replace(cht,'ar','od'),translate(cht,'ar','o
d') from dept;

CHT LTRIM(CHT) RTRIM(CHT) REPLACE(CHT,'AR','OD TRANSLATE(


---------- ---------- ---------- -------------------- ----------
naruto naruto naruto noduto noduto
baruto baruto baruto boduto boduto
itachi itachi itachi itachi itochi
kakshi kakshi kakshi kakshi kokshi
sauske sauske sauske sauske souske
minato minato minato minato minoto
hinata hinata hinata hinata hinoto
hinata hinata hinata hinata hinoto

8 rows selected.
5.DATE FUNCTIONS

SQL> create table dat(ddt date);

Table created.

SQL> insert into dat values('22-jul-76');

1 row created.

SQL> insert into dat values('12-dec-99');

1 row created.

SQL> insert into dat values('01-jan-02');

1 row created.

SQL> insert into dat values('29-aug-16');

1 row created.

SQL> insert into dat values('17-feb-20');

1 row created.

SQL> insert into dat values('30-apr-23');

1 row created.

SQL> select ddt,add_months(ddt,10)added,months_between(ddt,'23-jun-


90')diff from dat;

DDT ADDED DIFF


--------- --------- ----------
22-JUL-76 22-MAY-77 -167.03226
12-DEC-99 12-OCT-00 113.645161
01-JAN-02 01-NOV-02 138.290323
29-AUG-16 29-JUN-17 314.193548
17-FEB-20 17-DEC-20 355.806452
30-APR-23 29-FEB-24 394.225806

6 rows selected.
SQL> select ddt,next_day(ddt,'sunday')weekend,last_day(ddt)diff from
dat;

DDT WEEKEND DIFF


--------- --------- ---------
22-JUL-76 25-JUL-76 31-JUL-76
12-DEC-99 19-DEC-99 31-DEC-99
01-JAN-02 06-JAN-02 31-JAN-02
29-AUG-16 04-SEP-16 31-AUG-16
17-FEB-20 23-FEB-20 29-FEB-20
30-APR-23 07-MAY-23 30-APR-23

6 rows selected.

SQL> select ddt,next_day(ddt,'sunday')weekend,last_day(ddt)diff from


dat;

DDT WEEKEND DIFF


--------- --------- ---------
22-JUL-76 25-JUL-76 31-JUL-76
12-DEC-99 19-DEC-99 31-DEC-99
01-JAN-02 06-JAN-02 31-JAN-02
29-AUG-16 04-SEP-16 31-AUG-16
17-FEB-20 23-FEB-20 29-FEB-20
30-APR-23 07-MAY-23 30-APR-23

6 rows selected.

SQL> select
ddt,round(ddt,'year')roundyear,round(ddt,'month')roundmonth,round(ddt,
'day')roundday from dat;

DDT ROUNDYEAR ROUNDMONT ROUNDDAY


--------- --------- --------- ---------
22-JUL-76 01-JAN-77 01-AUG-76 25-JUL-76
12-DEC-99 01-JAN-00 01-DEC-99 12-DEC-99
01-JAN-02 01-JAN-02 01-JAN-02 30-DEC-01
29-AUG-16 01-JAN-17 01-SEP-16 28-AUG-16
17-FEB-20 01-JAN-20 01-MAR-20 16-FEB-20
30-APR-23 01-JAN-23 01-MAY-23 30-APR-23

6 rows selected.

SQL> select
ddt,trunc(ddt,'year')truncyear,trunc(ddt,'month')truncmonth,trunc(ddt,
'day')truncday from dat;

DDT TRUNCYEAR TRUNCMONT TRUNCDAY


--------- --------- --------- ---------
22-JUL-76 01-JAN-76 01-JUL-76 18-JUL-76
12-DEC-99 01-JAN-99 01-DEC-99 12-DEC-99
01-JAN-02 01-JAN-02 01-JAN-02 30-DEC-01
29-AUG-16 01-JAN-16 01-AUG-16 28-AUG-16
17-FEB-20 01-JAN-20 01-FEB-20 16-FEB-20
30-APR-23 01-JAN-23 01-APR-23 30-APR-23

6 rows selected.

SQL> select ddt,greatest(ddt,'12-nov-17')biggest,least(ddt,'15-sep-


18')smallest from dat;

DDT BIGGEST SMALLEST


--------- --------- ---------
22-JUL-76 12-NOV-17 22-JUL-76
12-DEC-99 12-NOV-17 12-DEC-99
01-JAN-02 12-NOV-17 01-JAN-02
29-AUG-16 12-NOV-17 29-AUG-16
17-FEB-20 17-FEB-20 15-SEP-18
30-APR-23 30-APR-23 15-SEP-18

6 rows selected.

2.DML COMMANDS

SQL> connect system/system

Connected.
SQL> create table Lib (BookId char(5),BName varchar(20),RDate
date,Pages number(4),Price number(6,2));

Table created.

SQL> desc Lib

Name Null? Type

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

BOOKID CHAR(5)

BNAME VARCHAR2(20)

RDATE DATE

PAGES NUMBER(4)

PRICE NUMBER(6,2)

SQL> Insert into Lib Values('LB101','HarryPotter','12-sep-


1982',2250,650.55);

1 row created.

SQL> select * from Lib;

BOOKI BNAME RDATE PAGES PRICE

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

LB101 HarryPotter 12-SEP-82 2250 650.55

SQL> Insert into Lib (BookID,BName,Pages)


values('LB102','SherLockHomes',500);

1 row created.

SQL> select * from Lib;

BOOKI BNAME RDATE PAGES PRICE

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

LB101 HarryPotter 12-SEP-82 2250 650.55

LB102 SherLockHomes 500

SQL> Insert into Lib


Values('&BookID','&BName','&Rdate',&Pages,&Price);
Enter value for bookid: LB103

Enter value for bname: Othello

Enter value for rdate: 11-Nov-1902

Enter value for pages: 855

Enter value for price: 750.50

old 1: Insert into Lib


Values('&BookID','&BName','&Rdate',&Pages,&Price)

new 1: Insert into Lib Values('LB103','Othello','11-Nov-


1902',855,750.50)

1 row created.

SQL> select * from Lib;

BOOKI BNAME RDATE PAGES PRICE

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

LB101 HarryPotter 12-SEP-82 2250 650.55

LB102 SherLockHomes 500

LB103 Othello 11-NOV-02 855 750.5

SQL> Insert into Lib


(BookID,BName,Rdate,Price)values('&BookID','&Bookname','&Rdate','&pric
e');

Enter value for bookid: LB104

Enter value for bookname: naruto

Enter value for rdate: 11-may-2002

Enter value for price: 955.60

old 1: Insert into Lib


(BookID,BName,Rdate,Price)values('&BookID','&Bookname','&Rdate','&pric
e')

new 1: Insert into Lib


(BookID,BName,Rdate,Price)values('LB104','naruto','11-may-
2002','955.60')

1 row created.
SQL> select * from Lib;

BOOKI BNAME RDATE PAGES PRICE

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

LB101 HarryPotter 12-SEP-82 2250 650.55

LB102 SherLockHomes 500

LB103 Othello 11-NOV-02 855 750.5

LB104 naruto 11-MAY-02 955.6

SQL> Insert into Lib values('LB105','Boruto',null,1055,958.34);

1 row created.

SQL> select * from Lib;

BOOKI BNAME RDATE PAGES PRICE

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

LB101 HarryPotter 12-SEP-82 2250 650.55

LB102 SherLockHomes 500

LB103 Othello 11-NOV-02 855 750.5

LB104 naruto 11-MAY-02 955.6

LB105 Boruto 1055 958.34

SQL> update Lib set pages=9634 where bookID='LB104';

1 row updated.

SQL> select * from Lib;

BOOKI BNAME RDATE PAGES PRICE

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

LB101 HarryPotter 12-SEP-82 2250 650.55

LB102 SherLockHomes 500

LB103 Othello 11-NOV-02 855 750.5

LB104 naruto 11-MAY-02 9634 955.6


LB105 Boruto 1055 958.34

SQL> delete from lib where bookID='LB105';

1 row deleted.

SQL> select * from Lib;

BOOKI BNAME RDATE PAGES PRICE

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

LB101 HarryPotter 12-SEP-82 2250 650.55

LB102 SherLockHomes 500

LB103 Othello 11-NOV-02 855 750.5

LB104 naruto 11-MAY-02 9634 955.6

SQL> delete from Lib;

4 rows deleted.

SQL> select * from Lib;

no rows selected

7.GROUP FUNCTIONS

SQL> create table svth(rollno number(6),name varchar2(15),dept


varchar2(9),course varchar(9));

Table created.

SQL> select * from svth;

ROLLNO NAME DEPT COURSE


---------- --------------- --------- ---------
210 mukesh d1 B.c0m
210 vedaprakash d2 B.sc
210 vedaprakash d2 B.sc
212903 gokul d3 B.ca
212902 krishna d2 B.sc
212904 sharath d1 B.com
212917 mani d3 B.ca
212999 kannan d2 B.sc
212922 yash d1 B.com
212921 divesh d3 B.ca
212907 karthee d2 B.sc

11 rows selected.

SQL> select sum(rollno), avg(rollno) from svth;

SUM(ROLLNO) AVG(ROLLNO)
----------- -----------
1704005 154909.545

SQL> select max(name),max(rollno), max(dept), max(course)from svth;

MAX(NAME) MAX(ROLLNO) MAX(DEPT) MAX(COURS


--------------- ----------- --------- ---------
yash 212999 d3 B.sc

SQL> select min(name),min(rollno), min(dept), min(course)from svth;

MIN(NAME) MIN(ROLLNO) MIN(DEPT) MIN(COURS


--------------- ----------- --------- ---------
divesh 210 d1 B.c0m

SQL> select count(*) from svth;

COUNT(*)
----------
11

SQL> select distinct rollno,name,dept,course from svth;

ROLLNO NAME DEPT COURSE


---------- --------------- --------- ---------
210 mukesh d1 B.c0m
212902 krishna d2 B.sc
212904 sharath d1 B.com
212999 kannan d2 B.sc
212922 yash d1 B.com
212921 divesh d3 B.ca
212917 mani d3 B.ca
212907 karthee d2 B.sc
212903 gokul d3 B.ca
210 vedaprakash d2 B.sc

10 rows selected.
SQL> select stddev(rollno),median(rollno) from svth;

STDDEV(ROLLNO) MEDIAN(ROLLNO)
-------------- --------------
99357.5857 212904

SQL> select dept, max(rollno), max(name), max(course),count(*) from


svth group by dept having count(*)>2;

DEPT MAX(ROLLNO) MAX(NAME) MAX(COURS COUNT(*)


--------- ----------- --------------- --------- ----------
d3 212921 mani B.ca 3
d1 212922 yash B.com 3
d2 212999 vedaprakash B.sc 5

8.SET FUNCTIONS

SQL> create table ved1(sno number(10),name varchar2(15), doj date,dept


varchar2(12));

Table created.
SQL> create table ved2(sno number(10),name varchar2(15), doj date,dept
varchar2(12));

Table created.

SQL> select * from ved1;

SNO NAME DOJ DEPT


---------- --------------- --------- ------------
1001 veda 11-APR-02 d1
1021 prakash 11-SEP-22 d2
1008 mukeesh 18-MAY-18 d3
1009 sharath 01-JAN-13 d1
1011 bala 11-NOV-10 d2
1003 devi 03-AUG-05 d3

6 rows selected.

SQL> select * from ved2;

SNO NAME DOJ DEPT


---------- --------------- --------- ------------
1001 veda 11-APR-02 d1
1009 sharath 01-JAN-13 d1
1008 mukeesh 18-MAY-18 d3
1003 devi 03-AUG-05 d3
1012 gokul 07-FEB-07 d2
1002 krishna 10-JUN-07 d2

6 rows selected.

SQL> select * from ved1 union select * from ved2;

SNO NAME DOJ DEPT


---------- --------------- --------- ------------
1001 veda 11-APR-02 d1
1002 krishna 10-JUN-07 d2
1003 devi 03-AUG-05 d3
1008 mukeesh 18-MAY-18 d3
1009 sharath 01-JAN-13 d1
1011 bala 11-NOV-10 d2
1012 gokul 07-FEB-07 d2
1021 prakash 11-SEP-22 d2

8 rows selected.

SQL> select * from ved1 union all select * from ved2;


SNO NAME DOJ DEPT
---------- --------------- --------- ------------
1001 veda 11-APR-02 d1
1021 prakash 11-SEP-22 d2
1008 mukeesh 18-MAY-18 d3
1009 sharath 01-JAN-13 d1
1011 bala 11-NOV-10 d2
1003 devi 03-AUG-05 d3
1001 veda 11-APR-02 d1
1009 sharath 01-JAN-13 d1
1008 mukeesh 18-MAY-18 d3
1003 devi 03-AUG-05 d3
1012 gokul 07-FEB-07 d2

SNO NAME DOJ DEPT


---------- --------------- --------- ------------
1002 krishna 10-JUN-07 d2

12 rows selected.

SQL> select * from ved1 intersect select * from ved2;

SNO NAME DOJ DEPT


---------- --------------- --------- ------------
1001 veda 11-APR-02 d1
1003 devi 03-AUG-05 d3
1008 mukeesh 18-MAY-18 d3
1009 sharath 01-JAN-13 d1

SQL> select * from ved1 minus select * from ved2;

SNO NAME DOJ DEPT


---------- --------------- --------- ------------
1011 bala 11-NOV-10 d2
1021 prakash 11-SEP-22 d2

SQL> select * from ved2 minus select * from ved1;

SNO NAME DOJ DEPT


---------- --------------- --------- ------------
1002 krishna 10-JUN-07 d2
1012 gokul 07-FEB-07 d2

SQL> select to_number('15000') from dual;

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.

SQL> select to_date('11-11-2003','dd/mm/yyyy') from dual;

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

PL/SQL procedure successfully completed.

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

PL/SQL procedure successfully completed.


SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark3.sql;
Enter value for no: -23
old 5: no:=&no;
new 5: no:=-23;
I predict the numeric symbols...your input number is :
negative number

PL/SQL procedure successfully completed.

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)

PL/SQL procedure successfully completed.

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

PL/SQL procedure successfully completed.

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

PL/SQL procedure successfully completed.

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

PL/SQL procedure successfully completed.


FOR LOOP:
Declare
a number:=0;
b number:=1;
c number;
n number;
i number;
Begin
n:=&n;
dbms_output.put_line('Fibonnaci series....');
dbms_output.put_line(a);
dbms_output.put_line(b);
for i in 1..n-2
Loop
c:=a+b;
dbms_output.put_line(c);
a:=b;
b:=c;
End loop;
End;
/

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

PL/SQL procedure successfully completed.

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;

ENO NAME SALARY INCENTIVE


---------- --------------- ---------- ----------
1 veda 10000
2 keer 5000
3 muku 4000
4 gay 3000

SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark8.sql;
Enter value for empno: 3
old 6: no:=&empno;
new 6: no:=3;

PL/SQL procedure successfully completed.


SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark8.sql;
Enter value for empno: 1
old 6: no:=&empno;
new 6: no:=1;

PL/SQL procedure successfully completed.

SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark8.sql;
Enter value for empno: 2
old 6: no:=&empno;
new 6: no:=2;

PL/SQL procedure successfully completed.

SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark8.sql;
Enter value for empno: 4
old 6: no:=&empno;
new 6: no:=4;

PL/SQL procedure successfully completed.

SQL> select * from stud1;

ENO NAME SALARY INCENTIVE


---------- --------------- ---------- ----------
1 veda 10000 500
2 keer 5000 250
3 muku 4000 200
4 gay 3000 150

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:

SQL> select * from student;

SNO NAME M1 M2 M3 TOTAL


AVERAGE
---------- ---------- ---------- ---------- ---------- ----------
----------
RESULT
----------
1 veda 100 100 100

2 keer 90 90 90

3 muku 80 80 80

SNO NAME M1 M2 M3 TOTAL


AVERAGE
---------- ---------- ---------- ---------- ---------- ----------
----------
RESULT
----------
4 gay 60 60 70
SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark9.sql;

PL/SQL procedure successfully completed.

SQL> select * from student;

SNO NAME M1 M2 M3 TOTAL AVERAGE


---------- ---------- ---------- ---------- ---------- ----------
----------
RESULT
----------
1 veda 100 100 100 300 100
Pass

2 keer 90 90 90 270 90
Pass

3 muku 80 80 80 240 80
Pass

SNO NAME M1 M2 M3 TOTAL


AVERAGE
---------- ---------- ---------- ---------- ---------- ----------
----------
RESULT
----------
4 gay 60 60 70 190
63
Pass
CREATION OF FUNCTION:
Create function Fun_Fact(n number) return number is f number;
Begin
f:=1;
for i in 1..n
loop
f:=f*i;
end loop;
return f;
End;
/

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

PL/SQL procedure successfully completed.

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;

ENO ENAME SAL


---------- ---------- ----------
1 veda 50000
2 keer 40000
3 mukeshwar 45000
SQL> @C:\Users\Vedaprakash\OneDrive\Desktop\mark12.sql;
Enter value for eno: 1
old 10: vno:=&eno;
new 10: vno:=1;
Enter value for increment: 2000
old 15: incr:=&increment;
new 15: incr:=2000;
salary updated by user...!!!

PL/SQL procedure successfully completed.

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...!!!

PL/SQL procedure successfully completed.

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...!!!

PL/SQL procedure successfully completed.

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

PL/SQL procedure successfully completed.

SQL> select * from emp3;

ENO ENAME SAL


---------- ---------- ----------
1 veda 52000
2 keer 41000
3 mukeshwar 46500
TRIGGERS:
Create or replace trigger mark122 before insert or update on emp1 for
each row when(new.sal<300)
Begin
Raise_application_error(-20107,'Salary must Be Above 300');
End;
/

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.

SQL> select * from emp1;

ENO ENAME SAL


---------- ---------- ----------
1 prakash 5000
2 keer 5500
3 mukeshwar 4500
SQL> insert into emp1 values(3,'mukeshwar',250);
insert into emp1 values(3,'mukeshwar',250)
*
ERROR at line 1:
ORA-20107: Salary must Be Above 300
ORA-06512: at "SYSTEM.MARK122", line 2
ORA-04088: error during execution of trigger 'SYSTEM.MARK122'

SQL> delete from emp1 where eno=1;


1 row deleted.
SQL> select * from emp2;
ENO ENAME SAL
---------- ---------- ----------
1 prakash 5000

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:

PL/SQL procedure successfully completed.

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!

PL/SQL procedure successfully completed.

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

PL/SQL procedure successfully completed.

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

PL/SQL procedure successfully completed.

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;

RNO NAME DEPT MARK BRANC


---------- ---------- -------- ---------- -----
201 divi cs 487 d1
203 keer eco 491 d1

SQL> insert into sam1 values (204,'gayu','cs',495,'d1');


1 row created.
SQL> insert into sam1 (rno,name,branch) values (205,'mukesh','d1');
1 row created.
SQL> select* from sam1;
RNO NAME DEPT MARK BRANC
---------- ---------- -------- ---------- -----
201 divi cs 487 d1
203 keer eco 491 d1
204 gayu cs 495 d1
205 mukesh d1
SQL> select * from stud4;
RNO NAME DEPT MARK BRANC
---------- ---------- -------- ---------- -----
201 divi cs 487 d1
202 veda general 446 d2
203 keer eco 491 d1
204 gayu cs 495 d1
205 mukesh d1

SQL> insert into stud4 values(206,'vijay','general',432,'d1');


1 row created.
SQL> update sam1 set dept = 'eng', mark=437 where name ='veda';
0 rows updated.
SQL> select*from sam1;
RNO NAME DEPT MARK BRANC
---------- ---------- -------- ---------- -----
201 divi cs 487 d1
203 keer eco 491 d1
204 gayu cs 495 d1
205 mukesh d1
206 vijay general 432 d1

SQL> delete from sam1 where rno=203;


1 row deleted.
SQL> select * from stud4
RNO NAME DEPT MARK BRANC
---------- ---------- -------- ---------- -----
201 divi cs 487 d1
202 veda general 446 d2
204 gayu cs 495 d1
205 mukesh d1
206 vijay general 432 d1

SQL> create view sam2 as select rno-200


newrno,name,dept,branch,mark*2 nmark,mark/5 percent from stud4;
View created.
SQL> select * from sam2;
NEWRNO NAME DEPT BRANC NMARK PERCENT
---------- ---------- -------- ----- ---------- ----------
1 divi cs d1 974 97.4
2 veda general d2 892 89.2
4 gayu cs d1 990 99
5 mukesh d1
6 vijay general d1 864 86.4

SQL> create view sam3 as select branch,count(*) count,sum (nmark)


totmark from sam2 group by branch;
View created.

SQL> select * from sam3


BRANC COUNT TOTMARK
----- ---------- ----------
d1 4 2828
d2 1 892

SQL> create view sam4 as select rno,sam2.name,mark,nmark,percent from


sam1,sam2 where sam1.branch = sam2.branch;
View created.

SQL> select * from sam4;


RNO NAME MARK NMARK PERCENT
---------- ---------- ---------- ---------- ----------
206 divi 432 974 97.4
205 divi 974 97.4
204 divi 495 974 97.4
201 divi 487 974 97.4
206 gayu 432 990 99
205 gayu 990 99
204 gayu 495 990 99
201 gayu 487 990 99
206 mukesh 432
205 mukesh
204 mukesh 495

RNO NAME MARK NMARK PERCENT


---------- ---------- ---------- ---------- ----------
201 mukesh 487
206 vijay 432 864 86.4
205 vijay 864 86.4
204 vijay 495 864 86.4
201 vijay 487 864 86.4

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> insert into stud4 values(stud5.nextval,'siva','tam',450,'d2');


1 row created.
SQL> insert into stud4 values(211,'vasu','eng',456,'d2');
1 row created.
SQL> select stud5.nextval from dual;
NEXTVAL
----------
215

SQL> insert into stud4 values(stud5.nextval,'ram','general',422,'d2');


1 row created.

SQL> select * from stud4;


RNO NAME DEPT MARK BRANC
---------- ---------- -------- ---------- -----
201 divi cs 487 d1
202 veda general 446 d2
204 gayu cs 495 d1
205 mukesh d1
206 vijay general 432 d1
210 siva tam 450 d2
211 vasu eng 456 d2
220 ram general 422 d2
8 rows selected.

SQL> create sequence samp1 start with 1 increment by 6 maxvalue 20


minvalue 1 cache 2 cycle;
Sequence created.

SQL> select samp1.nextval from dual;


NEXTVAL
----------
1

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> select samp1.nextval from dual;


NEXTVAL
----------
19

SQL> 1
1* select samp1.nextval from dual
SQL> select samp1.nextval from dual;

NEXTVAL
----------
1

SQL> create table syn(sno number(2),name varchar(8));


Table created.
SQL> create synonym dsyn for syn
Synonym created.
SQL> insert into dsyn values (30,'mouse');
1 row created.
SQL> select * from dsyn;
SNO NAME
---------- --------
30 mouse

SQL> select * from dsyn;


SNO NAME
---------- --------
30 mouse

SQL> insert into dsyn values (10,'laptop');


1 row created.

SQL> insert into dsyn values (20,'keyboard');


1 row created.

SQL> select * from dsyn;


SNO NAME
---------- --------
30 mouse
10 laptop
20 keyboard

SQL> update dsyn set name='monitor' where sno = 20;


1 row updated.
SQL> delete from dsyn where sno = 30;
1 row deleted.
SQL> select * from syn;

SNO NAME
---------- --------
10 laptop
20 monitor

You might also like