12 create packages

23
Examine this code: BEGIN theater_pck.v_total_seats_sold_overall := theater_pck.get_total_for_year; END; For this code to be successful, what must be true? Only the GET_TOTAL_FOR_YEAR variable must exist in the specification of the THEATER_PCK package. Only the V_TOTAL_SEATS_SOLD_OVERALL variable must exist in the specification of the THEATER_PCK package. Both the V_TOTAL_SEATS_SOLD_OVERALL variable and the GET_TOTAL_FOR_YEAR function must exist only in the body of the THEATER_PCK package. Both the V_TOTAL_SEATS_SOLD_OVERALL variable and the GET_TOTAL_FOR_YEAR function must exist in the specification of the THEATER_PCK package. Explanation: Only constructs declared in the package specification are public and can be referenced from outside the package by prefixing them with the package name. Examine this package specification and body: CREATE OR REPLACE PACKAGE theater_pck IS PROCEDURE find_seats_sold (v_movie_id IN NUMBER DEFAULT 34, v_theater_id IN NUMBER); END theater_pck; CREATE OR REPLACE PACKAGE BODY theater_pck IS

Upload: rashmihr

Post on 18-Nov-2014

220 views

Category:

Documents


6 download

TRANSCRIPT

Examine this code: BEGIN theater_pck.v_total_seats_sold_overall := theater_pck.get_total_for_year; END; For this code to be successful, what must be true? Only the GET_TOTAL_FOR_YEAR variable must exist in the specification of the THEATER_PCK package. Only the V_TOTAL_SEATS_SOLD_OVERALL variable must exist in the specification of the THEATER_PCK package. Both the V_TOTAL_SEATS_SOLD_OVERALL variable and the GET_TOTAL_FOR_YEAR function must exist only in the body of the THEATER_PCK package. Both the V_TOTAL_SEATS_SOLD_OVERALL variable and the GET_TOTAL_FOR_YEAR function must exist in the specification of the THEATER_PCK package.

Comment [1]:

Explanation:Only constructs declared in the package specification are public and can be referenced from outside the package by prefixing them with the package name.Comment [2]:

Examine this package specification and body: CREATE OR REPLACE PACKAGE theater_pck IS PROCEDURE find_seats_sold (v_movie_id IN NUMBER DEFAULT 34, v_theater_id IN NUMBER); END theater_pck; CREATE OR REPLACE PACKAGE BODY theater_pck IS current_avg_cost_per_ticket NUMBER; PROCEDURE find_seats_sold (v_movie_id IN NUMBER DEFAULT 34, v_theater_id IN NUMBER) IS v_seats_sold gross_receipt.seats_sold%TYPE; v_budget studio.yearly_budget%TYPE; BEGIN SELECT seats_sold INTO v_seats_sold FROM gross_receipt

Comment [3]:

WHERE movie_id = v_movie_id AND theater_id = v_theater_id; END find_seats_sold; FUNCTION get_budget (v_studio_id IN NUMBER) RETURN number IS v_yearly_budget NUMBER; BEGIN SELECT yearly_budget INTO v_yearly_budget FROM studio WHERE id = v_studio_id; RETURN v_yearly_budget; END get_budget; END theater_pck; Which code will successfully invoke the GET_BUDGET function within SQL*Plus? VARIABLE g_budget_amount NUMBER EXECUTE :g_budget_amount := get_budget(11); VARIABLE g_budget_amount NUMBER EXECUTE theater_pck.get_budget(g_budget_amount, 11); VARIABLE g_budget_amount NUMBER EXECUTE :g_budget_amount := theater_pck.get_budget(11); This function cannot be referenced from outside the package.

Explanation:Constructs declared in the package body only are considered to be private and cannot be invoked from outside the package.Comment [4]:

