Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 44

PLSQL

Practical Journal

SUBMITTED BY

Vinam Gosar
Roll No: 2211711
TYBSC CS (HONS)

Department of Computer Science


Somaiya Vidyavihar University
S K Somaiya College
INDEX
Prac No. Practical Sign
1 1.1 Wap to print hello world in plsql
1.2 Wap to perform addition of 2 numbers in plsql
1.3 Wap to perform arithmetic operations on 2 numbers in plsql
2 2.1 Wap to check if the number ‘a’ is less than 20 in plsql
2.2 Wap to check if the number ‘a’ is less than 20 using if else
2.3 Wap to check if the number ‘a’ is less than 20 using if elseif then
2.4 Wap to calculate commission of an employee that depends on sales values. If
the sales is more than 200000 then commission is 70%. If sales is less than 200000
and greater than 100000 then commission is 50%. If sales is less than 100000 and
greater than 50000 then commission is 30% else give 10% commission to the
employee.
2.5 Wap to check if the given number is odd or even in plsql
3 Create table acct_master with attributes account-no, cust_id, balanace, Address
and insert 5 records. Write a plsql code to check the balance of a particular account
no if it is less than minimum balance i.e 5000 then deduct Rs 100 as fine.
4 4.1 Wap to display numbers from 1 to 20 using loop in plsql
4.2 Wap to display even numbers from 1 to 20 using while loop in plsql
4.3 Wap to calculate area of circle where radius varies from 2 to 7
4.4 Wap to calculate area of circle where radius varies from 2 to 7 using for loop
5 5.1 Create a table circle with attributes area and radius and then wap to calculate
area of circle where radius varies from 2 to 7 and then insert the values into the
table.
5.2 Wap to create a multiplication table
5.3 Wap to print the numbers from 100 to 51.
5.4 Wap to print the reverse of a number.
5.5 Wap to show the working of goto statement.
6 6.1 Use switch case statement to identify grades of a student
6.2 Use switch case statement to calculate commission on sales.
7 Create procedures for the following:
7.1 printing hello world
7.2 multiplication of 2 numbers
7.3 finding the maximum of 2 numbers
7.4 factorial of a number
7.5 finding the square of a number
7.6 drop procedure
8 Create functions for the following:
8.1 to find the maximum of the 2 numbers.
8.2 to calculate average salary of all employees from the employee table.
8.3 drop function
9 Create sequence for the following:
9.1 to create a student table and increment the id values by 1, start with 1,
maxvalue is 10, minvalue is 1.
9.2 to create a supplier table and increment the id values by 1, start with
1, maxvalue is 99999, increment by 1 and should cache upto 20 values for
performance.
9.3 drop sequence
10 Create trigger for the following:
10.1 To not allow the user to insert any values into the employee table.
10.2 To not allow the user to insert values if the id>100.
10.3 To not allow the user to drop any table from the database.
10.4 Drop trigger.
11 11.1conect Create a Table Employee (ID, Name, Address, Contact No, Salary and
Location) and Insert 5 records.
11.2 Update Salary of Employee whose ID is 103.
11.3 Delete The Employee Whose Stays in Nahur.
11.4 Alter table & column salary of an employee.
11.5 Drop table Emp1.
Practical 1
1.1 Wap to print hello world in
plsql. CODE:
Declare
a char(50);
Begin
a:=’hello world’;
dbms_output.put_line(a);
End;
/

OUTPUT:

1.2 Wap to perform addition of 2 numbers in plsql.


CODE:
Declare
a number(2):=10;
b number(2):=10;
c number(2):=10;
Begin
c:=a+b;
dbms_output.put_line(‘Addition is ’|| c);
End;
/

OUTPUT:
1.3 Wap to perform arithmetic operations on 2 numbers in plsql.
CODE:
Declare
a integer:=25;
b integer:=20;
c integer;
Begin
c:=a+b;
dbms_output.put_line(‘Addition is ’||c);
c:=a-b;
dbms_output.put_line(‘Subtraction is ’||c);
c:=a*b;
dbms_output.put_line(‘Multiplication is ’||c);
c:=a/b;
dbms_output.put_line(‘Division is ’||c);
c:=a%b;
dbms_output.put_line(‘Mod is ’||c);
End;
/

OUTPUT:
Practical 2
2.1 Wap to check if the number ‘a’ is less than 20 in plsql
CODE:
Declare
a number(2):=10;
Begin
if(a<20) then
dbms_output.put_line(‘value of a is less than 20’);
end if;
dbms_output.put_line(‘actual value of a is ’||a);
End;
/

OUTPUT:

