Microsoft Word - Homeworkweek6.Doc

Total Page:16

File Type:pdf, Size:1020Kb

Microsoft Word - Homeworkweek6.Doc

Lab Assignment 9 KEY CIS 208A PL/SQL Programming and SQL

1. Function full_name:

A. Create a function called full_name. Pass two parameters to the function: an employee’s last name and first name. The function should return the full name in the format: last name, comma and space, first name (for example: Smith, Joe). Save your code.

B. Test your function from an anonymous block which uses a local variable to store and display the returned value.

C. Modify your anonymous block from step B to remove the local variable declaration and call the function directly from within the DBMS_OUTPUT.PUT_LINE call. Test the block again.

D. Now call the function from within a SQL SELECT statement. Execute a SQL statement (not a PL/SQL block) which displays the first_name, last_name and full name (using the function) of all employees in department 50. Your output should look like this:

2. Function divide:

A. Create a function called divide that accepts two numbers as input and returns the result of dividing the first number by the second number, rounded to two decimal places. Save your code.

B. Test your function twice from an anonymous block using input values (50,2) and (25,3).

Lab_9_Fa13.doc C. Test your function a third time using input values (16,0). What happens?

D. Modify the function code to trap the ZERO_DIVIDE exception. The exception handler should return a value of zero from the function if ZERO_DIVIDE is raised.

E. Test your function again using input values (16,0) as before. Now what happens?

The questions in this Practice use partial copies of the employees and departments tables. Create these copies by executing the following SQL statements:

CREATE TABLE f_emps AS SELECT employee_id, last_name, salary, department_id FROM employees;

CREATE TABLE f_depts AS SELECT department_id, department_name FROM departments;

3 Function sal_increase

A. Create and execute a function sal_increase using the following two code samples. The first creates a function that returns an employee’s new salary if a percentage increase is granted. The second calls this function in a SELECT statement, using an increase of 5 percent.

CREATE OR REPLACE FUNCTION sal_increase (p_salary f_emps.salary%TYPE, p_percent_incr NUMBER) RETURN NUMBER IS BEGIN RETURN (p_salary + (p_salary * p_percent_incr / 100)); END;

SELECT last_name, salary, sal_increase(salary , 5) FROM f_emps;

B. Now, suppose you want to see the same information in your SELECT

Lab_9_Fa13.doc statement, but only for those employees for whom the increased salary would be greater than 10000. Write and test two SELECT statements to do this. In the first, do NOT use your function. In the second, use your function. Use an increase of 5 percent. Which do you think is better, and why?

Lab_9_Fa13.doc 4. Functions:

A. Name five places within a SQL statement where a function can be used. The first one has been done for you (think of four more).

B. Modify your SELECT using the function from question 2B to ORDER the results by the increased salary in descending order, i.e., highest increased salary first.

C. Examine the following SELECT statement that lists the total salaries in each department, for those departments whose total salary is greater than 20000.

SELECT department_id, sum(salary) FROM f_emps GROUP BY department_id HAVING sum(salary) > 20000;

Modify the statement so that it also lists the total salary in each department if a 5 percent increase is granted, and lists those departments whose increased total salary would be greater than 20000. Your modified statement should call the sal_increase function twice, once in the column_list and once in the HAVING clause. Test the modified statement.

5. Which of the following statements are true:

A) The Data Dictionary is a list of hard coded table names in all Oracle databases. B) The Data Dictionary can be updated by all users with SELECT statements. C) All users of an Oracle Database can see details of all tables in that database. D) The Data Dictionary is owned by the user called SYS.

6. Write and execute a SELECT statement that lists all the stored objects you have created in your account so far. The query should return the object name and type and its status. Order the output by type of object.

7. This question shows how exceptions are propagated.

A. Execute the following two SQL statements to create a duplicate of the departments table, with department_id as the primary key.

CREATE TABLE my_depts AS SELECT * FROM departments;

ALTER TABLE my_depts

Lab_9_Fa13.doc ADD CONSTRAINT my_dept_id_pk PRIMARY KEY (department_id);

B. Examine the following code. Create the procedure in Application Express. Save your work (you will need to modify the procedure code later).

