7-10. | When these commands are executed in SQL*Plus or a similar interface, what is the output? CREATE TYPE temperature_t AS TABLE OF NUMBER;
/
CREATE TABLE hourly_temperatures
(temperature_date DATE,
temperatures temperature_t)
NESTED TABLE temperatures STORE AS temperature_store;
SELECT COUNT(*)
FROM hourly_temperatures
/
SELECT COUNT(*)
FROM temperature_store
/ |
7-11. | Assuming the same types and declarations shown in 7-10, what happens when a user issues the following SELECT statement? SELECT COUNT(*)
FROM temperature_store
/ |
7-12. | Knowitall Andthensome passed his MCSE exam by answering question number 1 as 10, question 2 as 20, question 3 as 30, question 4 as 40, and question 5 as 50. Given the following type definitions, create a single SQL statement that inserts Knowitall’s responses into the student_answers table: CREATE TYPE answer_t AS OBJECT ( question_no NUMBER(10),
answer_no NUMBER(10));
CREATE TYPE answer_list_t AS TABLE OF answer_t;
CREATE TABLE student_answers (
student_id INTEGER,
exam_date DATE,
exam_id INTEGER,
student_answer_values answer_list_t)
NESTED TABLE student_answer_values
STORE AS student_answer_store; |
7-13. | Now that there is an entry in the student_answers, what syntax is required to query all fields in the student_answers table? All fields in the nested table? |
7-14. | Continuing with the student_answers example, what SQL syntax is required to add another answer? |
7-15. | What is the SQL syntax to delete the entry just added? |
7-16. | An Oracle error is lurking within this code snippet. Can you spot and fix it? BEGIN
/*
|| Insert an exam entry
*/
INSERT INTO student_answers
(student_id,
exam_date,
exam_id,
student_answer_values)
VALUES
(100,
SYSDATE,
100,
NULL);
/*
|| Insert a single answer for the entry just created
*/
INSERT INTO THE ( SELECT student_answer_values
FROM student_answers
WHERE student_id = 100
AND TRUNC(exam_date) = TRUNC(SYSDATE)
AND exam_id = 100 )
VALUES (answer_t(1,10));
END; |
7-17. | Identify each of the following nested table methods: Returns TRUE if an element has been defined or FALSE if it has not. Returns the number of elements a nested table contains. It is 0 when the table is initially defined. Always returns NULL for nested tables. Returns the number of the first element in the nested table. It returns NULL if no elements are defined. Returns the number of the last element in the nested table. It returns NULL if no elements are defined. Returns the number of the next lowest element to the one specified. If there are no lower elements, it returns NULL. It also returns NULL if no elements are defined. Returns the number of the next highest element to the one specified. If there are no higher elements, it returns NULL. It also returns NULL if no elements are defined. Removes (undefines) elements in the nested table. However, it leaves a placeholder behind. Creates new placeholders on the end of the nested table. These placeholders can then be assigned values. Removes elements from the end of nested tables and leaves no placeholder behind.
|
7-18. | True or false (assume x is a nested table variable)? NVL(x.LAST,0) will always be equal to NVL(x.COUNT,0). x.FIRST will always be 1 or NULL. NVL(x.LAST,0) may not be equal to NVL(x.COUNT,0). x.LIMIT will always be NULL.
|
7-19. | Supply the missing datatype for v_answers: DECLARE
-- cursor to finally get some answers
CURSOR curs_get_answers IS
SELECT student_answer_values
FROM student_answers;
v_answers <data type required>;
BEGIN
-- I want some answers!
OPEN curs_get_answers;
FETCH curs_get_answers INTO v_answers;
CLOSE curs_get_answers;
-- display the last value if the first
-- exists?
IF v_answers.EXISTS(v_answers.FIRST) THEN
DBMS_OUTPUT.PUT_LINE(v_answers.LAST);
END IF;
-- add an element
v_answers.EXTEND;
END; |
7-20. | This code does not execute because of an error lurking within. What line causes the failure and why? DECLARE
v_answers_a answer_list_t := answer_list_t();
v_answers_b answer_list_t := answer_list_t();
v_answers_c answer_list_t;
v_answers_d answer_list_t := answer_list_t();
BEGIN
v_answers_a.EXTEND;
v_answers_a(1) := answer_t(1,10);
v_answers_b.EXTEND;
v_answers_b(1) := answer_t(1,10);
v_answers_c.EXTEND;
v_answers_c(1) := answer_t(1,10);
v_answers_d.EXTEND;
v_answers_d(1) := answer_t(1,10);
END; |
7-21. | What line in this code causes an Oracle error? DECLARE
-- declare a collection with no null
-- answers allowed
v_no_null_answers answer_list_t NOT NULL;
BEGIN
-- add an answer
v_no_null_answers.EXTEND;
v_no_null_answers(1) := answer_t(1,10);
-- add a NULL answer
v_no_null_answers.EXTEND;
v_no_null_answers(2) := NULL;
END; |
7-22. | What can be done to ensure that no NULL values are inserted into a user-defined type? |
7-23. | Other than programmatic validation, how can you ensure that entries in a nested table are unique within their corresponding outer-table record? |
7-24. | What is the output from this block? DECLARE
/*
|| Simple program to demonstrate the
|| interaction of EXTEND, TRIM and DELETE
*/
v_answers answer_list_t := answer_list_t();
v_element PLS_INTEGER;
BEGIN
v_answers.EXTEND;-- line 1
v_answers(1) := answer_t(1,10);-- line 2
v_answers.EXTEND(3);-- line 3
v_answers.EXTEND(10,1);-- line 4
v_answers.DELETE(1,3);-- line 5
v_answers.DELETE(7);-- line 6
v_answers.TRIM(2);-- line 7
v_answers.TRIM;-- line 8
DBMS_OUTPUT.PUT_LINE('First Element Is ' ||
v_answers.FIRST);
DBMS_OUTPUT.PUT_LINE('Last Element Is ' ||
v_answers.LAST);
DBMS_OUTPUT.PUT_LINE('Count Of Elements Is ' ||
v_answers.COUNT);
v_element := v_answers.FIRST;
LOOP
EXIT WHEN v_element IS NULL;
DBMS_OUTPUT.PUT_LINE(
'Element ' || v_element ||
' = ' || v_answers(v_element).question_no);
v_element := v_answers.NEXT(v_element);
END LOOP;
END; |
7-25. | An Oracle error arises from within this block. What error message is displayed, and what line causes the error? DECLARE
v_answers answer_list_t := answer_list_t(); -- line 1
BEGIN
v_answers.EXTEND(10); -- line 2
v_answers.DELETE(1,99); -- line 3
v_answers.TRIM(11); -- line 4
END; |
7-26. | What is the output of this block? DECLARE
v_answers answer_list_t := answer_list_t();
BEGIN
v_answers.EXTEND(10);-- line 1
v_answers.DELETE(4,5);-- line 2
v_answers.TRIM(v_answers.COUNT);-- line 3
DBMS_OUTPUT.PUT_LINE('Remaining Elements = ' ||
v_answers.COUNT);
END; |
7-27. | Using the test_answers and student_answers tables created in previous exercises, write a function to calculate the percentage of questions a particular student got correct for a particular test on a particular day. Use the following function header: CREATE OR REPLACE FUNCTION calculate_score (
p_student_id student_answers.student_id%TYPE,
p_test_id test_answers.test_id%TYPE,
p_exam_date DATE)
RETURN NUMBER |
7-28. | Write a function that returns TRUE if duplicate answers are found within a passed answer list. Use the following function header: CREATE OR REPLACE FUNCTION duplicates_exist (
p_answer_list answer_list_t)
RETURN BOOLEAN |
7-29. | Supply an argument to the DBMS_OUTPUT.PUT_LINE function that raises the “ORA-06533: Subscript beyond count” error: DECLARE
v_answers answer_list_t := answer_list_t();
BEGIN
DBMS_OUTPUT.PUT_LINE(<your answer here>);
END; |
7-30. | What are nested table locators and in what situations should they be used? |
7-31. | How are nested table locators implemented? |
7-32. | How can PL/SQL determine if a nested table is returning a locator? |