Lesson 4 - PLSQL Introduction

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Your Name: Michael Meyers

Assignment 4: Introduction to PL/SQL


Please write your name at the top of the sheet and use a different color than black when writing
answers.

1. (3 pts) In your own words, describe why a procedural language like PL/SQL is needed.
Instead of having to make a new SQL statement to update the table every time, you can use PL/SQL
to execute one statement that accomplishes the task of many. Which by saying write one simple
PL/SQL statement instead of having to write a thousand lines of SQL code to make more complex
changes to your tables. Basically, combines programing login and controls the flow with SQL.

2. (10 pts) In the following code, identify (circle or highlight) examples of these procedural constructs:
variable, conditional control statement, reusable/callable program unit (functions or methods),
assignment statement, and modular block structure.

DECLARE
v_first_name varchar2(40);
v_last_name varchar2(40);
v_first_letter varchar2(1);
BEGIN
SELECT first_name, last_name INTO v_first_name, v_last_name
FROM students
WHERE student_id = 105;

v_first_letter := get_first_letter(v_last_name);

IF 'N' > v_first_letter THEN


DBMS_OUTPUT.PUT_LINE('The last name for ' || v_first_name || ' ' || v_last_name || ' is between A and M');
ELSE
DBMS_OUTPUT.PUT_LINE('The last name for ' || v_first_name || ' ' || v_last_name || ' is between N and Z');
END IF;
END;

a. Variables?
v_first_name varchar2(40);
v_last_name varchar2(40);
v_first_letter varchar2(1);
b. Conditional Control Statement?
IF 'N' > v_first_letter THEN
DBMS_OUTPUT.PUT_LINE('The last name for ' || v_first_name || ' ' || v_last_name
|| ' is between A and M');
ELSE
DBMS_OUTPUT.PUT_LINE('The last name for ' || v_first_name || ' ' || v_last_name
|| ' is between N and Z');
END IF;
c. Reusable/callable program unit?
ELSE
DBMS_OUTPUT.PUT_LINE('The last name for ' || v_first_name || ' ' || v_last_name || ' is between N
and Z')
d. Assignment statement?
v_first_letter := get_first_letter(v_last_name);
e. modular block structure?
DECLARE … , BEGIN… , END.
3. (3 pts) Why is it more efficient to combine SQL statements into PL/SQL blocks?

It is more efficient to use SQL statements within PL/SQL blocks because network traffic can be decreased
significantly, and an application becomes more efficient as well.

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

1. It allows programmers to combine procedural constructs with SQL.

2. PL/SQL program is a block, and blocks allow the programmer to create code that is easier to read
and maintain.

3. PL/SQL allow the programmer to prepare for errors by writing exception handling logic into the
code.

5. (3 pts) How is PL/SQL different from Java and C++. List three differences.
1. PL/SQL requires a database or Oracle tool.
2. PL/SQL is easier to learn than C and Java.
3. PL/SQL is the most efficient language to use with an Oracle database.
6. (8 pts) Identify the vocabulary word for each definition below:
a. Anonymous PL/SQL block: Unnamed block, not stored int the database, declared inline at the
point in an application where it is executed, compiled each time the application is executed,
passed to the PL/SQL engine for execution at the run time, and cannot be invoked or called
because it does not have a name and does not exist after it is executed
b. Function: Computes and Returns a Value
c. Compiler: Checks and Translates high-level programming languages (C, Java, PL/SQL, and so on)
into binary code (ones and zeros) before it can execute
d. Procedure: Performs an Action

7. (4 pts) Complete the following chart defining the syntactical requirements for a PL/SQL block:

Optional or Mandatory? Describe what is included in this section


DECLARE Optional Contains declarations of all variables, constants,
cursors, and user-defined exceptions that are
referenced in the executable and exception
sections
BEGIN Mandatory This executable section begins with the keyword
BEGIN and ends with END. This executable
section of PL/SQL block can include any number
of nested PL/SQL blocks.
Contains SQL statements to retrieve data from
the database and PL/SQL statements to
manipulate data in the block. Must contain at
least one statement.
EXCEPTION Optional Specifies the actions to perform when errors and
abnormal conditions arise in the executable
section.
END; Mandatory This executable section begins with the keyword
BEGIN and ends with END. This executable
section of PL/SQL block can include any number
of nested PL/SQL blocks.
End is terminated with a semicolon
Contains SQL statements to retrieve data from
the database and PL/SQL statements to
manipulate data in the block. Must contain at
least one statement.

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

a. BEGIN
END;
A. Fails, because the executable section must contain at least one statement.

b. DECLARE
amount INTEGER(10);
END;
B. Fails, because there is no executable section (BEGIN is missing)

c. DECLARE
BEGIN
END;
C. Fails, because the executable section must contain at least one statement.

d. DECLARE
amount INTEGER(10);
BEGIN
DBMS_OUTPUT_PUTLINE(amount);
END;
D. Succeeds.

9. (4 pts) Fill in the blanks:

a.PL/SQL blocks that have no names are called _Anonymous Blocks_.


b._Procedure_ and _Function_ are named blocks and are stored in the database.

10. (4 pts) Create and execute a simple anonymous block that does the following:

 Declares a variable (v_timestamp) of datatype DATE and populates it with the date that is six
months from today using DUAL table.
Hint: use ADD_MONTHS(DATE, NUMBER_MONTHS) to get six months from today.

 Example output: if the today is Jan. 4. 2021


In six months, the date will be: 04-Jul-2021

DECLARE
v_timestamp DATE;
BEGIN
SELECT ADD_MONTHS(SystemDate,6) INTO v_timestamp FROM DUAL;
DBMS_OUTPUT_LINE(‘In six months, the date will be: ‘||v_timestamp);
END:
What to submit
Upload your solution word file into E-Portfolio/File Exchange.

You might also like