Sunteți pe pagina 1din 13

Section 1 Lesson 1: Introduction to PL/SQL

Vocabulary

Identify the vocabulary word for each definition below:
PL/SQL -Oracle Corporations standard procedural language for relational
databases which allows basic program logic and control flow to be combined with SQL
statements


Try it/Solve it

1). Circle the programming language meeting the criteria
Criteria
3GL
4GL
Is proprietary to Oracle Corporation
Nonprocedural
Procedural
Is ANSI-compliant
Language
PL/SQL
SQL
PLSQL
SQL
PLSQL
SQL

2). In your own words, describe why a procedural language like PL/SQL is needed
Rezolvare : este mai simplu sa se scrie un bloc PLSQL decat care sa substituie un
numar mare de integorari SQL

3). Define a procedural construct.
Rezolvare: - Unitati de program reutilizabile care sunt scrise o data si executate
de multe ori

4). List some examples of procedural constructs in PL/SQL.
Rezolvare : variabile, cursoare, constante i tipuri ,instructiuni de decizie si
repetitive (bucle)

5). In the following code, identify and circle examples of these procedural constructs:
variable, conditional control, SQL statement




DECLARE
v_first_name varchar2(40);
v_last_name varchar2(40); variable,
v_first_letter varchar2(1);
BEGIN
SELECT first_name, last_name into v_first_name, v_last_name
FROM students conditional
WHERE student_id=105; control
v_first_letter := get_first_letter(last_name);
IF 'N' > 'v_first_letter' THEN
DBMS_OUTPUT.PUT_LINE('The last name for: '||first_name||'
'||last_name||' is between A and M'); SQL
ELSE statement

DBMS_OUTPUT.PUT_LINE('The last name for: '||first_name||'
'||last_name||' is between N and Z');
END IF;
END;




Section 1 Lesson 2: Benefits of PL/SQL

Vocabulary

Identify the vocabulary word for each definition below:
Portability -The ability for PL/SQL programs to run anywhere an Oracle server
runs.
Block -The basic unit of PL/SQL programs-also known as modules.
Exception -An error that occurs in the database or in a users program

Try it/Solve it

1). Why is it more efficient to combine SQL statements into PL/SQL blocks?
Rezolvare: pentru o mai buna organizare a codului, blocurile pot constitui module
de sine statatoare ce pot fi reutilizate sau nu

2). Why is it beneficial to use PL/SQL with an Oracle database? List at least three
reasons.

Rezolvare: -portabilitate
-modularitate
-tratarea excepiilor

3). How is PL/SQL different from C and Java? List three differences.
Rezolvare: - foloseste baza de date Oracle sau instrumente Oracle
-orientat care obiect
-portabilitatea

4). List three examples of what you can build with PL/SQL code.
Rezolvare: -programe puternice
-blocuri PL/SQL

(3) CREATING PL/SQL BLOCKS

Vocabulary
Unnamed blocks of code not stored in the database and do not exist after they are
executed blocuri anonime
A program that computes and returns a value functie
Named PL/SQL blocks that are stored in the database and can be declared as procedures
or functions subprograme
Software that checks and translates programs written in high-level programming
languages into binary code to execute compilator
A program that performs an action and does not have to return a value procedura


Try it/Solve it
1). Complete the following chart defining the syntactical requirements for a PL/SQL
block:
Optional or Mandatory? Describe what is included in this section
DECLARE O declaratii de variabile, cursoare etc
BEGIN M instructiuni SQL si PL/SQL
EXCEPTION O se specifica ce trebuie sa se intample
atunci cand apare o situatie anormala
END; M

2). Which of the following PL/SQL blocks executes successfully? For the blocks that fail,
explain why they fail
A. BEGIN
END;
nu exista nicio instructiune
B. DECLARE
amount INTEGER(10);
END;
lipseste cuvantul cheie BEGIN, inceputul sectiunii executabile, sectiune
ce este obligatorie
C. DECLARE
BEGIN
END;
nu exista nicio instructiune
D. DECLARE
amount NUMBER(10);
BEGIN
DBMS_OUTPUT.PUT_LINE(amount);
END;
Correct