Examine this package specification and body: CREATE OR REPLACE PACKAGE theater_pck IS PROCEDURE find_seats_sold (v_movie_id IN NUMBER DEFAULT 34, v_theater_id IN NUMBER); END theater_pck; CREATE OR REPLACE PACKAGE BODY theater_pck IS current_avg_cost_per_ticket NUMBER;

Comment [5]:

PROCEDURE find_seats_sold (v_movie_id IN NUMBER DEFAULT 34, v_theater_id IN NUMBER) IS v_seats_sold gross_receipt.seats_sold%TYPE; v_budget studio.yearly_budget%TYPE; BEGIN SELECT seats_sold INTO v_seats_sold FROM gross_receipt WHERE movie_id = v_movie_id AND theater_id = v_theater_id; END find_seats_sold; FUNCTION get_budget (v_studio_id IN NUMBER) RETURN number IS v_yearly_budget NUMBER; BEGIN SELECT yearly_budget INTO v_yearly_budget FROM studio WHERE id = v_studio_id; RETURN v_yearly_budget; END get_budget; END theater_pck; Which statement about the GET_BUDGET function is true? It can be referenced within a SQL statement. It can be referenced from outside of the package. It can only be referenced from within the package. It must also be specified in the specification because it is specified in the body.

Explanation:Private constructs are declared in the package body only. These constructs can only be invoked from a construct within the same package. They cannot be invoked from outside the package.Comment [6]:

Which statement about packages is true? Package constructs stored in the database are public. Package constructs defined within the package specification are private.

Comment [7]:

Private and public package constructs are both declared and defined within the package body. A private package construct can only be referenced only by other constructs within the same package.

Explanation:A package consists of two parts, the package specification and the package body. A package construct can be either public or private. Public package constructs are declared in the package specification and defined in the package body. Private package constructs are defined only within the package body. Public constructs can be referenced from any Oracle server environment, but private constructs can only be referenced by other constructs that are in the same package.

Comment [8]:

You decide to use packages to logically group related programming constructs. Which two types of constructs can be grouped within a package? (Choose two.) view cursor variable database trigger application trigger

Comment [9]:

Explanation:Database and application triggers are constructs that cannot be part of a p ackage. Though a package may contain a construct that references a view, the view itself, cannot be part of a package.

Comment [10]:

Examine this package specification and body: CREATE OR REPLACE PACKAGE theater_pck IS PROCEDURE find_seats_sold (v_movie_id IN NUMBER DEFAULT 34, v_theater_id IN NUMBER); END theater_pck; CREATE OR REPLACE PACKAGE BODY theater_pck IS current_avg_cost_per_ticket NUMBER; PROCEDURE find_seats_sold (v_movie_id IN NUMBER DEFAULT 34, v_theater_id IN NUMBER) IS v_seats_sold gross_receipt.seats_sold%TYPE;

Comment [11]:

v_budget studio.yearly_budget%TYPE; BEGIN SELECT seats_sold INTO v_seats_sold FROM gross_receipt WHERE movie_id = v_movie_id AND theater_id = v_theater_id; END find_seats_sold; FUNCTION get_budget (v_studio_id IN NUMBER) RETURN number IS v_yearly_budget NUMBER; BEGIN SELECT yearly_budget INTO v_yearly_budget FROM studio WHERE id = v_studio_id; RETURN v_yearly_budget; END get_budget; END theater_pck; Which statement about the CURRENT_AVG_COST_PER_TICKET variable is true? It can only be referenced from outside the package. It can be referenced from outside and within the package. It can be referenced by all constructs within the package. It must be moved to the specification to compile successfully. Explanation:Variables declared in the package body declaration can be referenced from all constructs within the package. These variables are considered private and cannot be referenced from outside the package.Comment [12]:

Examine this procedure: PROCEDURE find_cpt (v_movie_id IN NUMBER, v_cost_per_ticket IN OUT NUMBER) IS BEGIN IF v_cost_per_ticket > 8.50 THEN SELECT cost_per_ticket INTO v_cost_per_ticket

Comment [13]:

