Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Oracle Pl SQL Cheatsheet for DBMS, Cheat Sheet of Database Management Systems (DBMS)

Symbols, Control Flow, Explicit Cursor Handling, Data Types, PL SQL Module types, Module Sections or Blocks, Package Syntax and Common exceptions are the headings in cheat sheet

Typology: Cheat Sheet

2020/2021

Uploaded on 04/26/2021

loveu
loveu šŸ‡ŗšŸ‡ø

4.5

(20)

297 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Symbols
; Semicolon. Statement terminator
% Percent sign
Attribute indicator (cursor attributes like
%ISOPEN and indirect declaration
attributes like %ROWTYPE). Also used as
multibyte wildcard symbol, as in SQL.
_ Single underscore Single-byte wildcard symbol, as in SQL
: Colon Host variable indicator, such as :block.item
in Oracle Forms
** Double asterisk Exponentiation operator
< > and != Not equals"
|| Double vertical
bar Concatenation operator
<< and >> Label delimiters
:= Assignment operator
=> Association operator for positional notation
-- Double dash: single-line comment
indicator
/* and */ Beginning and ending multiline comment
block delimiters
Data Types.
Database types
NUMBER
CHAR(N),
VARCHAR2(N)
DATE
LONG
LONG RAW
ROWID
MLSLABEL
Definition
Used to store any number
Used for storing text
Oracle system date
Stores large blocks of text
Stores large blocks of binary data
Smaller binary data store
Uesd for row identifier
Security label
Non database types.
DEC, DECIMAL, REAL, DOUBLE-
PRECISION, INTEGER, INT, SMALLINT,
NATURAL, POSITIVE, NUMERIC, BINARY-
INTEGER, CHARACTER, VARCHAR,
BOOLEAN, TABLE, RECORD
PLSQL Module types
Procedure A non-formal function that can accept
paremeters via value or reference. Similar
in form to a function.
Function A classical function that returns one value.
Usually contains declaration, execution
and exception sections.
Package A library, consisting of a specification with
function/prototype signatures, and a body
with actual code. eg
Trigger Code attached to a table that fires on
certian conditions.
Module Sections or Blocks
Variable Declaration DECLARE
employee-id employee.empid%TYPE, pi
Control Flow
IF..THEN..ELSE..ENDIF; As usual.
LOOP ..
IF (condition) THEN
EXIT END IF;
.. END LOOP:
Equivalent to if (conition) then
b
WHILE cond LOOP..END
LOOP; while () {};
FOR var IN n..m LOOP
.. END LOOP; for thing in range(n,m) {}
EXECUTE
function_name; Function call FUNCTION name
(parameter type,..) ..body.. EN
D
Cursor for.
Opens cursor, loops across until
%NOTFOUND.
FOR variables IN cursor LOOP..
E
LOOP;
Explicit Cursor Handling
Implict cursor
named by
developer.
T
hink of it as a select statement th
name.
Implict cusror is
called SQL IF SQL%NOTFOUND THEN ..
Declaring an explicit
cursor.
DECLARE CURSOR employee_crsr
I
SELECT empid, salary FROM empl
o
BEGIN ..
Executing a cursor
OPEN employee_cursor
LOOP
FETCH employee_cursor INTO my
_
my_salary;
EXIT WHEN employee_crsr%NOTF
O
..do stuff..
ENDLOOP;
FETCH Obtains next record from cursor.C
a
into individual variables (as above
)
RECORD.
Declaring an explicit
cursor using a
record.
T
YPE t_emp IS RECORD (T_Salary
t_empid number);
my_emprec t_emp;
CURSOR employee_crsr IS
SELECT empid, salary
FROM employee;
Executing explicit
cursror using record.
OPEN employee_cursr;
LOOP
FETCH emloyee_crsr INTO my_em
p
EXIT WHEN employee_crsr%NOTF
O
IF my_emprec.t_empid ..
Cursor Parameters.
Declarin
g
parameters to be used a
t
time.
DECLARE .. CURSOR
employee_crsr(low_end VARCHAR
2
high_end VARCHAR2) IS
SELECT empid, salary FROM empl
o
pf3

Partial preview of the text

Download Oracle Pl SQL Cheatsheet for DBMS and more Cheat Sheet Database Management Systems (DBMS) in PDF only on Docsity!

Symbols

; Semicolon. Statement terminator

% Percent sign

Attribute indicator (cursor attributes like %ISOPEN and indirect declaration attributes like %ROWTYPE). Also used as multibyte wildcard symbol, as in SQL. _ Single underscore Single-byte wildcard symbol, as in SQL

: Colon

Host variable indicator, such as :block.item in Oracle Forms ** Double asterisk Exponentiation operator < > and != Not equals" || Double vertical bar

Concatenation operator

<< and >> Label delimiters := Assignment operator => Association operator for positional notation

--

Double dash: single-line comment indicator

/* and */ Beginning and ending multiline comment block delimiters

Data Types.

Database types NUMBER CHAR(N), VARCHAR2(N) DATE LONG LONG RAW ROWID MLSLABEL

Definition Used to store any number Used for storing text Oracle system date Stores large blocks of text Stores large blocks of binary data Smaller binary data store Uesd for row identifier Security label

Non database types.

DEC, DECIMAL, REAL, DOUBLE-