3). Fill in the blanks:
A.PL/SQL blocks that have no names are called _anonymous_blocks_.
B. _Functions_ and _procedures_ are named blocks and are stored in the database.
4). In Application Express, create and execute a simple anonymous block that outputs
Hello World.
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello world.');
END;

Hello world.
Statement processed.
0.05 seconds
Extension Exercise
1) Create and execute a simple anonymous block that does the following:
Declares a variable of datatype DATE and populates it with the date that is six
months from today
Outputs In six months, the date will be: <insert date>.

DECLARE
data DATE:= ADD_MONTHS(sysdate,6);
BEGIN
DBMS_OUTPUT.PUT_LINE('In six months will be: '||data);
END;

In six months will be: 24/Aug/2014
Statement processed.


SECTION 2


(1) USING VARIABLES IN PL/SQL


Vocabulary
Identify the vocabulary word for each definition below:
Used for storage of data and manipulation of stored values. variabile
values passed to a program by a user or by another program to customize the
program valori

Try It/Solve It
1). Fill in the blanks.
A. Variables can be assigned to the output of a _subprogram_.
B. Variables can be assigned values in the _executable_ section of a PL/SQL block.
C. Variables can be passed as _parameters_ to subprograms.

2). Identify valid and invalid variable declaration and initialization:
number_of_copies PLS_INTEGER; valid
printer_name CONSTANT VARCHAR2(10); invalid
deliver_to VARCHAR2(10):=Johnson; valid
by_when DATE:= SYSDATE+1; invalid

3).Examine the following anonymous block and choose the appropriate statement.
DECLARE
fname VARCHAR2(20);
lname VARCHAR2(15) DEFAULT 'fernandez';
BEGIN
DBMS_OUTPUT.PUT_LINE( FNAME ||' ' ||lname);
END;
A. The block will execute successfully and print fernandez.
B. The block will give an error because the fname variable is used without initializing.
C. The block will execute successfully and print null fernandez.
D. The block will give an error because you cannot use the DEFAULT keyword to
initialize a variable of the VARCHAR2 type.
E. The block will give an error because the FNAMEvariable is not declared.

4). In Application Express:
A. Create the following function:
CREATE FUNCTION num_characters (p_string IN VARCHAR2)
RETURN INTEGER AS
v_num_characters INTEGER;
BEGIN
SELECT LENGTH(p_string) INTO v_num_characters
FROM dual;
RETURN v_num_characters;
END;

Function created.

B. Create and execute the following anonymous block:
DECLARE
v_length_of_string INTEGER;
BEGIN
v_length_of_string := num_characters('Oracle Corporation');
DBMS_OUTPUT.PUT_LINE(v_length_of_string);
END;

18

Statement processed.

5). Write an anonymous block that uses a country name as input and prints the highest and
lowest elevations for that country. Use the wf_countries table. Execute your block three
times using United States of America, French Republic, and Japan.
(A) DECLARE
v_high INTEGER;
v_low INTEGER;
BEGIN
SELECT LOWEST_ELEVATION, HIGHEST_ELEVATION INTO v_low,
v_high FROM wf_countries WHERE COUNTRY_NAME = 'United States of
America';
DBMS_OUTPUT.PUT_LINE(v_high||' '||v_low);
END;

6194 -86

Statement processed.

(B) DECLARE
v_high INTEGER;
v_low INTEGER;
BEGIN
SELECT LOWEST_ELEVATION, HIGHEST_ELEVATION INTO v_low,
v_high FROM wf_countries WHERE COUNTRY_NAME = French Republic
';
DBMS_OUTPUT.PUT_LINE(v_high||' '||v_low);
END;


4807 -2

Statement processed.


(C) DECLARE
v_high INTEGER;
v_low INTEGER;
BEGIN
SELECT LOWEST_ELEVATION, HIGHEST_ELEVATION INTO v_low,
v_high FROM wf_countries WHERE COUNTRY_NAME = Japan';
DBMS_OUTPUT.PUT_LINE(v_high||' '||v_low);
END;
3776 -4
Statement processed.