FROM gross_receipt WHERE movie_id = v_movie_id; END IF; END; You decide to create this procedure within the THEATER_PCK package . It will be accessible outside of the package. What will you add to the package specification? PROCEDURE find_cpt (v_movie_id IN NUMBER, v_cost_per_ticket PUBLIC PROCEDURE find_cpt (v_movie_id IN NUMBER, v_cost_per_ticket PROCEDURE find_cpt (v_movie_id IN NUMBER, v_cost_per_ticket IS BEGIN IF v_cost_per_ticket > 8.50 THEN SELECT cost_per_ticket INTO v_cost_per_ticket FROM gross_receipt WHERE movie_id = v_movie_id; END IF; END; PUBLIC PROCEDURE find_cpt (v_movie_id IN NUMBER, v_cost_per_ticket IS BEGIN IF v_cost_per_ticket > 8.50 THEN SELECT cost_per_ticket INTO v_cost_per_ticket FROM gross_receipt WHERE movie_id = v_movie_id; END IF; END; Explanation:To make a procedure public within a package, declare the procedure header in the specification. The header includes the procedure name and arguments: PROCEDURE find_cpt (v_movie_id IN NUMBER, v_cost_per_ticket IN OUT NUMBER);

IN OUT NUMBER); IN OUT NUMBER); IN OUT NUMBER)

IN OUT NUMBER)

Comment [14]:

Examine this package specification and body: CREATE OR REPLACE PACKAGE theater_pck

Comment [15]:

IS PROCEDURE find_seats_sold (v_movie_id IN NUMBER DEFAULT 34, v_theater_id IN NUMBER); END theater_pck; CREATE OR REPLACE PACKAGE BODY theater_pck IS current_avg_cost_per_ticket NUMBER; PROCEDURE find_seats_sold (v_movie_id IN NUMBER DEFAULT 34, v_theater_id IN NUMBER) IS v_seats_sold gross_receipt.seats_sold%TYPE; v_budget studio.yearly_budget%TYPE; BEGIN SELECT seats_sold INTO v_seats_sold FROM gross_receipt WHERE movie_id = v_movie_id AND theater_id = v_theater_id; END find_seats_sold; FUNCTION get_budget (v_studio_id IN NUMBER) RETURN number IS v_yearly_budget NUMBER; BEGIN SELECT yearly_budget INTO v_yearly_budget FROM studio WHERE id = v_studio_id; RETURN v_yearly_budget; END get_budget; END theater_pck; Which statement about the FIND_SEATS_SOLD procedure is true? It can be referenced within a SQL statement. It can only be referenced from within the package. It can be referenced from within and outside of the package. It cannot also be specified in the specification because it is specified in the body.

Explanation:

Procedures declared in a package specification are public and can be referenced from inside or outside of the package.

Comment [16]:

Examine this package specification and body: CREATE OR REPLACE PACKAGE theater_pck IS PROCEDURE find_seats_sold (v_movie_id IN NUMBER DEFAULT 34, v_theater_id IN NUMBER); END theater_pck; CREATE OR REPLACE PACKAGE BODY theater_pck IS current_avg_cost_per_ticket NUMBER; PROCEDURE find_seats_sold (v_movie_id IN NUMBER DEFAULT 34, v_theater_id IN NUMBER) IS v_seats_sold gross_receipt.seats_sold%TYPE; v_budget studio.yearly_budget%TYPE; BEGIN SELECT seats_sold INTO v_seats_sold FROM gross_receipt WHERE movie_id = v_movie_id AND theater_id = v_theater_id; END find_seats_sold; FUNCTION get_budget (v_studio_id IN NUMBER) RETURN number IS v_yearly_budget NUMBER; BEGIN SELECT yearly_budget INTO v_yearly_budget FROM studio WHERE id = v_studio_id; RETURN v_yearly_budget; END get_budget; END theater_pck; Which code will successfully assign a value to the CURRENT_AVG_COST_PER_TICKET variable within SQL*Plus?

