Week 2 (Introduction To PL SQL)
Week 2 (Introduction To PL SQL)
SQL*Plus Environment
Agenda
What is SQL*Plus?
What is PL/SQL?
PL/SQL Constructs
2
What is SQL*Plus?
list or list #
change
del
append
input
Default character: |
Set headsep !
10
set linesize
set pagesize
set newpage
run (/)
start (@)
save
store
12
13
ttitle off
btitle off
clear columns
clear breaks
clear computes
14
User_Constraints
User_Cons_Columns
SELECT column_name
FROM user_cons_columns
WHERE constraint_name=SYS_C0008791;
Retrieving constraints defined by the user
WHERE CONSTRAINT_NAME NOT LIKE '%SYS%';
15
user_sequences
user_errors
What Is PL/SQL?
17
PL/SQL Constructs
Block Structure
Error Handling
Variables and Types
Conditionals
Looping Constructs
Cursors
18
Introduction to PL / SQL
Chapter 1
What Is PL / SQL
20
File 3gl_4gl.sql
Demonstrates both SQL and PL/SQL
DECLARE
commands
v_NewMajor VARCHAR2(10) := 'History';
v_FirstName VARCHAR2(10) := 'Scott';
v_LastName VARCHAR2(10) := 'Urman';
BEGIN
UPDATE students
SET major = v_NewMajor
WHERE first_name = v_FirstName
AND last_name = v_LastName;
IF SQL%NOTFOUND THEN
INSERT INTO students (ID, first_name, last_name, major)
VALUES (student_sequence.NEXTVAL, v_FirstName, v_LastName,
v_NewMajor);
END IF;
END;
/
21
Client-Server Model
Features of PL / SQL
Block Structure
Error Handling
Variables and Types
Looping Constructs
Cursors
23
Features of PL / SQL
Block Structure
Declarative section
Executable section
Exception handling
Features of PL / SQL
Error Handling
25
File Error.sql
Illustrates an exception handler
DECLARE
v_ErrorCode NUMBER;
v_ErrorMsg VARCHAR2(200);
v_CurrentUser VARCHAR2(8);
v_Information VARCHAR2(100);
BEGIN
/* Code which processes some data here */
NULL;
-- (continued)
26
File Error.sql
Illustrates an exception handler
EXCEPTION
WHEN OTHERS THEN
v_ErrorCode := SQLCODE;
v_ErrorMsg := SQLERRM;
v_CurrentUser := USER;
v_Information := 'Error encountered on ' ||
TO_CHAR(SYSDATE) || ' by database user ' || v_CurrentUser;
INSERT INTO log_table (code, message, info)
VALUES (v_ErrorCode, v_ErrorMsg, v_Information);
END;
/
27
Features of PL / SQL
Variables and Types
Features of PL / SQL
Looping Constructs
Simple loop
Numeric For loop
While loop
29
File SimpleLoop.sql
Demonstrates a simple loop
DECLARE
v_LoopCounter BINARY_INTEGER := 1;
BEGIN
LOOP
INSERT INTO temp_table (num_col)
VALUES (v_LoopCounter);
v_LoopCounter := v_LoopCounter + 1;
EXIT WHEN v_LoopCounter > 50;
END LOOP;
END;
/
30
File NumericLoop.sql
Demonstrates a numeric FOR loop
BEGIN
FOR v_LoopCounter IN 1..50 LOOP
INSERT INTO temp_table (num_col)
VALUES (v_LoopCounter);
END LOOP;
END;
/
31
Features of PL / SQL
Cursors
32
File CursorLoop.sql
Demonstrates a cursor fetch loop
DECLARE
v_FirstName VARCHAR2(20);
v_LastName VARCHAR2(20);
CURSOR c_Students IS
SELECT first_name, last_name
FROM students;
BEGIN
OPEN c_Students;
LOOP
FETCH c_Students INTO v_FirstName, v_LastName;
EXIT WHEN c_Students%NOTFOUND;
/* Process data here */
END LOOP;
CLOSE c_Students;
END;
33
File Conditional.sql
Illustrates a conditional statement
DECLARE
v_TotalStudents NUMBER;
BEGIN
SELECT COUNT(*)
INTO v_TotalStudents
FROM students;
-- (continued)
34
File Conditional.sql
Illustrates a conditional statement
IF v_TotalStudents = 0 THEN
INSERT INTO temp_table (char_col)
VALUES ('There are no students registered');
ELSIF v_TotalStudents < 5 THEN
INSERT INTO temp_table (char_col)
VALUES ('There are only a few students registered');
ELSIF v_TotalStudents < 10 THEN
INSERT INTO temp_table (char_col)
VALUES ('There are a little more students registered');
ELSE
INSERT INTO temp_table (char_col)
VALUES ('There are many students registered');
END IF;
END;
/
35
File PrintStudents.sql
Illustrates a stored procedure
CREATE OR REPLACE PROCEDURE PrintStudents(
p_Major IN students.major%TYPE) AS
CURSOR c_Students IS
SELECT first_name, last_name
FROM students
WHERE major = p_Major;
BEGIN
FOR v_StudentRec IN c_Students LOOP
DBMS_OUTPUT.PUT_LINE(v_StudentRec.first_name || ' ' ||
v_StudentRec.last_name);
END LOOP;
END;
/
36
File PrintStudents.sql
Illustrates a stored procedure
BEGIN
PrintStudents ('Computer Science');
END;
/
37
Online Code
38
39
File tables.sql
PROMPT student_sequence...
DROP SEQUENCE student_sequence;
CREATE SEQUENCE student_sequence
START WITH 10000
INCREMENT BY 1;
PROMPT students table...
DROP TABLE students CASCADE CONSTRAINTS;
CREATE TABLE students (
id
first_name
VARCHAR2(20),
last_name
VARCHAR2(20),
major
VARCHAR2(30),
current_credits NUMBER(3)
);
41
Example Tables
classes
CREATE TABLE classes
(
department
CHAR (3),
course
NUMBER (3),
description
VARCHAR2 (2000),
max_students
NUMBER (3),
current_students
NUMBER (3),
num_credits
NUMBER (1),
room_id
NUMBER (5),
CONSTRAINT classes_department_course
PRIMARY KEY (department, course),
CONSTRAINT classes_room_id
FOREIGN KEY (room_id) REFERENCES rooms (room_id)
);
42
Example Tables
debug_table
CREATE TABLE debug_table
(
linecount
NUMBER,
debug_str
VARCHAR2 (100)
);
43
Example Tables
exception_view
CREATE VIEW exception_view AS
SELECT exception exception_description,
date_occurred
FROM exception_table;
44
Example Tables
log_table
CREATE TABLE
(
code
message
info
);
log_table
NUMBER,
VARCHAR2 (200),
VARCHAR2 (100)
45
Example Tables
major_stats
CREATE TABLE major_stats
(
major
VARCHAR2 (30),
total_credits NUMBER,
total_students NUMBER
);
46
Example Tables
registered_students
CREATE TABLE registered_students
(
student_id
NUMBER (5)
NOT NULL,
department CHAR (3)
NOT NULL,
course
NUMBER (3)
NOT NULL,
grade
CHAR (1),
CONSTRAINT rs_grade
CHECK (grade IN ('A', 'B', 'C', 'D', 'E')),
CONSTRAINT rs_student_id
FOREIGN KEY (student_id) REFERENCES students (id),
CONSTRAINT rs_department_course
FOREIGN KEY (department, course)
REFERENCES classes (department, course)
);
47
Example Tables
rooms
CREATE TABLE rooms
(
room_id
NUMBER (5)
PRIMARY KEY,
building
VARCHAR2 (15),
room_number NUMBER (4),
number_seats NUMBER (4),
description
VARCHAR2 (50)
);
48
Example Tables
RS_audit
CREATE TABLE RS_audit
(
change_type
CHAR (1)
NOT NULL,
changed_by
VARCHAR2 (8) NOT NULL,
timestamp
DATE
NOT NULL,
old_student_id
NUMBER (5),
old_department
CHAR (3),
old_course
NUMBER (3),
old_grade
CHAR (1),
new_student_id
NUMBER (5),
new_department
CHAR (3),
new_course
NUMBER (3),
new_grade
CHAR (1)
);
49
Example Tables
student_sequence
CREATE SEQUENCE student_sequence
START WITH 10000
INCREMENT BY 1;
50
Example Tables
students
CREATE TABLE students
(
id
NUMBER(5) PRIMARY KEY,
first_name
VARCHAR2 (20),
last_name
VARCHAR2 (20),
major
VARCHAR2 (30),
current_credits
NUMBER(3)
);
51
Example Tables
temp_table
CREATE TABLE temp_table
(
num_col
NUMBER,
char_col
VARCHAR2 (60)
);
52
In Conclusion
PL/SQL is a sophisticated
programming language used to
access an Oracle database
Procedural constructs are integrated
seamlessly with SQL, resulting in a
structured, powerful language
Combines flexibility of SQL with the
configure ability of a 3GL
53