(2) RECOGNIZING PL/SQL LEXICAL UNITS


Vocabulary
Identify the vocabulary word for each definition below:
An explicit numeric, character string, date, or Boolean value that is not represented by
an identifier. lexical unit
Symbols that have special meaning to an Oracle databasedelimitatori
Words that have special meaning to an Oracle database and cannot be used as
identifiers. cuvinte rezervate
Describe the purpose and use of each code segment and are ignored by PL/SQL.
comentarii
Building blocks of any PL/SQL block and are sequences of characters including
letters, digits, tabs, returns, and symbols. lexical units
A name, up to 30 characters in length, given to a PL/SQL object identificator


Try It/Solve It
1). Fill in the blanks.
A. An __identifier__ is the name given to a PL/SQL object.
B. A __reserved__word__ is a word that has special meaning to the Oracle
database.
C. A __delimitator__ is a symbol that has special meaning to the Oracle
database.
D. A __literal__ is an explicit numeric, character string, date, or Boolean value
that is not represented by an identifier.
E. A__comment___ explains what a piece of code is trying to achieve.

2.Identify each of the following identifiers as valid or invalid. If invalid, specify why.
Identifier

Valid
(X)
Invalid
(X)
Why Invalid?

Today X
Last name X
todays_date X
number_of_days_in_february_this_ X >30
year X Cuv.rez.
Isleap$year X
#number X Incepe cu #
NUMBER# X


Number1to7 X

3. Identify the reserved words in the following list.
Word Reserved?
create Y
make N
table Y
seat N
alter Y
rename Y
row Y
number Y
web N
Value Lexical Unit
4.What kind of lexical unit
(for example Reserved
word, Delimiter,
Literal, Comment) is
each of the following?










(3) RECOGNIZING DATA TYPES


Vocabulary
Identify the vocabulary word for each definition below:
Store large blocks of single-byte or fixed width multi-byte NCHAR data in the
database. NCLOB
Hold values, called locators, that specify the location of large objects (such as graphic
images) that are stored out of line. LOB
Hold a single value with no internal components. scalar
Store large unstructured or structured binary objects. BLOB
Contain internal elements that are either scalar (record) or composite (record and
table) composite
Store large binary files outside of the database. BFILE
Hold values, called pointers, that point to a storage location. references
A schema object with a name, attributes, and methods. object
SELECT Reserved word
:= Delimiter
'TEST' Literal
FALSE Literal
-- new process Comment
FROM Reserved word
/*select the country with the
highest elevation */

Comment

V_test Identifier
4.09 Literal
Store large blocks of character data in the database. CLOB

Try It / Solve It
1). In your own words, describe what a data type is and explain why it is important.
un tip de data este dat de multimea de constrangeri si de un interval de
valori intre care se poate situa o variabila ce apartine tipului de data respectiv
2.Match the data type category (LOB, Scalar, Composite, Reference, and Object) with
the appropriate definition. Each data type may be used more than once.
Description Data Type
Stores a large amount of data LOB
Has internal components that can be
manipulated individually

Composite
Has a name, attributes, and methods Object
Includes CLOBs, BLOBs, BFILEs, and
NCLOBs
LOB
Has no internal components Scalar
Includes TABLEs, RECORDs, NESTED
TABLEs, and VARRAYs
Composite
Includes TIMESTAMP, DATE,
BINARY_INTEGER, LONG, LONG RAW,
and BOOLEAN

Scalar

Holds values, called pointers, that point to
a storage location
References

3). Enter the data type category for each value into the Data Type Category column.
In the Data Type column, enter a specific data type that can be used for the value. The
first one has been done for you.




Value

Data Type Data Type



Category
Switzerland Scalar VARCHAR2
100.20 Scalar BINARY_FLOAT
1053 Scalar BINARY_INTEGER
12-DEC-2005 Scalar DATE
False Scalar BOOLEAN
Index Last_name

Composite



TABLE


1 'Newman'
2 'Raman'
3 'Han'
A movie LOB BFILE
A soundbyte LOB BFILE
A picture LOB BLOB

S-ar putea să vă placă și