Вы находитесь на странице: 1из 10

academy.oracle.

com

Database Programming with PL/SQL


2-1: Using Variables in PL/SQL Practice
Activities
Vocabulary
Identify the vocabulary word for each definition below:

Variables is a meaningful name which facilitates a programmer to store data


temporarily during the execution of code. It helps you to
manipulate data in PL/SQL programs. .
Parameters Values passed to a program by a user or by another program to
customize the program.

Try It / Solve It 1.
Fill in the blanks.

A. Variables can be assigned to the output of a ______Sub Program___________.

B. Variables can be assigned values in the ___________Declarative__________ 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; Invalid
by_when DATE := SYSDATE+1; Valid

, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their

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

DECLARE fname VARCHAR2(25);

Copyright © 2019 respective owners.


lname VARCHAR2(25) 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 FNAME variable 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;

, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their

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

Copyright © 2019 respective owners.


END;

OUTPUT : 18

5. Write an anonymous block that uses a country name as input and prints the highest and lowest
elevations for that country. Use the COUNTRIES table. Execute your block three times using United
States of America, French Republic, and Japan.

DECLARE
v_country_name varchar2(50):= 'United States of America';
v_lowest_elevation number(6);
v_highest_elevation number(6);
BEGIN
SELECT lowest_elevation, highest_elevation
INTO v_lowest_elevation, v_highest_elevation
FROM COUNTRIESWHERE country_name = v_country_name;
DBMS_OUTPUT.PUT_LINE('The lowest elevation for '||v_country_name||' is: '||v_lowest_elevation);
DBMS_OUTPUT.PUT_LINE('The highest elevation for '||v_country_name||' is: '||v_highest_elevation);
END

, Oracle and/r its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their

Copyright © 2019 respective owners.


academy.oracle.com

Database Programming with PL/SQL


2-2: Recognizing PL/SQL Lexical Units Practice Activities
Vocabulary
Identify the vocabulary word for each definition below:

LITERALS An explicit numeric, character string, date, or Boolean value that is not
represented by an identifier.

DELIMETERS Symbols that have special meaning to an Oracle database.

RESERVED WORDS Words that have special meaning to an Oracle database and cannot
be used as identifiers.

COMMENTS Describe the purpose and use of each code segment and are ignored
by PL/SQL.

LEXICAL UNITS Building blocks of any PL/SQL block and are sequences of characters
including letters, digits, tabs, returns, and symbols.

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

s. Other names may be trademarks of their

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliate respective owners.
Try It / Solve It Questions
1. Identify each of the following identifiers as valid or invalid. If invalid, specify why.

Identifier Valid Invalid Why Invalid?


(X) (X)

Today X

Last name X Contain space

today’s_date X Contain ‘

number_of_days_in_february_this_ X More than 30 alphabet


year

Isleap$year X

#number X # on begining

NUMBER# X

Number1to7 X

2. Identify the reserved words in the following list.


Word Reserved? Y/N

create Y

make N

table Y

seat N

alter Y

rename Y

row Y

number Y

web N

s. Other names may be trademarks of their

3. What kind of lexical unit (for example Reserved word, Delimiter, Literal, Comment) is each of the
following?
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliate respective owners.
Value Lexical Unit

SELECT Reserved Word

:= Delimiters

'TEST' Character Literal

FALSE Boolean Literal

-- new process Comments

FROM Reserved Word

/* select the country with the Comments


highest elevation */

v_test Literal

4.09 Numeric Literal

s. Other names may be trademarks of their

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliate respective owners.
academy.oracle.com

Database Programming with PL/SQL


2-3: Recognizing Data Types Practice
Activities
Vocabulary
Identify the vocabulary word for each definition below:

NCLOB
Store large blocks of single-byte or fixed width multi-byte NCHAR data
in the database.

LOB
Hold values, called locators, that specify the location of large objects
(such as graphic images) that are stored out of line.

SCALAR Hold a single value

BLOB Store large unstructured or structured binary objects.

COMPOSITE
Contain internal elements that are either scalar (record) or composite
(record and table)

BFILE Store large binary files outside of the database.

REFERENCE Hold values, called pointers, that point to a storage location.

OBJECT A schema object with a name, attributes, and methods.

CLOB Store large blocks of character data in the database.

Try It / Solve It
1. In your own words, describe what a data type is and explain why it is important.
Because data has a values which is we used for pl/sql

Copyright © 2019 respective owners.


2. Identify the three data type categories covered in this course. LOB, COMPOSITE, Scalar
, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their

3. Identify three data types covered in the Database Programming with SQL course. DATE,
NUMBER, BOOLEAN
4. What data type can be used in PL/SQL, but can’t be used to define a table column? Boolean
5. Which data type indicates a large data object that is stored outside of the database? LOB
6. Identify the data type category (LOB, Scalar, or Composite) for each data type. Each
category may be used more than once.

Data Type Data Type Category

CLOB LOB

VARCHAR2 Scalar

BLOB LOB

NUMBER Scalar

BFILE LOB

TIMESTAMP Scalar

NCLOB LOB

RECORD Composite

PLS_INTEGER Scalar

LONG Scalar

TABLE Composite

BOOLEAN Scalar

, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their

Copyright © 2019 respective owners.


7. Enter the data type category and the data type for each value. The first one has been done
for you.

Value Data Type Category Data Type

‘Switzerland’ Scalar VARCHAR2

Text of a resume SCALAR VARCHAR2

100.20 Scalar Number

A picture LOB BLOB

1053 Scalar Number

11-Jun-2016 Scalar Date

‘Computer science is the science Scalar Varchar2


of the 21st century.’

Index Last_name Composite Table

1 'Newman'

2 'Raman'

3 'Han'

A movie LOB BFILE

A sound byte LOB BFILE OR BLOB

FALSE Scalar BOOLEAN

Copyright © 2019 respective owners.


, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their

Copyright © 2019 respective owners.

Вам также может понравиться