Comment [17]:

BEGIN current_avg_cost_per_ticket := 6.75; END; BEGIN theater_pck.current_avg_cost_per_ticket := 6.75; END; BEGIN SELECT AVG(cost_per_ticket) INTO current_avg_cost_per_ticket FROM gross_receipt; END; This variable is private to the package and cannot be directly assigned a value within SQL*Plus. Explanation:Constructs declared in the package body only are considered to be private and cannot be invoked or referenced from outside the package.Comment [18]:

Examine this package specification: CREATE OR REPLACE PACKAGE theater_package IS PROCEDURE find_cpt (v_movie_id IN NUMBER, v_cost_per_ticket IN OUT NUMBER); PROCEDURE update_theater (v_name IN VARCHAR2); PROCEDURE find_seats_sold (v_movie_id IN NUMBER DEFAULT 34, v_theater_id IN NUMBER); PROCEDURE add_theater; END theater_package; Which statement about the procedures in this specification is true? They are public procedures. They are private procedures. Each procedure is missing code and, therefore, is illegal. Each procedure contains an argument list and, therefore, is illegal.

Comment [19]:

Explanation:Constructs listed in a package specification are considered public and can be invoked from outside the package.Comment [20]:

Examine this package specification and body:

Comment [21]:

CREATE OR REPLACE PACKAGE theater_pck IS PROCEDURE find_seats_sold (v_movie_id IN NUMBER DEFAULT 34, v_theater_id IN NUMBER); END theater_pck; CREATE OR REPLACE PACKAGE BODY theater_pck IS current_avg_cost_per_ticket NUMBER; PROCEDURE find_seats_sold (v_movie_id IN NUMBER DEFAULT 34, v_theater_id IN NUMBER) IS v_seats_sold gross_receipt.seats_sold%TYPE; v_budget studio.yearly_budget%TYPE; BEGIN SELECT seats_sold INTO v_seats_sold FROM gross_receipt WHERE movie_id = v_movie_id AND theater_id = v_theater_id; END find_seats_sold; FUNCTION get_budget (v_studio_id IN NUMBER) RETURN number IS v_yearly_budget NUMBER; BEGIN SELECT yearly_budget INTO v_yearly_budget FROM studio WHERE id = v_studio_id; RETURN v_yearly_budget; END get_budget; END theater_pck; Which will successfully invoke the FIND_SEATS_SOLD procedure within SQL*Plus? EXECUTE find_seats_sold (500,11); theater_pck.find_seats_sold (500,11); EXECUTE theater_pck.find_seats_sold (500,11); SELECT find_seats_sold(movie_id, theater_id) FROM gross_receipt;

Explanation:Procedures within a package must be invoked using the package name as a prefix. The EXECUTE command is used in SQL*Plus to invoke PL/SQL constructs. Procedures, stand-alone or part of a package, cannot be invoked from a SQL statement.Comment [22]:

Examine this package specification and body: CREATE OR REPLACE PACKAGE theater_pck IS v_total_budget NUMBER; PROCEDURE find_seats_sold (v_movie_id IN NUMBER DEFAULT 34, v_theater_id IN NUMBER); END theater_pck; CREATE OR REPLACE PACKAGE BODY theater_pck IS current_avg_cost_per_ticket NUMBER; PROCEDURE find_seats_sold (v_movie_id IN NUMBER DEFAULT 34, v_theater_id IN NUMBER) IS v_seats_sold gross_receipt.seats_sold%TYPE; v_budget studio.yearly_budget%TYPE; BEGIN SELECT seats_sold INTO v_seats_sold FROM gross_receipt WHERE movie_id = v_movie_id AND theater_id = v_theater_id; END find_seats_sold; FUNCTION get_budget (v_studio_id IN NUMBER) RETURN number IS v_yearly_budget NUMBER; BEGIN SELECT yearly_budget INTO v_yearly_budget FROM studio WHERE id = v_studio_id; RETURN v_yearly_budget; END get_budget; END theater_pck;

