17
SECTION 1 (1)INTRODUCTION Vocabulary Oracle Corporations standard procedural language for relational databases which allows basic program logic and control flow to be combined with SQL statements PL/SQL 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 PLSQL SQL PLSQL SQL PLSQL SQL PLSQL SQL PLSQL SQL PLSQL SQL 2). In your own words, describe why a procedural language like PL/SQL is needed pentru ca e mai simplu sa se scrie un bloc PLSQL decat care sa substituie un numar mare de integorari SQL 3). Define a procedural construct. unitati din program (bucati de cod) ce pot fi scrise o singura data si utilizate de mai multe ori 4). List some examples of procedural constructs in PL/SQL.

DumitruGeorgiana(tema1)

Embed Size (px)

DESCRIPTION

g

Citation preview

Page 1: DumitruGeorgiana(tema1)

SECTION 1

(1)INTRODUCTION

VocabularyOracle Corporations standard procedural language for relational databases which allows basic program logic and control flow to be combined with SQL statements PL/SQL

Try it/Solve it1). Circle the programming language meeting the criteriaCriteria3GL4GLIs proprietary to Oracle CorporationNonproceduralProceduralIs ANSI-compliant

LanguagePLSQL SQLPLSQL SQLPLSQL SQLPLSQL SQLPLSQL SQLPLSQL SQL

2). In your own words, describe why a procedural language like PL/SQL is needed

pentru ca e mai simplu sa se scrie un bloc PLSQL decat care sa substituie un numar mare de integorari SQL

3). Define a procedural construct. unitati din program (bucati de cod) ce pot fi scrise o singura data si

utilizate de mai multe ori4). List some examples of procedural constructs in PL/SQL.

variabile, cursoare, 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);v_first_letter varchar2(1);

Page 2: DumitruGeorgiana(tema1)

BEGIN SELECT first_name, last_name into v_first_name, v_last_name

FROM studentsWHERE student_id=105;

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');

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

END IF;END;

(2)BENEFITS OF PL/SQL

VocabularyThe ability for PL/SQL programs to run anywhere an Oracle server runs. PortabilityThe basic unit of PL/SQL programs-also known as modules. blockAn error that occurs in the database or in a user’s program exception

Try it/Solve it1). Why is it more efficient to combine SQL statements into PL/SQL blocks?

pentru ca aceste blocuri pot constitui module de sine statatoare ce pot fi reutilizate sau nu (o mai buna organizare a codului)2). Why is it beneficial to use PL/SQL with an Oracle database? List at least three reasons.

portabilitatemodularitate (prin crearea si utilizarea blocurilor)tratarea excepiilor

3). How is PL/SQL different from C and Java? List three differences. foloseste b.d. Oracle sau instrumente Oraclee mai usor de invatatportabilitatea

4). List three examples of what you can build with PL/SQL code.programe puterniceblocuri PL/SQL

Page 3: DumitruGeorgiana(tema1)

(3) CREATING PL/SQL BLOCKS

VocabularyUnnamed blocks of code not stored in the database and do not exist after they are executed blocuri anonimeA program that computes and returns a value functieNamed PL/SQL blocks that are stored in the database and can be declared as procedures or functions subprogrameSoftware that checks and translates programs written in high-level programming languages into binary code to execute compilatorA program that performs an action and does not have to return a value procedura

Try it/Solve it1). Complete the following chart defining the syntactical requirements for a PL/SQL block:

Optional or Mandatory? Describe what is included in this sectionDECLARE O declaratii de variabile, cursoare etcBEGIN M instructiuni SQL si PL/SQLEXCEPTION O se specifica ce trebuie sa se intample

atunci cand apare o situatie anormalaEND; M

2). Which of the following PL/SQL blocks executes successfully? For the blocks that fail, explain why they fail

A. BEGINEND; nu exista nicio instructiune

B. DECLAREamount INTEGER(10);

END; lipseste cuvantul cheie BEGIN, inceputul sectiunii executabile, sectiune ce este obligatorie

C. DECLAREBEGINEND;nu exista nicio instructiune

D. DECLAREamount NUMBER(10);

