Dbms Ex6
Dbms Ex6
Description:
PL/SQL supports variables, conditions, loops and exceptions, arrays are also supported, though in a
somewhat unusual way, involving the use of PL/SQL collections. PL/SQL collections are a slightly
advanced topic. Implementations from version 8 of Oracle Database onwards have included features
associated with orientation.PL/SQL program units (essentially code containers) can be compiled into the
Oracle database. Programmers can thus embed PL/SQL units of functionality into the database directly.
They also can write scripts containing PL/SQL program units that can be read into the database using
the Oracle SQL *Plus tool.
Once the program units have been stored into the database, they become available for execution at a
later time.While programmers can readily embed Data Manipulation Language(DML) statements
directly into their PL/SQL code using straight forward SQL statements, Data Definition Language
(DDL) requires more complex “Dynamic SQL” statements to be written in the PL/SQL code. However,
DML statements underpin the majority of PL/SQL code in typical software applications.
In the case of PL/SQL dynamic SQL, early versions of the Oracle Database required the use of a
complicated Oracle DBMS_SQL package library. More recent versions have however introduced a
simpler “Native Dynamic SQL” , along with an associated EXECUTE IMMEDIATE syntax. Oracle
Corporation customarily extends package functionality with each successive release of the Oracle
Database.
Detailed Procedure:
Anonymous blocks form the basis of the simplest PL/SQL code, and have the following structure:
<<label>>
DECLARE
TYPE / item / FUNCTION / PROCEDURE declarations
BEGIN
Statements
EXCEPTION
EXCEPTION handlers
END label;
The <<label>> and the DECLARE and EXCEPTION sections are optional.
Exceptions, errors which arise during the execution of the code, have one of two types:
1. Predefined exceptions
2. User-defined exceptions.
User-defined exceptions are always raised explicitly by the programmers, using the RAISE or
RAISE_APPLICATION_ERROR commands, in any situation where they have determined that it is
impossible for normal execution to continue. RAISE command has the syntax:
20CS2016L - Database Management Systems Lab URK22CS
RAISE <exception name>. Oracle Corporation has pre-defined several exceptions like
NO_DATA_FOUND, TOO_MANY_ROWS, etc. Each exception has a SQL Error Number and SQL
Error Message associated with it. Programmers can access these by using the SQLCODE and
SQLERRM functions. The DECLARE section defines and (optionally) initializes variables. If not
initialized specifically, they default to NULL.
For example:
DECLARE
number1 NUMBER(2);
number2 NUMBER(2) := 17; -- value default
text1 VARCHAR2 (12):= „Hello world‟;
text2 DATE := SYSDATE; -- current date and time
BEGIN
SELECT street_number
INTO number1
FROM address
WHERE name = 'INU';
END;
Functions: Functions in PL/SQL are a collection of SQL and PL/SQL statements that perform a task
and should return a value to the calling environment. User defined functions are used to supplement the
many hundreds of functions built in by Oracle.
There are 2 different types of functions in PL/SQL. The traditional function is written in the form:
CREATE OR REPLACE FUNCTION <function_name> [(input/output variable declarations)]
RETURN return_type
[AUTHID <CURRENT USER | DEFINER>] <IS|AS>
[declaration block]
BEGIN
<PL/SQL block WITH RETURN statement>
RETURN <return value>;
[EXCEPTION EXCEPTION block]
RETURN <return value>;
END;
BEGIN
<PL/SQL block WITH RETURN statement>
PIPE ROW <return type>;
RETURN;
[EXCEPTION
EXCEPTION block]
PIPE ROW <return type>;
RETURN;
END;
There are three types of parameters: IN, OUT and IN OUT.
An IN parameter is used as input only. An IN parameter is passed by copy and thus cannot be changed
by the called program. An OUT parameter is initially NULL. The program assigns the parameter a value
and that value is returned to the calling program. An IN OUT parameter may or may not have an initial
value. That initial value may or may not be modified by the called program. Any changes made to the
parameter are returned to the calling program by default by copying but, ith the NOCOPY hint may be
passed by reference.
Procedures
Procedures are similar to Functions; in that they can be executed to perform work. The primary
difference is that procedures cannot be used in a SQL statement and although they can have multiple out
parameters they do not “RETURN” a value.
Procedures are traditionally the workhorse of the coding world and functions are traditionally the
smaller, more specific pieces of code. PL/SQL maintains many of the distinctions between functions and
procedures found in many general-purpose programming languages, but in addition, functions can be
called from SQL, while procedures cannot.
Questions:
1. Write a PL/SQL function named getUserEmail that takes a users name as input and returns their email
address as output. Assume you have a table named users with the following columns: UserID, Name,
Email, Password, and Phone. The function should use the input name to retrieve the corresponding email
address from the table and return it.
2. Write a PL/SQL procedure named changePhoneNumber that takes a users name and a new phone
number as input and updates the phone number in the users table. Assume you have a table named users
with the following columns: UserID, Name, Email, Password, and Phone. The procedure should use the
provided name to locate the user and update their phone number with the new one.
20CS2016L - Database Management Systems Lab URK22C
3. Write a PL/SQL function named getEventDescription that takes an event name as input and returns its
description as output. Assume you have a table named events with the following columns: EventID,
Name, Date, Time, VenueID, and Description. The function should use the input event name to retrieve
the corresponding event description from the table and return it.
4. Write a PL/SQL procedure named updateEventVenue that takes an event name and a new venue ID as
input and updates the venue ID in the events table. Assume you have a table named events with the
following columns: EventID, Name, Date, Time, VenueID, and Description. The procedure should use
the provided event name to locate the event and update its venue ID with the new one.
20CS2016L - Database Management Systems Lab URK22
5. Write a PL/SQL function named getVenueAddress that takes a venue name as input and returns its
address as output. Assume you have a table named venues with the following columns: VenueID, Name,
Address, City, State, and Country. The function should use the input venue name to retrieve the
corresponding venue address from the table and return it.
20CS2016L - Database Management Systems Lab U
6. Write a PL/SQL procedure named updateVenueCity that takes a venue name and a new city as input
and updates the city in the venues table. Assume you have a table named venues with the following
columns: VenueID, Name, Address, City, State, and Country. The procedure should use the provided
venue name to locate the venue and update its city with the new one.
7. Write a PL/SQL function named getVenueState that takes a venue name as input and returns its state
as output. Assume you have a table named venues with the following columns: VenueID, Name,
Address, City, State, and Country. The function should use the input venue name to retrieve the
corresponding venue state from the table and return it.
20CS2016L - Database Management Systems Lab URK22CS
8. Write a PL/SQL function named getTicketPrice that takes a ticket ID as input and returns the price of
the ticket as output. Assume you have a table named tickets with the following columns: TicketID,
EventID, UserID, SeatNumber, Price, and Status. The function should use the input ticket ID to retrieve
the corresponding ticket price from the table and return it.
9. Write a PL/SQL procedure named updateTicketStatus that takes a ticket ID and a new status as input
and updates the status in the tickets table. Assume you have a table named tickets with the following
columns: TicketID, EventID, UserID, SeatNumber, Price, and Status. The procedure should use the
provided ticket ID to locate the ticket and update its status with the new one.
20CS2016L - Database Management Systems Lab URK22
10. Write a PL/SQL function named getTotalTicketsForEvent that takes an event ID as input and returns
the total number of tickets booked for that event as output. Assume you have a table named tickets with
the following columns: TicketID, EventID, UserID, SeatNumber, Price, and Status. The function should
use the input event ID to count the number of tickets with the specified event ID in the table and return
that count.
Result:
The PL/SQl functions and procedures have been executed successfully.