PRECISION, INTEGER, INT, SMALLINT,

NATURAL, POSITIVE, NUMERIC, BINARY-

INTEGER, CHARACTER, VARCHAR,

BOOLEAN, TABLE, RECORD

PLSQL Module types

Procedure

A non-formal function that can accept paremeters via value or reference. Similar in form to a function.

Function

A classical function that returns one value. Usually contains declaration, execution and exception sections.

Package

A library, consisting of a specification with function/prototype signatures, and a body with actual code. eg

Trigger

Code attached to a table that fires on certian conditions.

Module Sections or Blocks

Variable Declaration

DECLARE

employee-id employee.empid%TYPE, pi

Control Flow

IF..THEN..ELSE..ENDIF; As usual. LOOP .. IF (condition) THEN EXIT END IF; .. END LOOP:

Equivalent to if (conition) then b

WHILE cond LOOP..END LOOP;

while () {};

FOR var IN n..m LOOP .. END LOOP; for thing in range(n,m) {} EXECUTE function_name;

Function call FUNCTION name (parameter type,..) ..body.. END

Cursor for.

Opens cursor, loops across until %NOTFOUND. FOR variables IN cursor LOOP..E LOOP;

Explicit Cursor Handling

Implict cursor named by developer.

Think of it as a select statement th name.

Implict cusror is called SQL IF SQL%NOTFOUND THEN ..

Declaring an explicit cursor.

DECLARE CURSOR employee_crsr I SELECT empid, salary FROM emplo BEGIN ..

Executing a cursor

OPEN employee_cursor LOOP FETCH employee_cursor INTO my_ my_salary; EXIT WHEN employee_crsr%NOTFO ..do stuff.. ENDLOOP;

FETCH

Obtains next record from cursor.Ca into individual variables (as above) RECORD.

Declaring an explicit cursor using a record.

TYPE t_emp IS RECORD (T_Salary t_empid number); my_emprec t_emp; CURSOR employee_crsr IS SELECT empid, salary FROM employee;

Executing explicit cursror using record.

OPEN employee_cursr; LOOP FETCH emloyee_crsr INTO my_emp EXIT WHEN employee_crsr%NOTFO IF my_emprec.t_empid ..

Cursor Parameters.

Declaring parameters to be used at time. DECLARE .. CURSOR employee_crsr(low_end VARCHAR high_end VARCHAR2) IS SELECT empid, salary FROM emplo

CONSTANT number := 3.14, ratio REAL,.. BEGIN..

Executable Section

.. BEGIN select * into my_employee where employee.emid = 42; END; ..

Exception Handler.

END;

EXCEPTIONS .. END;

Package Syntax

Specification

PACKAGE package_name IS [ declarations of variables and types ] [ specifications of cursors ] [ specifications of modules ] END [ package_name ];

Body

PACKAGE BODY package_name IS [ declarations of variables and types ] [ specification and SELECT statement of cursors ] [ specification and body of modules ] [ BEGIN executable statements ] [ EXCEPTION exception handlers ] END [ package_name ];

Filename Extensions

General SQL*Plus script .sql Testing script .tst Stored procedure .sp Stored function .sf Stored package body spb Stored package specification .sps

Implict cursor attributes.

%NOTFOUND True if fetch did not return row. %ROWCOUNT Number of rows processed by this cursor %FOUND Opposite of %NOTFOUND %ISOPEN If currently open for processing then true.

Transaction processing

Same Options as SQL COMMIT, ROLLBACK, SAVEPOINT Transaction begins at execution of first change of data.

Rollbacks go to last COMMIT or SAVE_POINT

DBMS_TRANSACTION A package with functions for transaction control.

Exception Handling

Predefined

Relates to an oracle error. No need to invoke. Just catch. EXCEPTION WHEN NO_DATA_FOUN THEN DBMS_OUTPUT.PUT_LINE('No data found');

User defined.

Need to be declared, tested and handled in their respective blocks. DECLARE My_salary_null EXCEPTION; .. EBGIN.. IF my_emp_record.salary IS NULL THEN RAISE my_salary_null; END IF; EXCEPTION.. WHEN my_salary_null THEN DBMS_OUTPUT.PUT_LINE('Salary column was null for employee'); END

WHERE substr(lastname,1,1) BETW UPPER(low_end) AND UPPER(high_

Common exceptions

INVALID_CURSOR Occurs when you attempt to c cursor that has not been open

CURSOR_ALREADY_OPEN

Occurs when you attempt to o cursor the second time

DUP_VAL_ON_INDEX Unique or primary key constraviolation

TOO_MANY_ROWS

More than one row was opbtai a single row subquery, or anot context when Oracle was expe one row. ZERO_DIVIDE An attempt to divide by zero.

ROWTYPE_MISMATCH An attempt to FETCH a cursor incompatible variable type.

INVALID_NUMBER

An char type was referenced a number. OTHERS Special catchall exception.

Pragmas

EXCEPTION_INIT

Tells the compiler to associate a particular error number with an identifier you have declared as exception in your program.

RESTRICT_REFERENCES

Tells the compiler the purity lev (freedom from side effects) of a packaged program.

SERIALLY_REUSABLE

Tells the PL/SQL runtime engin package-level data should not p between references to that data Chapter 25, Tuning PL/SQL Applications for more informati