Comment [23]:

Which statement about the V_TOTAL_BUDGET variable is true? It must also be declared in the body. It can only be referenced from inside the package. It can only be referenced from outside the package. It can be referenced from inside and outside the package.

Explanation:Variables declared in the specification are public and can be referenced from inside and outside of the package.Comment [24]:

Evaluate this statement: DROP PACKAGE dept_pack; Which statement is true? This statement removes only the package body. The statement removes only the package specification. The statement removes the package specification and the package body. This statement contains a syntax error.

Comment [25]:

Explanation:The DROP PACKAGE package_name; statement removes the package specification and the body. To remove only the package body, issue the DROP PACKAGE BODY package_name; statement. The DROP PACKAGE BODY statement retains the package specification.

Comment [26]:

Which command must you issue in SQL*Plus to display the result of the DBMS_OUTPUT package? SET ECHO ON SET OUTPUT ON SET FEEDBACK ON SET SERVEROUTPUT ON

Comment [27]:

Explanation:DBMS_OUTPUT is an Oracle supplied package that allows you to display messages during a SQL*Plus session. PUT_LINE is a procedure within this package that places a line of text into a buffer and then displays the contents of the buffer to the screen.

You must issue the command, SET SERVEROUTPUT ON, for this package to work in SQL*Plus.Comment [28]:

Examine this code: CREATE OR REPLACE PACKAGE prod_pack IS g_tax_rate NUMBER := .08; END prod_pack; Which statement about this code is true? This package specification can exist without a body. This package body can exist without a specification. This package body cannot exist without a specification. This package specification cannot exist without a body.

Comment [29]:

Explanation:A package specification is created using the CREATE OR REPLACE PACKAGE statement. Package specifications without a body are used to create public variables and do not contain subprograms.Comment [30]:

While creating a package, you placed the function name in the specification and the body. Which type of construct have you created? public illegal private one-time only

Comment [31]:

Explanation:All constructs must be declared in the package body. Constructs that are also declared in a package specification are public and can be referenced from inside or outside of the package.Comment [32]:

Examine this package specification and body: CREATE OR REPLACE PACKAGE theater_pck IS PROCEDURE find_seats_sold (v_movie_id IN NUMBER DEFAULT 34, v_theater_id IN NUMBER); END theater_pck;

Comment [33]:

CREATE OR REPLACE PACKAGE BODY theater_pck IS current_avg_cost_per_ticket NUMBER; PROCEDURE find_seats_sold (v_movie_id IN NUMBER DEFAULT 34, v_theater_id IN NUMBER) IS v_seats_sold gross_receipt.seats_sold%TYPE; v_budget studio.yearly_budget%TYPE; BEGIN SELECT seats_sold INTO v_seats_sold FROM gross_receipt WHERE movie_id = v_movie_id AND theater_id = v_theater_id; END find_seats_sold; FUNCTION get_budget (v_studio_id IN NUMBER) RETURN number IS v_yearly_budget NUMBER; BEGIN SELECT yearly_budget INTO v_yearly_budget FROM studio WHERE id = v_studio_id; RETURN v_yearly_budget; END get_budget; END theater_pck; Which type of variable is CURRENT_AVG_COST_PER_TICKET? public private invalid constant

Explanation:Variables declared in the package body declaration can be referenced from all constructs within the package. These variables are considered private and cannot be referenced from outside the package.Comment [34]:

Examine this function: CREATE OR REPLACE FUNCTION set_budget (v_studio_id IN NUMBER, v_new_budget IN NUMBER) RETURN number IS BEGIN UPDATE studio SET yearly_budget = v_new_budget WHERE id = v_studio_id; COMMIT; RETURN SQL%ROWCOUNT; END; While executing this in SQL*Plus, you want to see the value of SQL%ROWCOUNT displayed on the screen. Which line of code will accomplish this? OUTPUT.PUT_LINE(TO_CHAR(SQL%ROWCOUNT)); DBMS_DEBUG.PUT_LINE(TO_CHAR(SQL%ROWCOUNT)); DBMS_OUTPUT.DISPLAY(TO_CHAR(SQL%ROWCOUNT)); DBMS_OUTPUT.PUT_LINE(TO_CHAR(SQL%ROWCOUNT));