2.2 Wap to check if the number ‘a’ is less than 20 using if else
CODE:
Declare
a number(2):=10;
Begin
if(a<20) then
dbms_output.put_line(‘value of a is less than 20’);
else
dbms_output.put_line(‘value of a is greater than 20’);
end if;
dbms_output.put_line(‘actual value of a is ’||a);
End;
/
OUTPUT:

2.3 Wap to check if the number ‘a’ is less than 20 using if elseif then
CODE:
Declare
a number(3):=100;
Begin
if (a=10) then
dbms_output.put_line(‘value of a is ’||a);
elsif (a=20) then
dbms_output.put_line(‘value of a is ’||a);
elsif (a=30) then
dbms_output.put_line(‘value of a is ’||a);
else
dbms_output.put_line(‘none of the values are matching’);
End;
/

OUTPUT:
2.4 Wap to calculate commission of an employee that depends on sales
values. If the sales is more than 200000 then commission is 70%. If sales is
less than 200000 and greater than 100000 then commission is 50%. If sales is
less than 100000 and greater than 50000 then commission is 30% else give
10% commission to the employee.

CODE:
Declare
sales number:=120000;
commission number;
Begin
if (sales>200000) then
commission:=sales*0.7;
elsif (sales<200000 and sales>100000) then
commission:=sales*0.5;
elsif (sales<100000 and sales>50000) then
commission:=sales*0.3;
else
commission:=sales*0.1;
end if;
dbms_output.put_line(‘The commission is ’||commission);
End;
/

OUTPUT:
2.5 Wap to check if the given number is odd or even in plsql.

CODE:
Declare
a number:=7;
Begin
if(a mod 2=0) then
dbms_output.put_line(a ||’ is even’);
else
dbms_output.put_line(a ||’ is odd’);
end if;
End;
/

OUTPUT:
Practical 3
Create table acct_master with attributes account-no, cust_id, balanace,
Address and insert 5 records. Write a plsql code to check the balance of a
particular account no if it is less than minimum balance i.e 5000 then deduct
Rs 100 as fine.

CODE:
Table creation-
create table acct_master(cust_id int, acc_no int, balance int, address varchar(50));

Insert Query-
Insert into acct_master values(001,2211715,9000,’Navi Mumbai’);
Insert into acct_master values(002,2211716,5000,’Marol’);
Insert into acct_master values(003,2211717,4500,’Kandivali’);
Insert into acct_master values(004,2211718,4000,’Andheri’);
Insert into acct_master values(005,2211719,8000,’Sakinaka’);
Insert into acct_master values(006,2211720,6000,’Kandivli’);
PLSQL CODE-
Declare
curr_balance number;
myacct_no number;
fine number:=150;
min_balance constant number(6,2):=5000.00;
Begin
myacct_no:=&myacct_no;
select balance into curr_balance from acct_master where
acct_no=myacct_no;
if curr_balance<min_balance then
update acct_master set balance = balance-fine where
acct_no=myacct_no;
end if;
dbms_output.put_line(‘account balance successfully updated’);
End;
/
Practical 4
4.1 Wap to display numbers from 1 to 20 using loop in plsql
CODE:
Declare
a number;
Begin
for a in 1..20
loop
dbms_output.put_line(a);
end loop;
End;
/

OUTPUT:
4.2 Wap to display even numbers from 1 to 20 using while loop in plsql
CODE:
Declare
a number;
Begin
a:=1;
while (a<=20) loop
if (a mod 2=0) then
dbms_output.put_line(a);
end if;
a:=a+1;
End loop;
End;
/

OUTPUT:
4.3 Wap to calculate area of circle where radius varies from 2 to 7
CODE:
Declare
r number;
pi number:=3.14;
area number;
Begin
r:=1;
While (r<=7) loop
area:=pi*r*r;
dbms_output.put_line(‘area is ’||area);
r:=r+1;
End loop;
End;
/

OUTPUT:
4.4 Wap to calculate area of circle where radius varies from 2 to 7 using for
loop
CODE:
Declare
a number;
pi number:=3.14;
r number;
Begin
For r in 2..7
Loop
a:=pi*r*r;
dbms_output.put_line(‘area is ’||a);
End loop;
End;
/

OUTPUT:
Practical 5
5.1 Create a table circle with attributes area and radius and then wap to
calculate area of circle where radius varies from 2 to 7 and then insert the
values into the table.
CODE:
Create table circle(area float, radius int);

Declare
a number;
pi number:=3.14;
r number;
Begin
FOR r in 2..7
LOOP
a:=pi*r*r;
insert into circle values(a,r);
END LOOP;
Dbms_output.put_line(‘values inserted successfully’);
END;
/
Select * from circle;