CREATE OR REPLACE PROCEDURE add_my_dept (p_dept_id IN VARCHAR2, p_dept_name IN VARCHAR2) IS BEGIN INSERT INTO my_depts (department_id,department_name) VALUES (p_dept_id, p_dept_name); END add_my_dept;

C. What do you think would happen if you execute this procedure to insert department_id 10 (which already exists)? Write and execute an anonymous block to test your theory.

D. Modify your procedure to handle the exception in a generic WHEN OTHERS exception handler.

E. Now what do you think would happen if you execute this procedure for department_id 10 (which already exists)? Test it again as in step c.

8. Write and execute a SELECT statement to list the names of all the procedures you have created so far.

9. Delete the last procedure you created: outer_proc.

10.. Write and execute a SELECT statement to list the source code of your add_my_dept procedure. Make sure the lines of code are listed in the correct sequence.

11. If you wanted user SUSAN to be able to execute SELECT and all DML statements on your wf_countries table, what SQL statement would you execute to give her the required privileges?

Lab_9_Fa13.doc 12.Another user TOM creates a table called tomtab, and does not grant you any privileges on it.

A. If you try to execute the following statement, will it work?

INSERT INTO tom.tomtab (….) VALUES (…..);

B. Examine the following code. Now the INSERT statement has been included in a procedure which you have created. Will it work now?

CREATE OR REPLACE PROCEDURE my_ins_proc IS BEGIN INSERT into tom.tomtab (….) VALUES (……); END;

C. TOM now executes the following statement: GRANT INSERT ON tomtab TO ; Will your my_ins_proc procedure work now? Why or why not?

D. TOM now REVOKEs your INSERT privilege on tomtab. TOM then writes the following procedure. Which privilege must TOM grant to you to allow you to execute the procedure? With this privilege, will your INSERT work now?

CREATE OR REPLACE PROCEDURE tom_ins_proc IS BEGIN INSERT into tom.tomtab (….) VALUES (……); END;

Lab_9_Fa13.doc The following questions illustrate how Definer’s and Invoker’s rights work.

13.The following two procedures have been created in an account called IACAD_SCHEMA, which also contains an emps table:

CREATE OR REPLACE PROCEDURE show_emps_def (p_emp_id IN NUMBER) IS v_name emps.name%TYPE; v_dept_id emps.department_id%TYPE; v_dept_name emps.department_name%TYPE; v_sal emps.salary%TYPE; BEGIN SELECT name, department_id, department_name, salary INTO v_name, v_dept_id, v_dept_name, v_sal FROM emps WHERE employee_id = p_emp_id; DBMS_OUTPUT.PUT_LINE('The employee details are: '|| v_name ||' '|| v_dept_id ||' '|| v_dept_name ||' '|| v_sal); EXCEPTION WHEN no_data_found THEN DBMS_OUTPUT.PUT_LINE('No data found for employee id : ' || p_emp_id|| '. Sorry, please enter another number and try again.'); END;

CREATE OR REPLACE PROCEDURE show_emps_inv (p_emp_id IN NUMBER) AUTHID CURRENT_USER IS v_name emps.name%TYPE; v_dept_id emps.department_id%TYPE; v_dept_name emps.department_name%TYPE; v_sal emps.salary%TYPE; BEGIN SELECT name, department_id, department_name, salary INTO v_name, v_dept_id, v_dept_name, v_sal FROM emps WHERE employee_id = p_emp_id; DBMS_OUTPUT.PUT_LINE('The employee details are: '|| v_name ||' '|| v_dept_id ||' '|| v_dept_name ||' '|| v_sal); EXCEPTION WHEN no_data_found THEN DBMS_OUTPUT.PUT_LINE('No data found for employee id : ' || p_emp_id|| '. Sorry, please enter another number and try again.'); Lab_9_Fa13.doc END;

A. DESCRIBE both procedures, to verify that you can see them in your account. Remember to prefix the procedure name with the schema name.

B. Execute a SQL statement to try to select directly from the table used in the procedures.

C. What are the differences between the two procedures?

14. Execute the first procedure (show_emps_def) with the following actual parameter value: employee_id = 100. What happens and why?

15. Execute the first procedure again, this time with employee_id = 10. What happens and why?

Lab_9_Fa13.doc

Recommended publications