Tải bản đầy đủ (.pdf) (12 trang)

Tài liệu Summary of PL / SQL pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (77.81 KB, 12 trang )

Summary of PL/SQL
26
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder26Ć2
Summary of PL/SQL 26Ć3
Summary
PL/SQL (Procedural Language/SQL) is an extension to SQL. It is a language that
allows you to combine multiple SQL statements with procedural controls such as
identifiers, conditional, and iterative constructs in a PL/SQL block.
Benefits from PL/SQL
D Modularize program development by grouping similar concepts together and
nesting blocks
D Declare identifiers, for example, variables, constants, and cursors
D Program with procedural language structures, for example, IF statements and
loops
D Exception handling for Oracle7 Server errors and user-defined exceptions
D Port code between applications and the Oracle7 Server
D Integrate Oracle7 stored subprograms and application subprograms
D Improve performance by sending a block of code to the server
PL/SQL Block
A PL/SQL block contains code in up to four sections. The header section is where
you set the name and parameters for named blocks. The declarative section is
optional and contains all variables, constants, cursors, and user-defined exceptions
that will be referenced within the executable section. The executable section is
required and contains SQL statements to manipulate data in the database and PL/SQL
statements to manipulate data in the block. The exception-handling section is optional
and specifies the actions to perform when errors and abnormal conditions arise within
the executable section.
Anonymous Blocks
Anonymous blocks are unnamed blocks. They are declared at the point in an
application where they are to be executed and are passed to the PL/SQL engine for
execution at runtime. You can embed an anonymous block within a precompiler


program and within SQL*Plus or Server Manager. Triggers in Developer/2000
components consist of such blocks.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder26Ć4
Summary of PL/SQL 26Ć5
Summary continued
Procedures
Procedures are named PL/SQL blocks that store a series of actions for later execution.
The procedure can contain zero or more parameters, which are arguments that you list
when calling the procedure. Procedures can either be application specific or stored in
the Oracle7 Server.
Functions
A function returns a value to the calling environment. You must declare the RETURN
datatype in the header section of the function definition, and define the value to be
returned in the PL/SQL block. You can also declare a list of parameters to be passed
into the function. Functions are called as part of an expression.
DML in PL/SQL
You can use DML statements in your PL/SQL blocks. Use the SELECT statement to
retrieve data from the database.
D The SELECT statement contains an additional mandatory clause: the INTO
clause. In the INTO clause, list the output variables for receiving the data. The
SELECT statement must return exactly one row or an error will occur.
D The INSERT statement adds new rows of data to the table.
D The UPDATE statement modifies existing rows in the table.
D The DELETE statement removes unwanted rows from the table.
D COMMIT ends the current transaction by making all pending changes to the
database permanent.
D ROLLBACK ends the current transaction by discarding all pending changes.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder26Ć6
Summary of PL/SQL 26Ć7
Summary continued

IF Conditional Statements
The PL/SQL IF statement is similar to the structure of IF statements in other
procedural languages. It allows PL/SQL to perform actions selectively based upon
conditions. If the controlling Boolean condition is TRUE, the associated sequence of
statements is executed; if the controlling Boolean condition is FALSE or NULL, the
associated sequence of statements is passed over.
Iterative Loop Statements
You can use one of three loop structures to repeat a sequence of statements.
D The basic loop consists of the body of statements to be repeated enclosed between
the delimiters LOOP and END LOOP. This uncontrolled loop is an infinite loop
that is to be avoided. To avoid an infinite loop, add an EXIT statement.
D FOR loops have a control statement at the front of the LOOP keyword to
determine the number of iterations PL/SQL performs.
D The WHILE loop repeats a sequence of statements until the controlling condition
is no longer TRUE. The condition is evaluated at the start of each iteration.
Cursors
PL/SQL cursors let you name a private SQL area and access its stored information.
The cursor directs all phases of processing. Explicit cursors are declared and named
by the programmer and manipulated through specific statements within the block’s
executable actions. Control explicit cursors in a loop with four steps: declare, open,
fetch, and close. Cursor FOR loops are a shortcut to open the cursor, fetch rows once
for each loop iteration, and automatically close the cursor after all rows are
processed.
Exception Handling
An exception is an identifier in PL/SQL, raised during the execution of a block that
terminates its main body of actions. A block will always terminate when PL/SQL
raises an exception, but you specify an exception handler to perform final actions.
Exceptions can be raised either by the Oracle7 Server or by the user. You can either
trap an exception in the exception-handling section of the block, or allow it to
propagate to the calling environment.

Procedure Builder
Oracle Procedure Builder is a tool you can use to create, execute, and debug PL/SQL
programs used in your application tools, such as a form or report, or on the Oracle7
Server through its graphical interface.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder26Ć8
Summary of PL/SQL 26Ć9
Practice Overview
This practice has you build a PL/SQL-based application to insert, update, and delete
records in a video store database and generate a report. The database contains only
the essential tables.
Practice Contents
D Creating procedures and functions
D Inserting, updating, and deleting data
D Incorporating IF statements and loops
D Handling exceptions
Note: If you are new to the class, you can execute the buildvid.sql script in
SQL*Plus to create and populate the tables. If you want to drop the work you
did in the course summary from the SQL section, you can execute the
dropvid.sql script in SQL*Plus to drop their tables. Then, you can execute the
buildvid.sql script in SQL*Plus to create and populate the tables.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder26Ć10
Summary of PL/SQL 26Ć11
Practice 26
Entity Relationship Diagram
The facing page contains an entity relationship diagram depicting the tables and
columns for a small movie rental application.
1. Complete the database design by writing subprograms to perform the following
actions.
a. Create a function named NEW_MEMBER to return the membership number
for that new member. Be sure to use a sequence generator to create the unique

member numbers. Check the data dictionary for the sequence name.
b. Create a function named NEW_RENTAL to record a new rental based on a
member’s last name. Return a message with the expected due date. If two or
more people have the same last name, store the first name, last name, phone,
and membership number for all matching last names in the RESULTS table.
c. Create a procedure named MEM_TITLES to retrieve all the titles rented by a
member. Store the results in the RESULTS table.
d. If a title is rented, create a reservation for that movie. Name your procedure
RESERVE_MOVIE.
e. When a video is returned, change the rental status for that returned copy.
When the video is successfully checked in, store a message in the RESULTS
table. Check the to see if there are reservations for the film title, and store a
message in the RESULTS table. Name your procedure RETURN_RENTAL.
f. If a video is returned damaged, change its status to DAMAGED. If that copy
was the only copy owned by the store, store a message to buy another copy in
the RESULTS table. If only one copy remains for rental, store a warning that
only one copy is left for rental in the RESULTS table. Name your procedure
DAMAGED_COPY.
g. Create a procedure named TOP_RENTAL to determine the top rental. Allow
for more than one top rental. Store the names and a message of the top rentals
in the RESULTS table.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder26Ć12

×