Homework Week2 - PLSQL-variables

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8
At a glance
Powered by AI
The document discusses PL/SQL concepts like identifiers, reserved words, delimiters, literals and data types. It also provides examples of good and bad programming practices.

Identifiers name objects, reserved words have special meaning, delimiters are symbols with special meaning, and literals are explicit values that are not represented by identifiers.

Data types in PL/SQL fall under scalar, composite and LOB categories with specific types like character, number, date etc.

Darko Petrovic PL/SQL Homework 2 solution

Homework Week #2 PL/SQL Virtual Training


1. Fill in the blanks. 0
1 1 1

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 DELIMTER is a symbol that has special meaning to the Oracle database.
1 D. A LITERAL is an explicit numeric, character string, date, or Boolean value

that is not represented by an identifier.


1

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 Today Last name Todays_date number_of_days_in_february_this_ year Isleap$year #number NUMBER# Number1to7 Valid (X) X Invalid (X) X X X X X X X # on begining Why Invalid?

Contain space Contain ' Contain space

Darko Petrovic PL/SQL Homework 2 solution


3. Identify the reserved words in the following list. Word Create Make Table Seat Alter Rename Row number Web Reserved? Y/N Y N Y N Y Y Y Y N

4. What kind of lexical unit (for example Reserved word, Delimiter, Literal, Comment) is each of the following? Value SELECT := TEST FALSE -- new process FROM /*select the country with the highest elevation */ V_test 4.09 Lexical Unit RESERVED WORD DELIMITER LITERAL LITERAL COMMENT RESERVED WORD COMMENT LITERAL LITERAL

Darko Petrovic PL/SQL Homework 2 solution


5. 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 Switzerland 100.20 1053 12-DEC-2005 False Index Last name 1 'Newman' 2 'Raman' 3 'Han' A movie A soundbyte A picture

Data Type Category SCALAR SCALAR SCALAR SCALAR SCALAR COMPOSITE

Data Type
CHARACTER NUMBER NUMBER DATE BOOLEAN PL/SQL SCHEMA

LOB LOB LOB

BFILE BFILE BLOB

6. Evaluate the variables in the following code. Answer the following questions about each variable. Is it named well? Why or why not? If it is not named well, what would be a better name and why? DECLARE d_country_name VARCHAR2 (50); d_median_age NUMBER(6,2); BEGIN SELECT country_name, median_age INTO d_country_name, d_median_age FROM wf_countries WHERE country_name = United States of America'); DBMS_OUTPUT.PUT_LINE(' The median age in '||d_country_name||' is '||d_median_age||'.'); END; Avoid using column names as identifiers.

Darko Petrovic PL/SQL Homework 2 solution


7. Examine the declarations in question 6. Change the declarations so that they use the %TYPE attribute. 8. Create the following anonymous block: BEGIN DBMS_OUTPUT.PUT_LINE('Hello World'); END; A. Add a declarative section to this PL/SQL block. In the declarative section, declare the following variables: The today variable of the DATE type. Initialize today with sysdate. DECLARE v_today DATE := SYSDATE; The tomorrow variable of the today type. Use the %TYPE attribute to declare this variable. DECLARE v_today DATE := SYSDATE; v_tomorow v_today%TYPE := SYSDATE +1; B. In the executable section, initialize the tomorrow variable with an expression that calculates tomorrows date (add 1 to the value in today). Print the value of today and tomorrow after printing Hello World. DECLARE v_today DATE := SYSDATE; v_tomorow v_today%TYPE := SYSDATE +1; BEGIN DBMS_OUTPUT.PUT_LINE('Hello World'); DBMS_OUTPUT.PUT_LINE(v_today); DBMS_OUTPUT.PUT_LINE(v_tomorow); END;

Darko Petrovic PL/SQL Homework 2 solution


9. Examine the following code and then answer the questions. DECLARE x VARCHAR2(20); BEGIN x:= '123' + '456' ; DBMS_OUTPUT.PUT_LINE(x); END; A. What do you think the output will be when you run the above code? 123456 B. Now, run the code. What is the output? 579 C. In your own words, describe what happened when you ran the code. Did any implicit conversions take place? Implicit conversion took place

10. Write an anonymous PL/SQL block that uses the programmers full name and then returns the number of characters in the name. BEGIN DBMS_OUTPUT.PUT_LINE('Broj slova u imenu Darko Petrovic je:'); DBMS_OUTPUT.PUT_LINE(LENGTH('Darko Petrovic')); END;

Darko Petrovic PL/SQL Homework 2 solution


11. Write an anonymous PL/SQL block that uses todays date and outputs it in the format of Month DD, YYYY. Store the date in a DATE variable called my_date. Create another variable of the date type called v_last_day. Assign v_last_day to the last day of this month. Display the output. DECLARE my_date DATE :=SYSDATE; v_last_date my_date%TYPE :=SYSDATE; BEGIN DBMS_OUTPUT.PUT_LINE('Danas je: ' || TO_CHAR(my_date, 'MONTH DD, YYYY')); DBMS_OUTPUT.PUT_LINE('Zadnji dan ovog meseca je: ' || LAST_DAY(my_date)); END;

12. Examine the following code and then answer the questions. DECLARE x NUMBER(6); BEGIN x := 5 + 3 * 2 ; DBMS_OUTPUT.PUT_LINE(x); END; A. What do you think the output will be when you run the above code? 11 B. Now run the code. What is the output? 11 C. In your own words, explain the results. multiplication takes precedence over the addition

Darko Petrovic PL/SQL Homework 2 solution


13. Evaluate the PL/SQL block below and determine the value of each of the following variables according to the rules of scoping. DECLARE weight NUMBER(3) := 600; message VARCHAR2(255) := 'Product 10012'; BEGIN DECLARE weight NUMBER(3) := 1; message VARCHAR2(255) := 'Product 11001'; new_locn VARCHAR2(50) := 'Europe'; BEGIN weight := weight + 1; new_locn := 'Western ' || new_locn; -- Position 1 -END; weight := weight + 1; message := message || ' is in stock'; -- Position 2 -END; 1 A. The value of weight at position 1 is: 2 B. The value of new_locn at position 1 is: Western Europe C. The value of weight at position 2 is: 3 D. The value of message at position 2 is: Product 11001 is in stock

Darko Petrovic PL/SQL Homework 2 solution


14. Enter and run the following PL/SQL block. It will execute correctly if you have entered it correctly, but it contains some examples of bad programming practice. A. Modify the block to use good programming practice, and re-run the block. B. Your modified block should contain examples of the following good programming practices: explicit data type conversions, meaningful and consistent variable names, use of %TYPE, upper and lowercase conventions, single and multi-line comments, and clear indentation. DECLARE myvar1 VARCHAR2(20); myvar2 number(4); BEGIN SELECT country_name INTO myvar1 FROM wf_countries WHERE country_id = 1246; myvar2 := '1234'; MYVAR2 := myvar2 * 2; DBMS_OUTPUT.PUT_LINE(myvar1); End; DECLARE myvar1 VARCHAR2(20); myvar2 NUMBER(4); BEGIN SELECT country_name INTO myvar1 FROM wf_countries WHERE country_id = 1246; myvar2 := 1234; myvar2 := myvar2 * 2; DBMS_OUTPUT.PUT_LINE(myvar1); END

You might also like