Académique Documents
Professionnel Documents
Culture Documents
The questions in this Practice use partial copies of the employees and departments tables.
Create these copies by executing the following SQL statements:
1. 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.
Now, suppose you want to see the same information in your SELECT
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?
2. 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).
Lab_9_Sp17_208A.doc
The column-list of a SELECT statement
3. Modify your anonymous block from question 1 (the block with the calls to the
sal_increase function) to ORDER the results by the increased salary in descending order
(i.e., highest increased salary first)
4. Examine the following SELECT statement that lists the total salaries in each
department, for those departments whose total salary is greater than 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.
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.
3. 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.
Lab_9_Sp17_208A.doc
ection 9-4, Exercise #1 A-E, 2, 3, 4
B. Examine the following code. Create the procedure. Save your work (you will
need to modify the procedure code later).
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.
Modify your procedure to handle the exception in a generic WHEN OTHERS exception
handler.
D. 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.
F. Modify the procedure code to leave out the exception section again. Run the
code.
Lab_9_Sp17_208A.doc
G. Execute the following code to create a new procedure called outer_proc which
calls add_my_dept, passing department_id 10 to it:
2. Write and execute a SELECT statement to list the names of all the procedures you
have created so far.
4. Write and execute a SELECT statement to list the source code of your add_my_dept
procedure. Make sure your SELECT statement list the lines of code in the correct order.
1. If you wanted user SUSAN to be able to execute SELECT and all DML statements on
your countries table, what SQL statement would you execute to give her the required
privileges?
2. User TOM creates a table called TOMTAB, and does not grant you any privileges on
it.
Lab_9_Sp17_208A.doc
B. Examine the following code. Now the INSERT statement has been included in a
procedure which you have created. Will it work now?
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 his
tom_ins_proc procedure? With this privilege, will the INSERT work when you invoke
TOM’s procedure?
Lab_9_Sp17_208A.doc
Section 9-6, Exercise #1, 2, 3, 4
The following questions illustrate how definer’s and Invoker’s rights work.
1. Assume the following two procedures have been created in an account called
IACAD_SCHEMA, which also contains an emps table:
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.
2. Execute the first procedure (show_emps_def) with the following actual parameter
value: employee_id = 100. What happens and why?
3.. Execute the first procedure again, this time with employee_id = 10. What happens and
why?
4. Execute the second procedure (show_emps_inv) with employee_id = 100. What happens and
why?
Lab_9_Sp17_208A.doc