OUTPUT:
5.2 Wap to create a multiplication table
CODE:
Declare
Constant number:=10;
Multiplier number;
Answer number;
Begin
FOR multiplier in 1..10
LOOP
Answer:=constant*multiplier; Dbms_output.put_line(constant||’x’||
multiplier||’=’||answer);
END LOOP
End;
/

OUTPUT:
5.3 Wap to print the numbers from 100 to 51.
CODE:
Declare
a number;
Begin
FOR a in reverse 51..100
LOOP
Dbms_output.put_line(a);
END LOOP;
END;
/

OUTPUT:
5.4 Wap to print the reverse of a
number. CODE:
Declare
num integer;
rem integer;
rev integer;
Begin
rev:=0;
num:=&num;
WHILE(num>0) LOOP
rem:=num mod 10;
rev:=rev*10=rem;
num:=num/10;
END LOOP;
dbms_output.put_line(‘The reverse is ’||rev);
END;
/

OUTPUT:
5.5 Wap to show the working of goto
statement. CODE:
Declare
a number:=11;
Begin
<<loop_start>>
WHILE (a<=20) LOOP
IF (a=15) THEN
a:=a+1;
dbms_output.put_line(‘I am ignoring the value here’);
goto loop_start;
END IF;
dbms_output.put_line(a);
a:=a+1;
END LOOP;
END;
/

OUTPUT:
Practical 6
6.1 Use switch case statement to identify grades of a student.
CODE:
Declare
Grade char(1):=’A’;
Begin
CASE grade when ‘A’ then
dbms_output.put_line(‘Excellent’);
when ‘B’ then
dbms_output.put_line(‘Very good’);
when ‘C’ then
dbms_output.put_line(‘Good’);
when ‘D’ then
dbms_output.put_line(‘You passed);
when ‘F’ then
dbms_output.put_line(‘Failed, noob’);
else
dbms_output.put_line(‘Enter valid grade’);
END CASE;
End;
/

OUTPUT:
6.2 Use switch case statement to calculate commission on sales.
CODE:
Declare
Sales number(6):=120000;
Commission number;
Begin
CASE when (sales>200000) then
Commission:=sales*0.7;
When (sales<200000 and sales>100000) then
Commission:=sales*0.5;
When (sales<100000 and sales>50000) then
Commission:=sales*0.3;
Else
Commission:=sales*0.1;
END CASE;
Dbms_output.put_line(‘The commission is ’||commission);
END;
/

OUTPUT:
Practical 7
Create procedures to for the following:
7.1 printing hello world
CODE:
Create or Replace procedure greetings AS
Begin
dbms_output.put_line('Hello World');
End;
/

Begin
greetings;
End;
/
OUTPUT:
7.2 multiplication of 2
numbers CODE:
Create or Replace procedure multiplication(x in number, y in number, z out
number) AS
Begin
z:=x-y;
End;
/

Declare
x number(10);
y number(10);
z number(10);
Begin
x:=&x;
y:=&y;
multiplication(x,y,z);
dbms_output.put_line('multiplication is '11z);
End;
/

OUTPUT:
7.3 finding the maximum of 2
numbers CODE:
Create or Replace procedure maximum(x in number, y in number)AS
Begin
if(x>y)then
dbms_output.put_line('The maximum number is '||x);
else
dbms_output.put_line('The maximum number is '||y);
end if;
End maximum;
/