Page 4: DumitruGeorgiana(tema1)

BEGINDBMS_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.”

BEGINDBMS_OUTPUT.PUT_LINE('Hello world.');

END;

Hello world.Statement processed.0.05 seconds

Extension Exercise1) 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>.”

DECLAREdata DATE:= ADD_MONTHS(sysdate,6);

BEGINDBMS_OUTPUT.PUT_LINE('In six months will be: '||data);

END;

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

Page 5: DumitruGeorgiana(tema1)

SECTION 2

(1) USING VARIABLES IN PL/SQL

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

Try It/Solve It1). 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; validprinter_name CONSTANT VARCHAR2(10); invaliddeliver_to VARCHAR2(10):=Johnson; validby_when DATE:= SYSDATE+1; invalid

3).Examine the following anonymous block and choose the appropriate statement.

DECLAREfname VARCHAR2(20);lname VARCHAR2(15) DEFAULT 'fernandez';

BEGINDBMS_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.

Page 6: DumitruGeorgiana(tema1)

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_charactersFROM 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;

BEGINSELECT 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.

Page 7: DumitruGeorgiana(tema1)

(B) DECLAREv_high INTEGER;v_low INTEGER;

BEGINSELECT 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) DECLAREv_high INTEGER;v_low INTEGER;

BEGINSELECT 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 -4Statement processed.

(2) RECOGNIZING PL/SQL LEXICAL UNITS

VocabularyIdentify the vocabulary word for each definition below:An explicit numeric, character string, date, or Boolean value that is not represented by an identifier. lexical unitSymbols that have special meaning to an Oracle databasedelimitatoriWords that have special meaning to an Oracle database and cannot be used as identifiers. cuvinte rezervateDescribe the purpose and use of each code segment and are ignored by PL/SQL. comentariiBuilding blocks of any PL/SQL block and are sequences of characters including letters, digits, tabs, returns, and symbols. lexical units

Page 8: DumitruGeorgiana(tema1)

A name, up to 30 characters in length, given to a PL/SQL object identificator

Try It/Solve It1). 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   ‘ ‘today’s_date    X  ‘number_of_days_in_february_this_    X  >30year    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 Ymake Ntable Yseat Nalter Yrename Yrow Ynumber Yweb N

Page 9: DumitruGeorgiana(tema1)

4.What kind of lexical unit (for example Reserved word, Delimiter, Literal, Comment) is each of the following?

(3) RECOGNIZING DATA TYPES

VocabularyIdentify the vocabulary word for each definition below:Store large blocks of single-byte or fixed width multi-byte NCHAR data in the database. NCLOBHold values, called locators, that specify the location of large objects (such as graphic images) that are stored out of line. LOBHold a single value with no internal components. scalarStore large unstructured or structured binary objects. BLOBContain internal elements that are either scalar (record) or composite (record and table) compositeStore large binary files outside of the database. BFILEHold values, called pointers, that point to a storage location. referencesA schema object with a name, attributes, and methods. objectStore large blocks of character data in the database. CLOB

Value Lexical UnitSELECT   Reserved word:=   Delimiter'TEST'   LiteralFALSE   Literal-- new process   CommentFROM   Reserved word

/*select the country with the highest elevation */

 

  Comment

 V_test   Identifier4.09   Literal

Page 10: DumitruGeorgiana(tema1)

Try It / Solve It1). 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 TypeStores a large amount of data  LOBHas internal components that can be

manipulated individually

 

 CompositeHas a name, attributes, and methods  ObjectIncludes CLOBs, BLOBs, BFILEs, and

NCLOBs  LOB

Has no internal components  Scalar

Includes TABLEs, RECORDs, NESTEDTABLEs, and VARRAYs

 Composite

Includes TIMESTAMP, DATE,BINARY_INTEGER, LONG, LONG RAW,and BOOLEAN

 

 Scalar 

Holds values, called pointers, that point toa 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.

Page 11: DumitruGeorgiana(tema1)

Value

 

 

   Data Type

Category

Data Type

    

Page 12: DumitruGeorgiana(tema1)

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