Professional Documents
Culture Documents
Database Programming
Instructor: Lu Zhang, Ph.D.
Email: lzhang@cs.depaul.edu
Office Hours: Monday 4:30 5:30 PM
Monday after class
Prerequisite:
1. CSC319/CSC 449 and (CSC212 or
CSC224).
2. Review chapters 1 and 2 in the
textbook.
Online Resource:
http://otn.oracle.com/documentation/content.html
Topics
Oracle PL/SQL
Introduction to PL/SQL
PL/SQL Language Fundamentals
Conditions, Controls, and Loops
Database Interactions and Cursors
Exception Handlers
Debugging
Procedures and Functions
Packages
PL/SQL and SQL Interactions
Triggers
PL/SQL Best Practices
JDBC API that enables database access
in Java
SQLJ Embedded SQL with Java
Topics
Grading
Homework/Project: 40% (Late assignments will
be accepted but will be penalized at 16.67% per
day.)
Mid Term: 30% (in-class week 7)
Final: 30% (take-home)
Grades will be assigned based on the following
scale:
A: 90 - 100
B: 80 - 89
C: 70 79
D: 60 69, and
F: 59 and below
The "+" and "-" grade extensions will be used at the
instructor's discretion. Note, however, this does not
elevate grades.
Expectations
Attendance
Classroom participation
Classroom behavior
Homework
Prerequisites
Course On Line
Notes
Homework
Projects
Announcements
Grades
Graders email
etc.
About Email
Make sure that your email address is
current and correct in the system
For yahoo and hotmail users, make sure
that your account is not over the quota
What and how should I email?
What shouldnt I email?
Programming Environment
change password
source /usr/local/oracle.csh
hawk% exit
Programming Environment
ls
ls la
mkdir <dir-name>
cd <dir-name>
pwd
cd..
cp <file1> <file2>
mv <file1> <file2>
passwd
rm <file1>
rmdir
rm r <dir>
cat <file1>
more <file1>
Programming Environment
Programming Environment
2.
3.
Programming Environment
5.
6.
Programming Environment
Programming Environment
Programming Environment
Programming Environment
Programming Environment
Programming Environment
INST1_HTTP.CSTCIS.CTI.DEPAUL.EDU =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = lzhanglap)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = SHARED)
(SERVICE_NAME = MODOSE)
(PRESENTATION = http://HRService)
)
)
EXTPROC_CONNECTION_DATA.CSTCIS.CTI.DEPAUL.EDU =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC0))
)
(CONNECT_DATA =
(SID = PLSExtProc)
(PRESENTATION = RO)
)
)
LAPTOP.CSTCIS.CTI.DEPAUL.EDU =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = lzhanglap)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = LAPTOP)
)
)
Programming Environment
Programming Environment
Tools
SQL Navigator (server side only)
DBArtisan
etc.
Programming Environment
Introduction to PL/SQL
What Is PL/SQL?
Introduction to PL/SQL
What Is PL/SQL?
PL/SQL, Oracles procedural extension of
SQL, is an advanced fourth-generation
programming language (4GL). It offers modern
features such as data encapsulation,
overloading, collection types, exception
handling, and information hiding. PL/SQL also
offers seamless SQL access, tight integration
with the Oracle server and tools, portability,
and security. Oracle PL/SQL Users Guide
and Reference
Introduction to PL/SQL
Readings: Chapter 4
What Is PL/SQL?
1. Design features of programming languages.
2. Data manipulation and query statements (i.e.,
SQL) capabilities (are included within
procedural units of code.)
3. Interactions between programming language
constructs and SQL
Introduction to PL/SQL
Readings: Chapter 4
FLOW
CONTROL
ERROR
HANDLING
DECLARE
VARIABLES
qty_on_hand NUMBER(5);
BEGIN
SELECT quantity INTO qty_on_hand FROM inventory
WHERE product = TENNIS RACKET FOR UPDATE OF quantity;
IF qty_on_hand > 0 THEN -- check quantity
UPDATE inventory SET quantity = quantity - 1
SQL STATEMENT
WHERE product = TENNIS RACKET;
INSERT INTO purchase_record
VALUES (Tennis racket purchased, SYSDATE);
ELSE
INSERT INTO purchase_record
VALUES (Out of tennis rackets, SYSDATE);
END IF;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
INSERT INTO temp VALUES (NULL, NULL, Not found);
COMMIT;
END;
Introduction to PL/SQL
Introduction to PL/SQL
Block Structure
[BLOCK HEADER
IS]
[DECLARE
-- declarations]
BEGIN
-- statements
[EXCEPTION
-- handlers]
END;
Introduction to PL/SQL
part_no NUMBER(4);
in_stock BOOLEAN;
Variables can have any SQL data types such as CHAR, DATE,
or NUMBER, or any PL/SQL data type such as BOOLEAN or
BINARY_INTEGER. You can also declare nested tables,
variable-size arrays (varrays for short), and records using the
TABLE, VARRAY, and RECORD composite datatypes.
Introduction to PL/SQL
Introduction to PL/SQL
Introduction to PL/SQL
Introduction to PL/SQL
Cursors
Oracle uses work areas to execute SQL statements
and store processing information. A cursor lets you
name a work area and access its stored information.
You can use the OPEN, FETCH, and CLOSE
statements or the Cursor For loops to control
cursors.
cursor
1
2
3
4
5
Jane
John
Jack
Jill
Jesse
Doe
Doe
Doe
Doe
Doe
current row
Introduction to PL/SQL
Attributes
emp.name%TYPE;
NUMBER(20, 2);
v_data%TYPE;
employee%ROWTYPE;
Introduction to PL/SQL
Attributes
Advantages:
1. dont need to know the exact data type of the
underlying database column, and
2. if the data type of the underlying database
column or the variable an attribute references got
changed, the data type of the attribute changes
accordingly at run time.
Introduction to PL/SQL
Control Structures
Conditional Control
1. IF-THEN-ELSE statements
2. CASE statements
Iterative Control
1. Loop statements
2. For-loop statements
3. While-loop statements
Sequential Control
1. Goto statements
CSC452 Database Programming
Introduction to PL/SQL
Modularity
Introduction to PL/SQL
Block Structure
[BLOCK HEADER
IS]
[DECLARE
-- declarations]
BEGIN
-- statements
[EXCEPTION
-- handlers]
END;
Introduction to PL/SQL
Modularity
Introduction to PL/SQL
Collections
The collection types TABLE and VARRAY works like arrays. A
collection is an ordered group of elements, all of the same
type. Each element has a unique subscript that determines its
position in the collection.
Records
You can use the %ROWTYPE attribute to declare a record
that represents a row in a table or a row fetched from a
cursor. You can declare fields of your own with user-defined
records. You can also nest records. That is, a record can be
a component of another record.
CSC452 Database Programming
Introduction to PL/SQL
Object Types
In PL/SQL, object-oriented programming is based on object
types. An object type encapsulates a data structure along with
the functions and procedures needed to manipulate the data.
The variables that form the data structure are called
attributes. The functions and procedures that characterize the
behavior of the object type are called methods.
CREATE TYPE Bank_Account AS OBJECT (
acct_number INTEGER(5),
balance REAL,
status VARCHAR2(10),
MEMBER PROCEDURE open (amount IN REAL),
MEMBER PROCEDURE verify_acct (num IN INTEGER),
MEMBER PROCEDURE close (num IN INTEGER, amount OUT REAL),
MEMBER PROCEDURE deposit (num IN INTEGER, amount IN REAL),
MEMBER PROCEDURE withdraw (num IN INTEGER, amount IN REAL),
MEMBER FUNCTION curr_bal (num IN INTEGER) RETURN REAL
);
Introduction to PL/SQL
Introduction to PL/SQL
DECLARE
...
comm_missing EXCEPTION; -- declare exception
BEGIN
...
IF commission IS NULL THEN
RAISE comm_missing; -- raise exception
END IF;
bonus := (salary * 0.10) + (commission * 0.15);
EXCEPTION
WHEN comm_missing THEN ... -- process the exception
Introduction to PL/SQL
PL/SQL Architecture
PL/SQL can reside in two environments:
1. Oracle Database Server
2. Oracle Tools (forms and reports)
Client
Side
SQL*Plus
PL/SQL
Programs
PL/SQL
Programs
Server
Oracle Server
Side
PL/SQL Engine
Procedural
Statement
Executor
SQL
PL/SQL
Procedural
Block
PL/SQL
Programs
SQL
SQL
Statement
Executor
Introduction to PL/SQL
Advantages of PL/SQL
1.
2.
3.
4.
5.
6.
7.
Introduction to PL/SQL
Client
SQL
Server
SQL
Client
Client
SQL
IF THEN
SQL
ELSE
SQL
END IF;
SQL
RPC
Server
Server
(stored
PL/SQL)
Introduction to PL/SQL
PL/SQL is Oracle's procedural extension to industrystandard SQL. PL/SQL naturally, efficiently, and
safely extends SQL. Its primary strength is in
providing a server-side, stored procedural language
that is easy-to-use, seamless with SQL, robust,
portable, and secure. Thus, it offers a platform for
robust, high-performing enterprise applications, not
only for our Fortune 500 customers
Introduction to PL/SQL
To PL/SQL or Not To PL/SQL?
PL/SQL:
Data centric and tightly integrated into the database
Proprietary to Oracle and difficult to port to other database
systems
Processing is done inside the database engine so it is faster
in PL/SQL than in Java
PL/SQL is a better solution for SQL-intensive applications.
PL/SQL is optimized for SQL, and so SQL operations are
faster in PL/SQL than in Java.
Java:
Open standard, not proprietary to Oracle
Incurs some data conversion overhead between the Database
and Java type systems
is a better solution for logic-intensive applications.
CSC452 Database Programming