Declare
x number(10);
y number(10);
Begin
x:=&x;
y:=&y;
maximum(x,y);
End;
/
OUTPUT:
7.4 factorial of a
number CODE:
Declare
x number:=6;
y number;
procedure factorial(x in number, y out number)AS
Begin
y:=1;
for i in 1..x
loop
y:=y*i;
end loop;
dbms_output.put_line('factorial of ‘||x||’ is ‘||y);
End;
Begin
factorial(x,y);
End;
/

OUTPUT:
7.5 finding the square of a
number CODE:
Declare
x number(10);
y number(10);
procedure square(x in number, y out number)AS
Begin
y:=x*x;
dbms_output.put_line('The square of the given number is ‘||y);
End;
Begin
x:=&x;
square(x,y);
End;
/

OUTPUT:
7.6 drop procedure
CODE:
Drop procedure maximum;

OUTPUT:
Practical 8
Create functions for the following:
8.1 To find the maximum of the 2 numbers.
CODE:
Declare
a number;
b
number; c
number;
Function findmax(x in number, y in number)
Return number IS
z number;
Begin
if x>y then
z:=x;
else
z:=y;
end if;
return z;
End;
Begin
a:=23;
b:=45;
c:=findmax(a,b);
dbms_output.put_line(‘Maximum of(23,45) is ‘||c);
End;
/

OUTPUT:
8.2 To calculate average salary of all employees from the employee table.
CODE:
Create table employee(emp_id int, emp_name varchar(20), location varchar(20),
salary int);

Insert into employee values(1,’employee1’,’Mumbai’,20000);


Insert into employee values(2,’employee2’,’Navi Mumbai’,60000);
Insert into employee values(3,’employee3’,’Panvel’,90000);

Create or replace function avgsalary


RETURN number is
Total number:=0;
Begin
Select avg(salary) into total from employee;
Return Total;
End;
/

Declare
sal number;
Begin
sal:=avgsalary();
dbms_output.put_line(‘The avg salary is ||sal’);
End;
/

OUTPUT:
8.3 Drop function
CODE:
Drop function avgsalary;

OUTPUT:
Practical 9
9.1 To create a student table and increment the id values by 1, start with 1,
maxvalue is 10, minvalue is 1.

CODE:
Create table student (id int, stud_name varchar(20), class varchar(20), address
varchar(20));

Create sequence studs


Increment by 1
Start with 1
Maxvalue 10
Minvalue 1
Nocycle
Order;

Insert into student values(studs.nextval,’student1’,’tycs’,’chembur’);


Insert into student values(studs.nextval,’student2,’tybaf’,’nerul’);
Insert into student values(studs.nextval,’student3’,’tycs’,’sanpada’);
Insert into student values(studs.nextval,’student4’,’tycs’,’andheri’);
Insert into student values(studs.nextval,’student5’,’tycs’,’marol’);

Select * from students;

OUTPUT:
9.2 To create a supplier table and increment the id values by 1, start with 1,
maxvalue is 99999, increment by 1 and should cache upto 20 values for
performance.

CODE:
Create table supplier(sup_id, name varchar(20));

Create sequence supplier_sequence


Start with 1
Minvalue 1
Maxvalue 99999
Increment by 1
Cache 20;

Insert into supplier values(supplier_sequence.nextval, ‘supplier1’);


Insert into supplier values(supplier_sequence.nextval, ‘supplier2’);
Insert into supplier values(supplier_sequence.nextval, ‘supplier3’);
Insert into supplier values(supplier_sequence.nextval, ‘supplier4’);
Insert into supplier values(supplier_sequence.nextval, ‘supplier5’);

Select * from supplier;

OUTPUT:
9.3 drop the sequence
CODE:
Drop sequence supplier_sequence;

OUTPUT:
Practical 10
10.1 To not allow the user to insert any values into the employee table.

CODE:
Create or replace trigger insert1
Before insert on employee
Begin
Raise_application_error(-20000,’sorry you cannot insert any values’);
End;
/

Insert into employee values(4,’employee4’,’Kharghar’,30000);

OUTPUT:
10.2 To not allow the user to insert values if the id>100.
CODE:
Create or replace trigger insert2
Before insert on employee
For each row
When(new.id>100)
Begin
Raise_application_error(-20001,’Sorry you cannot insert values here’);
End;
/

Insert into employee values(101,’employee101’,’Vidyavihar’,40000);

OUTPUT:
10.3 To not allow the user to drop any table from the
database. CODE:

Create or replace trigger drop1


Before drop on schema
Begin
Raise_application_error(‘-20002’,’You are not allowed to drop’);
End;
/

Drop table employee;

OUTPUT:
10.4 To drop a
trigger. CODE:
Drop trigger drop1

OUTPUT:

Practical 11
11.1conect Create a Table Employee (ID, Name, Address, Contact No, Salary
and Location) and Insert 5 records.
Code:
Create Table Emp (Id int, Name varchar (20), Salary number, Location varchar
(20), Contact_No int);

insert into Emp values(101,'Vinam',10000,'Thane',9819130004);


insert into Emp values(102,'Subin',20000,'Mulund',9781354242);
insert into Emp values(103,'Deep',30000,'Nahur',9781354546);
insert into Emp values(104,'Veer',40000,'Mumbai',9781355464);
insert into Emp values(105,'Ram',50000,'Navi Mumbai',9781355323);

Output:
11.2 Update Salary Of Employee whose ID is 103.
Code:
Update Emp1
Set salary=salary+(salary * 0.1)
where id=101;
Output:

11.3 Delete The Employee Whose Stays in Nahur.


Code:
Delete from Emp1
where location='Nahur';

Output:
11.4 Alter table & column salary of an employee.
Code:
Alter table Emp1
add dept char(20);

insert into Emp1 (Dept) values (CS');


insert into Emp1 (Dept) values ('IT');
Output:

11.5 Drop table Emp1.


Code:
Drop Table Emp;
Output:

You might also like