Comment [35]:

Explanation:DBMS_OUTPUT is an Oracle supplied package that allows you to display messages during a SQL*Plus session. PUT_LINE is a procedure within this package that places a line of text into a buffer and then displays the contents of the buffer to the screen. To view the results of the DBMS_OUTPUT package in SQL*Plus, you must first issue the SET SERVEROUTPUT ON command.

Comment [36]:

Examine this function: CREATE OR REPLACE FUNCTION get_budget (v_studio_id IN NUMBER) RETURN number IS v_yearly_budget NUMBER; BEGIN SELECT yearly_budget INTO v_yearly_budget FROM studio WHERE id = v_studio_id; RETURN v_yearly_budget; END;

Comment [37]:

This function is owned by the account, PROD. The user, JSMITH, must execute this function. Which GRANT command(s) should be issued? GRANT EXECUTE ON get_budget TO jsmith; GRANT EXECUTE, SELECT ON studio TO jsmith; GRANT EXECUTE, SELECT ON get_budget TO jsmith; GRANT SELECT ON studio TO jsmith; GRANT EXECUTE ON get_budget TO jsmith;

Explanation:Unless you are the owner of a PL/SQL construct, you must be granted the EXECUTE privilege to run it. The executor only requires the EXECUTE privilege and does not require the privileges on the objects referenced within the construct unless the AUTHID CURRENT_USER clause in the header to require privileges of the executor on the referenced objects. If this clause had been added to the construct, JSMITH would also need the SELECT privilege on the STUDIO table.

Comment [38]:

Examine this package body: CREATE OR REPLACE PACKAGE BODY theater_pck IS current_avg_cost_per_ticket NUMBER; PROCEDURE find_seats_sold (v_movie_id IN NUMBER DEFAULT 34, v_theater_id IN NUMBER) IS v_seats_sold gross_receipt.seats_sold%TYPE; v_budget studio.yearly_budget%TYPE; BEGIN SELECT seats_sold INTO v_seats_sold FROM gross_receipt WHERE movie_id = v_movie_id AND theater_id = v_theater_id; END find_seats_sold; FUNCTION get_budget (v_studio_id IN NUMBER) RETURN number IS v_yearly_budget NUMBER; BEGIN

Comment [39]:

SELECT yearly_budget INTO v_yearly_budget FROM studio WHERE id = v_studio_id; RETURN v_yearly_budget; END get_budget; BEGIN current_avg_cost_per_ticket := 8.50; END theater_pck; Which statement about the value of CURRENT_AVG_COST_PER_TICKET is true? It is 0 until explicitly referenced. It is assigned 8.50 each time the package is referenced. It is assigned 8.50 only when it is explicitly referenced. It is assigned 8.50 when the package is first invoked within a session.

Explanation:The body of a package has a header, declaration, and executable section. The executable section of a package body is specified using the BEGIN keyword after all subprograms are declared and before the END package_name. Code that is written in this section is one-time-only code; it executes the first time the package is referenced within a session. It will not execute this code again unless the user changes sessions or the package is recompiled. The CURRENT_AVG_COST_PER_TICKET is assigned 8.50 the first time the packaged is referenced and retains this value for the session.

Comment [40]:

Which statement about packages is true? A package can be called. A package can be nested. A package can be parameterized. Package contents can be shared by multiple applications.

Comment [41]:

Explanation:Unlike a procedure or function, the package itself cannot be called, parameterized, or nested. However, once compiled, the contents of a package can be shared by multiple applications.Comment [42]: