ORACLE Lab Manual
ORACLE Lab Manual
Oracle
INDEX
Sr Definition Page no
no
UNIT : 2
“Managing Tables and Data, Data Control And Transaction Control Command”
UNIT : 3
Other ORACLE Database Objects Concurrency Control Using Lock
1 Perform View. 53
2 Perform Sequence. 55
3 Perform Synonyms. 57
4 Perform Database Link. 58
5 Perform Index. 59
6 Perform cluster. 61
UNIT : 4
Introduction to PL/SQL, Advanced PL/SQL
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 3
Subject : RDBMS Using Oracle
Write a PL/SQL statement to create procedure called p1 which accept a
number
& print multiply by 2 on the screen
33 Procedure (with in out parameter) 97
Write a PL/SQL statement to create procedure which accept no from the
user
and multiply by 3 on the screen. (Use in out parameter)
34 Procedure (with out parameter) 98
Write a PL/SQL statement to create procedure which accept no from the
user and multiply by 3 on the screen. (Use out parameter)
35 Procedure to insert value in table. 99
36 Procedure to update value in table. 100
37 Procedure to delete value in table 101
38 Function 102
Create a function for addition of two values
39 Package 103
-- Create a package to display appropriate message.
40 Create a package for addition and subtraction of two values. 104
41 Trigger 105
--Create a trigger to display salary changes in the customer table.
42 PLSQL TABLES [Nested table] 107
Create a nested table called dept and use it in to query.
43 PLSQL TABLES [Varray] 108
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 4
Subject : RDBMS Using Oracle
Step:1
Click on startAll ProgramsOracle Database10g express editionStart Database
Step 2:
Click on Go To Database Home Page icon on desktop.
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 5
Subject : RDBMS Using Oracle
Step 3:
Give Username : system
& Password : manager and click on login button.
Step 4:
You will get following Screen. Now click on SQL.
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 6
Subject : RDBMS Using Oracle
Step 5:
You will get following Screen. Now, perform Your Queries.
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 7
Subject : RDBMS Using Oracle
UNIT : 2
Managing Tables and Data Control and Transaction Control Command
Exercise-1
Create a table Student which include these fields, Insert records in the table then perform a given
queries:
Create Table:
create table Student
(Roll_No varchar2 (6),
Name varchar2 (20),
Marks number (3));
Output:
Table Created.
Note: Below screen will be display and click on submit button then following output will come
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 8
Subject : RDBMS Using Oracle
Output:
1 row(s) inserted.
5. Delete from Student where the column name holds the value Twinkal
Delete from Student Where name= 'Twinkal';
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 9
Subject : RDBMS Using Oracle
Exercise-2
Create a table Client which include these fields, Insert records in the table then perform a given
queries:
Create Table:
create table Client
(Client_No varchar2 (6),
Name varchar2 (20),
City varchar2 (15),
PinCode number (6),
State varchar2 (15),
BalDue number (10));
Output:
Table Created.
Output:
1 row(s) inserted.
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 10
Subject : RDBMS Using Oracle
3. Retrieve the list of Names, City and the State of all the Clients.
Select Name, City, State from Client;
8. Delete from Client where the column state holds the value ‘TamilNadu’.
Delete from Client Where State = 'TamilNadu';
9. Add a column called ‘Contact_no’ of data type ‘number’ and size = ‘10’ to the Client table.
Alter table Client Add (Contact_no number (10)) ;
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 11
Subject : RDBMS Using Oracle
Exercise-3
Create a table Client_Master which include these fields, Insert records in the table then perform a
given queries:
Create Table:
Output:
Table Created.
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 12
Subject : RDBMS Using Oracle
1. List the name of all Clients having ‘a’ as the second letter in their names.
Select Name from Client_Master where Name like '_a%';
2. List the Client no, Name who stay in a city whose first letter is ‘M’.
Select Client_no, Name from Client_Master where City like 'M%';
5. List the name, city and state of Clients who are not in the state of ‘Maharashtra’.
Select Name, City, State from Client_Master where State not in ('Maharashtra');
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 13
Subject : RDBMS Using Oracle
Exercise-4
Create a table Product_Master which include these fields, Insert records in the table then perform a given
queries.
Create Table:
create table Product_Master
(Product_No varchar2(6) Primary Key,
Description varchar2(15) not null,
ProfitPercent number(4,2) not null,
UnitMeasure varchar2(10) not null,
QtyOnHand number(8) not null,
ReOrderLvl number(8)not null,
SellPrice number(8,2)not null,
CostPrice number(8,2)not null,
Check(Product_No like 'P%'),
Check(SellPrice<>0),
Check(CostPrice<>0));
Output:
Table Created.
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 14
Subject : RDBMS Using Oracle
Output:
1 row(s) inserted.
1. List products whose selling price is greater than 500 and less than or equal to 750.
Select Product_No, Description from Product_Master Where SellPrice >= 500 and
SellPrice < =750;
Or
Select * From Product_Master Where Sellprice Between 500 And 750;
2. List products whose selling price is more than 500. Calculate a new selling price as original
selling price * 15. Rename the new column in the output of the above query as New_Price.
Select Product_No, Description, SellPrice, SellPrice*15 "New_Price" from
Product_Master Where SellPrice > 500;
4. Determine the maximum and minimum product prices. Rename the output as Max_Price and
Min_Price respectively.
Select Max(SellPrice) "Max_Price", Min(SellPrice) "Min_Price" from Product_Master;
6. Count the number of products having price less than or equal to 500
Select Count (Product_No) from Product_Master Where SellPrice <= 500;
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 15
Subject : RDBMS Using Oracle
Exercise-5
Create a table Salesman_Master which include these fields, Insert records in the table then perform a given
queries:
Create Table:
Output:
Table Created.
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 16
Subject : RDBMS Using Oracle
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 17
Subject : RDBMS Using Oracle
Exercise-6
Create a table Sales_Order which include given fields, Insert records in the table and use
foreign key concept.
Create Table:
Output:
Table Created.
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 18
Subject : RDBMS Using Oracle
Output:
1 row(s) inserted.
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 19
Subject : RDBMS Using Oracle
Exercise-7
Create a table Sales_Order_Details which include these fields use foreign key, Insert records
in the table then perform a given queries:
Create Table:
Output:
Table Created.
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 20
Subject : RDBMS Using Oracle
insert into Sales_Order_Details
(Order_No,Product_No,QtyOrdered,QtyDisp,ProductRate)val
ues ('O19001','P00001',4,4,525);
Output:
1 row(s) inserted.
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 21
Subject : RDBMS Using Oracle
1. List all information from the Sales_Order table for orders place in the month of June.
Select * from Sales_Order Where to_char (OrderDate, 'Mon') = 'Jun';
4. List the order no and day on which client placed their order.
Select Order_No, to_char (OrderDate, 'DAY') from
Sales_Order;
5. List the month (in alphabets) and date when the orders must be delivered.
Select to_char(DelyDate, 'MONTH'),DelyDate from Sales_Order order by to_char
(DelyDate, 'MONTH');
6. List the OrderDate in the format ‘DD – Month – YY’ e.g. 12 – February -
02 Select to_char (OrderDate,'DD – MONTH – YY') from Sales_Order;
8. Print the description and total qty sold for each product.
select description, sum(qtydisp) from product_master pm, sales_order_details sod where
pm.product_no = sod.product_no group by description;
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 22
Subject : RDBMS Using Oracle
Exercise-8
Create Table:
Output:
Table Created.
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 23
Subject : RDBMS Using Oracle
Output:
1 row(s) inserted.
1. Where
Select * From Emp Where Deptno>20;
Output:
Output:
Select Job, Avg (Sal) From Emp Group By Job Having Job='Analyst';
Output:
3. Roll Up
Select EmpNo, Ename, Sum(sal) From Emp Group By Rollup(EmpNo,Ename);
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 24
Subject : RDBMS Using Oracle
4. Cube
Select EmpNo, Ename, Sum(sal) From Emp Group By Cube(EmpNo,Ename);
Output:
5. Order By
Select Job From Emp Order By Job;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 25
Subject : RDBMS Using Oracle
Select Ename From Emp Order By Ename Desc;
Output:
6. Distinct
Select Distinct Job From Emp;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 26
Subject : RDBMS Using Oracle
Special Operator
1. In
List all employee whose salary in 1500 to 2500
Output:
2. Any
Select Empno, Sal From Emp Where Sal > Any (2000, 3000, 4000);
Output:
3. All
Select Empno, Sal From Emp Where Sal > All (2000, 3000, 4000);
Output:
4. Between
Select * From Emp Where Sal Between 1500 And 2500;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 27
Subject : RDBMS Using Oracle
FROM Sales_Order WHERE Sales_Order.Salesman_No = Salesman_Master.Salesman_No AND
OrderStatus = 'Fulfilled');
Output:
Not Exists
Output:
6. Like
List the name of all client having ‘a’ as the second letter in their names.
Select * From Emp Where Ename Like '_a%';
Output:
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 28
Subject : RDBMS Using Oracle
Exercise-9
Perform following queries using all different kinds of Joins
Output:
Output:
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 29
Subject : RDBMS Using Oracle
Output:
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 30
Subject : RDBMS Using Oracle
Exercise-10
Perform following query using the concepts of Subquery, Minus, Intersect, Union
1. Subquery
SELECT * FROM Client
WHERE Client_no IN
(SELECT Client_no FROM Client WHERE Baldue > 1500) ;
Output:
2. Minus
SELECT Salesman_No
FROM Salesman_Master
MINUS
SELECT Salesman_No
FROM Sales_order;
Output:
3. Intersect
SELECT Salesman_No
FROM Salesman_Master
INTERSECT
SELECT Salesman_No
FROM Sales_order;
Output:
4. Union
SELECT Salesman_No
FROM Salesman_Master
UNION
SELECT Salesman_No
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 31
Subject : RDBMS Using Oracle
FROM Sales_order;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 32
Subject : RDBMS Using Oracle
Exercise-11
Perform following queries using different types function such as Numeric function, Character
function, Date function, Aggregate function, General function.
Numeric Function
Create Table:
Output:
Table Created.
No1 No2
6.28 5
7.28 6
-8.28 3
-8.28 3
0 3
Output:
1 row(s) inserted.
1. Abs
Select No1, Abs(No1) From Number_1;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 33
Subject : RDBMS Using Oracle
2. Ceil
Select No1, Ceil(No1) From Number_1;
Output:
3. Exp
Select No2, Exp(No2) From Number_1;
Output:
4. Floor
Select No1, Floor(No1) From Number_1;
Output:
5. Greatest
Select Greatest (1, '3.925', '2.4') "Greatest" From Dual;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 34
Subject : RDBMS Using Oracle
Output:
6. Least
Select Least('Harry','Harriot','Harold') "Least" From Dual;
Output:
7. Log
Select Log(10,100) From Dual;
Output:
8. Max
Select Max(No2) "Max Value" From Number_1;
Output:
9. Min
Select Min(No2) "Min Value" From Number_1;
Output:
10. Remainder
Select Remainder(15, 6)"Remainder" From Dual;
Output:
11. Round
Select No1, Round(No1) From Number_1;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 35
Subject : RDBMS Using Oracle
12. Sign
Select Sign(-5) From Dual;
Output:
13. Sqrt
Select No1,Abs(No1),Sqrt(Abs(No1)) From Number_1;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 36
Subject : RDBMS Using Oracle
Character Function
Create Table:
Output:
Table Created.
Name
mahesh
dipesh
Ramesh
PRITESH
HARESH
Output:
1 row(s) inserted.
1. Chr
Select Chr(65) As "Character" From Dual;
Output:
2. Concat
Select Concat('Welcome ','To Oracle') "Concat" From Dual;
Output:
3. Initcap
Select Name,Initcap(Name) From Stud_List;
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 37
Subject : RDBMS Using Oracle
Output:
4. Lower
Select Name,Lower(Name) From Stud_List;
Output:
5. Upper
Select Name,Upper(Name) From Stud_List;
Output:
6. Lpad
Select Lpad(Name,10,'*')"Lpad" From Stud_List;
Output:
7. Rpad
Select Rpad(Name,10,'*')"Rpad" From Stud_List;
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 38
Subject : RDBMS Using Oracle
Output:
8. Ltrim
Select Name,Ltrim (Name,'P') "Ltrim" From Stud_List;
Output:
9. Rtrim
Select Name,Rtrim (Name,'h') "Rtrim" From Stud_List;
Output:
10. Trim
select TRIM(' ' FROM ' tech ')AS TrimmedString from dual;
Output:
11. Replace
Select replace('PGDCA College','PGDCA','BCA')AS Replace from dual;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 39
Subject : RDBMS Using Oracle
12. Substr
Select Name,substr(Name, 3,4) "Substring" from Stud_List;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 40
Subject : RDBMS Using Oracle
Date Function
1. Add_months
Select add_months (sysdate ,4) "Add_Months" from dual ;
Output:
2. Last_day
Select last_day (sysdate)"Last_day" from dual;
Output:
3. Months_between
Select months_between ('02-april-21','02-july-21')"Months_between" from dual;
Output:
4. Next_day
Select next_day ('02-july-21','wednesday')"Next_day" from dual;
Output:
5. Round(date)
Select ROUND(TO_DATE ('22-june-21'),'YEAR')"Round" from dual;
Output:
6. Sysdate
Select SYSDATE From Dual;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 41
Subject : RDBMS Using Oracle
7. Systimestamp
Select systimestamp from dual;
Output:
8. Trunc(date)
Select TRUNC(TO_DATE('22-JULY-21'), 'MONTH')"Truncate" from dual;
Output:
9. To_date
Select To_Date('09/07/2021', 'Dd/Mm/Yyyy') To_Date From Dual;
Output:
10. To_char
Select To_Char(33459, '$99,999') To_Char From Dual;
Output:
Output:
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 42
Subject : RDBMS Using Oracle
Aggregate Function
1. Sum
Select Sum(SalAmt)As "Total Salary" From Salesman_Master;
Output:
2. Count
Select Count(Salesman_No)As "Total Salesman" From Salesman_Master;
Output:
3. Avg
Select Avg(SalAmt)As "Average Salary" From Salesman_Master;
Output:
4. Max
Select Max(SalAmt)As "Maximum Salary" From Salesman_Master;
Output:
5. Min
Select Min(SalAmt)As "Minimum Salary" From Salesman_Master;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 43
Subject : RDBMS Using Oracle
General Function
1. Coalesce
Select Coalesce(NULL, NULL, NULL, 'W3Schools.com', NULL, 'Example.com') As
"Coalesce" From Dual;
Output:
2. Case
SELECT Product_No, QtyOnHand,
CASE
WHEN QtyOnHand > 100 THEN 'The quantity is greater than 100'
WHEN QtyOnHand = 100 THEN 'The quantity is 100'
ELSE 'The quantity is either greater than 100 nor less than 100'
END AS QuantityText
FROM Product_Master;
Output:
3. Decode
SELECT SalesmanName,
DECODE(Salesman_No, 'S00001', 'Aman',
'S00002', 'Omkar',
'S00003', 'Raj',
'Aarav') result
FROM SALESMAN_MASTER;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 44
Subject : RDBMS Using Oracle
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 45
Subject : RDBMS Using Oracle
Exercise-12
Data Control and Transaction Control Command
SQL> connect
Enter user-name: system
Enter password: manager
Connected.
SQL> create user angel identified by angel123;
User created.
Grant succeeded.
SQL> connect
Enter user-name: scott
Enter password: tiger
Connected.
SQL> create table test_data
2 (no number(5),
3 name varchar2(15));
Table created.
1 row created.
1 row created.
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 46
Subject : RDBMS Using Oracle
NO NAME
1 aarva
2 prathvi
Grant succeeded.
SQL> commit;
Commit complete.
SQL> connect
Enter user-name: angel
Enter password: angel123
Connected.
SQL> select * from scott.test_data;
NO NAME
1 aarva
2 prathvi
1 row created.
NO NAME
1 aarva
2 prathvi
3 freya
Note: We have not given delete grant to user angel so it will generate error insufficient privileges.
SQL> connect
Enter user-name: scott
Enter password: tiger
Connected.
SQL> revoke insert on test_data from angel;
Revoke succeeded.
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 47
Subject : RDBMS Using Oracle
SQL> connect
Enter user-name: angel
Enter password: angel123
Connected.
SQL> insert into scott.test_data (no,name) values (4,'mann');
insert into scott.test_data (no,name) values (4,'mann')
*
ERROR at line 1:
ORA-01031: insufficient privileges
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 48
Subject : RDBMS Using Oracle
SQL> connect
Enter user-name: system
Enter password: manager
Connected.
SQL> create role testing;
Role created.
Grant succeeded.
Grant succeeded.
User created.
Grant succeeded.
Grant succeeded.
SQL> commit;
Commit complete.
SQL> connect
Enter user-name: mmg
Enter password: mmghodasara
Connected.
SQL> select * from system.stud;
S00001 Ishita 80
S00002 Ruhi 65
SQL> connect
Enter user-name: system
Enter password: manager
Connected.
SQL> revoke select on stud from testing;
Revoke succeeded.
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 49
Subject : RDBMS Using Oracle
SQL> connect
Enter user-name: scott
Enter password: tiger
Connected.
SQL> create table stud_data
2 (no number(5),
3 name varchar2(15),
4 marks number (5));
Table created.
SQL> commit;
Commit complete.
NO NAME MARKS
1 sachi 75
2 radha 80
3 krishna 80
NO NAME MARKS
1 sachi 85
2 radha 80
3 krishna 80
4 pakhi 90
SQL> rollback;
Rollback complete.
NO NAME MARKS
1 sachi 85
2 radha 80
3 krishna 80
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 50
Subject : RDBMS Using Oracle
SQL> connect
Enter user-name: scott
Enter password: tiger
Connected.
1 sachi 85
2 radha 80
3 krishna 80
Savepoint created.
1 row deleted.
2 radha 80
3 krishna 80
Savepoint created.
1 row deleted.
3 krishna 80
Savepoint created.
1 row deleted.
no rows selected
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 51
Subject : RDBMS Using Oracle
Rollback complete.
2 radha 80
3 krishna 80
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 52
Subject : RDBMS Using Oracle
UNIT : 3
Other ORACLE Database Objects Concurrency Control Using Lock
Exercise-1
Perform View.
A. View - Create, Insert, Update, Delete, Drop on Single Table:
create view vw_sales as select * from sales_order;
View created.
View created.
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 53
Subject : RDBMS Using Oracle
View created.
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 54
Subject : RDBMS Using Oracle
Exercise-2
Perform Sequence.
A. Create Sequence
CREATE SEQUENCE sequence_1
start with 1
increment by 1
minvalue 0
maxvalue 100
cycle;
E. Alter sequence
ALTER SEQUENCE sequence_1
INCREMENT BY 2
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 55
Subject : RDBMS Using Oracle
MAXVALUE 1000
CYCLE;
Sequence altered.
F. Drop sequence
DROP SEQUENCE sequence_1;
Sequence dropped.
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 56
Subject : RDBMS Using Oracle
Exercise-3
Perform Synonyms.
A. Create Synonyms
CREATE OR REPLACE SYNONYM synon1
FOR stud;
Synonym created.
Synonym dropped.
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 57
Subject : RDBMS Using Oracle
Exercise-4
Perform Database Link
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 58
Subject : RDBMS Using Oracle
Exercise-5
Perform Index.
A. Simple Index
B. Composite Index:
C. Unique Index:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 59
Subject : RDBMS Using Oracle
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 60
Subject : RDBMS Using Oracle
Exercise-6
Perform cluster.
A. Create cluster
Create Cluster C1(D Number(2));
Cluster created.
Index created.
Table created.
1 row created.
1 row created.
Table created.
1 row created.
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 61
Subject : RDBMS Using Oracle
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 62
Subject : RDBMS Using Oracle
UNIT : 4
Introduction to PL/SQL, Advanced PL/SQL
Exercise-1
Simple PL/SQL Block structure.
Begin
--this is first PL/SQL Block form
dbms_output.put_line ('Welcome to PL/SQL Block');
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 63
Subject : RDBMS Using Oracle
Exercise-2
Variable Declaration
Declare
x number (5);
ans number (5);
Begin
x:= :x;
ans:= x*x;
dbms_output.put_line ('Square is = '||ans);
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 64
Subject : RDBMS Using Oracle
Exercise-3
Write A Program To Find Simple Interest.
Declare
p number:= :p;
r number:= :r;
n number:= :n;
ans number;
Begin
ans:= (p*r*n)/100;
dbms_output.put_line('Simple Interest is = '||ans);
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 65
Subject : RDBMS Using Oracle
Exercise-4
Write a program for global and local variable.
DECLARE
-- Global variables
num1 number := 95;
num2 number := 85;
BEGIN
dbms_output.put_line('Outer Variable num1: ' || num1);
dbms_output.put_line('Outer Variable num2: ' || num2);
DECLARE
-- Local variables
num1 number := 195;
num2 number := 185;
BEGIN
dbms_output.put_line('Inner Variable num1: ' || num1);
dbms_output.put_line('Inner Variable num2: ' || num2);
END;
END;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 66
Subject : RDBMS Using Oracle
Exercise-5
DECLARE
-- constant declaration
pi constant number := 3.141592654;
-- other declarations
radius number(5,2);
dia number(5,2);
circumference number(7, 2);
area number (10, 2);
BEGIN
radius := 9.5;
dia := radius * 2;
circumference := 2.0 * pi * radius;
area := pi * radius * radius;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 67
Subject : RDBMS Using Oracle
Exercise-6
Write a program to check ticket age wise with the use of IF-THEN-ELSIF-ELSE
Declare
age number:= :age;
Begin
if age<=4 then
dbms_output.put_line ('No Ticket');
else
dbms_output.put_line ('Senior citizen Discount');
end if;
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 68
Subject : RDBMS Using Oracle
Exercise-7
Write a program to input three no and find out maximum and minimum from it.
Declare
val1 number(5);
val2 number(5);
val3 number(5);
ma number(5);
mi number(5);
Begin
val1:=:val1;
val2:=:val2;
val3:=:val3;
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 69
Subject : RDBMS Using Oracle
Exercise-8
Write a program to input rollno and three subject marks. find out total, percentage, result
and grade for the student from the entered data.
Declare
rollno number(5);
sub1 number(10);
sub2 number(10);
sub3 number(10);
total number(10);
per number(5,2);
result char(4);
grade char(25);
Begin
rollno:=:rollno;
sub1:=:sub1;
sub2:=:sub2;
sub3:=:sub3;
total:= sub1 + sub2 + sub3;
per:= total/3;
end if;
else
result:= 'Fail';
grade:= 'No Grade';
end if;
End;
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 70
Subject : RDBMS Using Oracle
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 71
Subject : RDBMS Using Oracle
Exercise-9
Write a program for the use of PL/SQL Case Statement
DECLARE
grade char(1):=:grade;
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('Average');
when 'F' then
dbms_output.put_line('Passed with Grace');
else dbms_output.put_line('Failed');
END CASE;
END;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 72
Subject : RDBMS Using Oracle
Exercise-10
Looping
Write a program to print first 10 numbers using basic loop.. exit loop.
Declare
i number := 1;
Begin
Loop
Exit When i>10;
Dbms_Output.Put_Line(i);
i := i+1;
End Loop;
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 73
Subject : RDBMS Using Oracle
Exercise-11
Write a program to print first 10 numbers using while loop.
Declare
i integer := 1;
Begin
while i <= 10
loop
dbms_output.put_line(i);
i := i+1;
end loop;
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 74
Subject : RDBMS Using Oracle
Exercise-12
Write a program to print first 10 numbers using for loop.
Declare
Var1 Number;
Begin
Var1:=10;
For Var1 In 1..10
Loop
Dbms_Output.Put_Line (Var1);
End Loop;
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 75
Subject : RDBMS Using Oracle
Exercise-13
Write a program to print first 1 to 10 numbers in reverse using for loop.
Declare
x number (5);
Begin
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 76
Subject : RDBMS Using Oracle
Exercise-14
Write a program to print first 10 prime number using while loop.
Declare
c number (4);
cntr number (4);
a number (4);
flag number (2);
Begin
c:=0;
cntr:=3;
While c<10
loop
flag:=0;
For i in 2.. (cntr-1)
loop
a:=cntr/i;
if (a*i) = cntr then
Flag:=1;
end if;
End loop;
if flag = 0 then
dbms_output.put_line (cntr);
C:=c+1;
end if;
cntr:=cntr+1;
End loop;
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 77
Subject : RDBMS Using Oracle
Exercise-15
String reverse
Declare
s1 varchar2 (20);
s2 varchar2 (20);
x number (20);
Begin
s1:=:s1;
x:=length (s1);
For i in reverse 1..x
loop
s2:=s2 || substr (s1, i, 1);
End loop;
dbms_output.put_line ('Reverse string is ' || s2);
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 78
Subject : RDBMS Using Oracle
Exercise-16
Triangle
Declare
s1 varchar2 (10);
s2 varchar2 (10);
x number (10);
Begin
s1:=:s1;
x:=length (s1);
for i in 1..x
loop
s2:= substr (s1, 1, i);
dbms_output.put_line (s2);
end loop;
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 79
Subject : RDBMS Using Oracle
Exercise-17
Declare
Begin
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 80
Subject : RDBMS Using Oracle
Exercise-18
DECLARE
a number(2) := 10;
BEGIN
<<loopstart>>
-- while loop execution
WHILE a < 20 LOOP
dbms_output.put_line ('value of a: ' || a);
a := a + 1;
IF a = 15 THEN
a := a + 1;
GOTO loopstart;
END IF;
END LOOP;
END;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 81
Subject : RDBMS Using Oracle
Exercise-19
Table created.
Program
Declare
no number (5):=1;
s number (5):=0;
Begin
While no<=10
Loop
s:=no*no;
Insert into square values (no, s);
No:=no+1;
End loop;
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 82
Subject : RDBMS Using Oracle
Exercise-20
Looping to update specified table.
Declare
n number (5):=1;
Begin
While n<=10 Loop
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 83
Subject : RDBMS Using Oracle
Exercise-21
Declare
n number(5):=10;
Begin
n:=:n;
Delete from square where no =n;
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 84
Subject : RDBMS Using Oracle
Exercise-22
%TYPE
Create table....
PL/SQL block
Declare
eno emp1.emp_no %TYPE;
ename emp1.name %TYPE;
Begin
eno:= '5';
ename:= 'lavya';
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 85
Subject : RDBMS Using Oracle
Exercise-23
%ROWTYPE
Declare
-- declare variables
-- declare record variable that represents a row fetched from the emp1 table
emp_rec emp1%ROWTYPE; -- declare variable with %ROWTYPE attribute
Begin
SELECT * INTO emp_rec FROM emp1 WHERE emp_no = 1; -- retrieve record
dbms_output.put_line('Employee name: ' || emp_rec.name );
dbms_output.put_line('Employee salary: ' || emp_rec.salary); -- display
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 86
Subject : RDBMS Using Oracle
Exercise-24
Implicit Cursor [%found]
Write a program to accept empno. If the employee is found then update the salary of
employee by 15% and display a message base on the record.
Declare
Empno emp1.emp_no %type;
Begin
Update emp1 set salary =salary+ salary * 0.15 where emp_no = :empno;
If sql%found then
dbms_output.put_line ('Record is updated');
Else
dbms_output.put_line ('Record does not exist');
End if;
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 87
Subject : RDBMS Using Oracle
Exercise-25
Implicit Cursor [%rowcount]
Write a program to accept department number of employee and update the salary of that
employee to increment by 1000. Display appropriate message and display how many rows
are updated. (Using %rowtype attribute)
Declare
rows_update char (3);
deptno number (10);
Begin
Update emp1 set salary = salary+1000 where dept_no=:deptno;
rows_update:= to_char (sql%rowcount);
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 88
Subject : RDBMS Using Oracle
Exercise-26
Explicit Cursor [%isopen]
Write a program to find that explicit cursor is open or not if cursor is open then display
appropriate message.
Declare
Cursor cur_emp is select name from emp1 where dept_no=10;
Begin
Open cur_emp;
Else
dbms_output.put_line ('The cursor is closed');
End if;
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 89
Subject : RDBMS Using Oracle
Exercise-27
Explicit Cursor [%notfound]
Write a program to display name and salary of emp1 table. (using %rowtype)
Declare
xemp emp1 %rowtype;
Cursor c_emp1 is select * from emp1;
Begin
Open c_emp1;
Loop
Fetch c_emp1 into xemp;
Exit when c_emp1 %notfound;
dbms_output.put_line ('Salary of ' || xemp.name || ' is ' || xemp.salary);
End loop;
Close c_emp1;
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 90
Subject : RDBMS Using Oracle
Exercise-28
Explicit cursor [%found]
Write a program to update the salary of employee and insert the record in bonus table (using
explicit cursor %found).
We need 2 tables emp1 and bonus. emp1 table we created in the above exercise so create
bonus table.
--PL/SQL Block
Declare
Cursor cur_emp is Select emp_no,salary from emp1 where dept='sales';
e_no emp1.emp_no %type;
e_sal emp1.salary %type;
Begin
Open cur_emp;
Loop
Fetch cur_emp into e_no, e_sal;
Close cur_emp;
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 91
Subject : RDBMS Using Oracle
Exercise-29
Explicit cursor [%notfound]
Write a program for the use of parameterized cursor. (In this program insert record in the
branch_master table that is exists in the branch table. If the record does not exist then display
message.
Table : branch
Create table branch
(branch_no varchar2(10),
name varchar2(10));
Table : branch_master
Create table branch_master
(branch_no varchar2(10),
name varchar2(10));
--PL/SQL Block
Declare
Cursor cur_branch is select * from branch;
Cursor cur_branch_mstr(str_branch_name varchar2) is select branch_no from branch_master
where name = str_branch_name;
str_branch_no branch.branch_no %type;
str_branch_name branch.name %type;
mast_view varchar2 (10);
Begin
Open cur_branch;
Loop
Fetch cur_branch into str_branch_no,str_branch_name;
Exit when cur_branch %notfound;
Open cur_branch_mstr(str_branch_name);
Fetch cur_branch_mstr into mast_view;
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 92
Subject : RDBMS Using Oracle
Close cur_branch_mstr;
End loop;
Close cur_branch;
Commit;
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 93
Subject : RDBMS Using Oracle
Exercise-30
--Cursor for loops
/*Write a program to give a bonus of rs. 1000 to employee who are in the sales department
and store that record in the bonus table using cursor for loops.*/
Declare
Cursor cur_emp is select emp_no,salary from emp1 where dept='sales';
Begin
For emp_rec in cur_emp
Loop
Update emp1 set salary=salary + 1000 where emp_no=emp_rec.emp_no;
Insert into bonus values (emp_rec.emp_no, emp_rec.salary + 1000, sysdate);
End loop;
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 94
Subject : RDBMS Using Oracle
Exercise-31
Exception Handling
/*Write a program to enter two numbers and divide the no1 by no2. If the user enters the zero
value of no2 then display appropriate error message using the exception handler. */
Declare
no1 number(10);
no2 number(10);
ans number(10);
Begin
no1:=:number1;
no2:=:number2;
ans:= no1/no2;
dbms_output.put_line ('Division is = '|| ans);
Exception
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 95
Subject : RDBMS Using Oracle
Exercise-32
Procedure (with in parameter)
--Procedure
Create or replace procedure p1(a in number)is
ans number;
Begin
ans:= a*2;
dbms_output.put_line ('The Answer is:'|| ans);
End;
--PL/SQL Block
Begin
p1(5);
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 96
Subject : RDBMS Using Oracle
Exercise-33
/*Write a PL/SQL statement to create procedure which accept no from the user
and multiply by 3 on the screen. (Use in out parameter)*/
--Procedure
--PL/SQL Block
Declare
a number;
Begin
a:=:a;
dbms_output.put_line ('The Real no is: '||a);
p2(a);
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 97
Subject : RDBMS Using Oracle
Exercise-34
/*Write a PL/SQL statement to create procedure which accept no from the user
and multiply by 3 on the screen. (Use out parameter)*/
--Procedure
--PL/SQL Block
Declare
a number;
Begin
p3(a);
dbms_output.put_line ('The answer is '||a);
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 98
Subject : RDBMS Using Oracle
Exercise-35
Procedure to insert value in table.
--Procedure
--PL/SQL block
Begin
in_record(:no,:name);
End;
Output:
Run the PL/SQL block again and insert more records given below…
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 99
Subject : RDBMS Using Oracle
Exercise-36
Procedure to update value in table.
--Procedure
--PL/SQL block
Begin
up_record (:num,:nm);
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 100
Subject : RDBMS Using Oracle
Exercise-37
Procedure to delete value in table
--Procedure
--PL/SQL block
Begin
del_record (:num);
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 101
Subject : RDBMS Using Oracle
Exercise-38
Function
--Function
--PL/SQL block
Declare
a number:=:a;
b number:=:b;
ans number(3);
Begin
ans:=func_add(a, b);
dbms_output.put_line ('The addition is:-' || ans);
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 102
Subject : RDBMS Using Oracle
Exercise-39
--Package
-- Create a package to display appropriate message.
-- Package Definition:
--Package body
End pack1;
--PL/SQL block
Declare
--msg varchar(50);
Begin
--msg:=pack1.f1();
dbms_output.put_line(pack1.f1 ());
pack1.p1 ();
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 103
Subject : RDBMS Using Oracle
Exercise-40
-- Package
-- Create a package for addition and subtraction of two values.
-- Package Definition:
--Package body
End pack2;
--PL/SQL block
Declare
a number:=3;
b number:=2;
ans number;
Begin
pack2.p2 (a,b);
ans:=pack2.f2(a,b);
dbms_output.put_line ('The Substraction is:-' || ans);
End;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 104
Subject : RDBMS Using Oracle
Exercise-41
-- Trigger
--Create a trigger
DECLARE
total_rows number(2);
BEGIN
UPDATE customers SET salary = salary + 5000;
IF sql%notfound THEN
dbms_output.put_line('no customers updated');
ELSIF sql%found THEN
total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' customers updated ');
END IF;
END;
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 105
Subject : RDBMS Using Oracle
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 106
Subject : RDBMS Using Oracle
Exercise-42
Create table emp1_1 (emp_no number (10), ename varchar2 (10), deptartment dept1)
Nested table deptartment store as dept_tab;
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 107
Subject : RDBMS Using Oracle
Exercise-43
Create table student (std_no number (10) primary key, name varchar2 (15), marks
marks_va);
Output:
Prof. P.V.Thummar Kamani Science & Prataprai Arts College - Amreli Page 108