Sunteți pe pagina 1din 11

academy.oracle.

com

Database Programming with


Nama : Baihaqi Suhaeri
PL/SQL 2-1: Using Variables in Kelas : C
Npm : 1402018017
PL/SQL Practice Activities Tugas 2

Vocabulary
Identify the vocabulary word for each definition below:

Used for storage of data and manipulation of stored values.


Variables
Values passed to a program by a user or by another program to cus-
Parameters
tomize 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


Invalid
printer_name CONSTANT VARCHAR2(10);
Invalid
deliver_to VARCHAR2(10) := Johnson;
Valid
by_when DATE := SYSDATE+1;

Copyright © 2019, 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
respective owners.
2

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

DECLARE
fname VARCHAR2(25);
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 vari-
able 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;
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;

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 Unit-
ed 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 wf_countries
WHERE 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;
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:

An explicit numeric, character string, date, or Boolean value that is not


Literals represented by an identifier.

Symbols that have special meaning to an Oracle database.


Delimiters
Words that have special meaning to an Oracle database and cannot
Reserved Words be used as identifiers.
Describe the purpose and use of each code segment and are ignored
Comments by PL/SQL.
Building blocks of any PL/SQL block and are sequences of characters
Lexile Units including letters, digits, tabs, returns, and symbols.

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


Identifiers

Copyright © 2019, 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
respective owners.
2

Try It / Solve It Questions


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

Valid Invalid
Identifier Why Invalid?
(X) (X)

Today
X
Last name
X Contain space
today’s_date
X Contain ‘
number_of_days_in_february_this_
year
X Contain space
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
3
web N
3. What kind of lexical unit (for example Reserved word, Delimiter, Literal, Comment) is each of the
following?
Value Lexical Unit

SELECT RESERVED WORD

:= DELIMITER
'TEST' LITERAL
FALSE LITERAL
-- new process COMMENT
FROM RESERVED WORD

/* select the country with the high- COMMENT


est elevation */

v_test LITERAL
4.09 LITERAL
academy.oracle.com

Database Programming with PL/SQL


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

Store large blocks of single-byte or fixed width multi-byte


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

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 composite (record and table)

Store large binary files outside of the database.


BFILE

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


REFERENCE

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


OBJECT

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.
PL/SQL uses special data types to keep track of the different types of data it processes. These data
types define how the data is physically stored, what the constraints for the data are, and finally,
what the valid range of values for the data is.

Copyright © 2019, 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
respective owners.
2. Identify the three data type categories covered in this course.

LOB, Scalar, Composite

Copyright © 2019, 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
respective owners.
2

3. Identify three data types covered in the Database Programming with SQL course.
Number, Date, Varchar2

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?
BFILE

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
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 SCALAR VARCHAR2
science of the 21st century.’

Index Last_name
1 'Newman'
2 'Raman' COMPOSITE TABLE
3 'Han'

A movie LOB BFILE


A sound byte LOB BFILE OR BLOB
FALSE SCALAR BLOB

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