Friday, April 29, 2011

PL/SQL Exercise and short questions

1. (TCO A) You issue this command:

GRANT update ON inventory TO joe WITH GRANT OPTION;

Which task has been accomplished? (Points: 3)
      Only a system privilege was given to user JOE.
      Only an object privilege was given to user JOE.
      User JOE was granted all privileges on the object.
      Both an object privilege and a system privilege were given to user JOE.


2. (TCO A) Which privilege is a system privilege? (Points: 3)
      Alter
      Execute
      References
      Create any table


3. (TCO A) Which condition must be met for a user to give SELECT
privileges on the CUSTOMER table to everyone using the PUBLIC keyword?
(Points: 3)
      A) Must have SELECT privileges
      B) Must own the CUSTOMER table
      C) Must have been granted the PUBLIC privilege
      D) Must have been granted the SELECT privilege WITH GRANT OPTION
      Either B or D


4. (TCO A) You query the database with this command:

SELECT name
FROM employee
WHERE name LIKE '_z_%';

Which names are displayed? (Points: 3)
      Names starting with "z"
      Names starting with "z" or "Z"
      Names containing "z" as the second letter
      Names containing "z" as any letter except the first


5. (TCO A) The TEACHER table contains these columns:

ID                       NUMBER(9)
SALARY             NUMBER (7,2)
SUBJECT_ID      NUMBER(3)

You need to create a SQL script that will prompt the user to input an
id number and a percent increase value. Each teacher's salary should
be multiplied by the percent increase provided.

Which SQL*Plus script would you use to achieve the desired results? (Points: 3)
      UPDATE teacher SET salary = salary *&increase;
      UPDATE teacher SET salary = salary*&increase WHERE id=&id;
      UPDATE teacher SET salary = salary *&increase WHERE subject_id= ¼ject_id;


6. (TCO A) Which SELECT statement is an equijoin query between two
tables? (Points: 3)
      SELECT region.region_name, employee.salary
FROM region, employee
where region.id = employee.region_no;
      SELECT region.region_name, employee.salary
FROM region, employee
where region.id = employee.region_no(+);
      SELECT region.region_name, employee.salary
FROM region, employee
where employee.salary BETWEEN region.avg_salary AND region.max_salary
      SELECT region.region_name, employee_info.last_name
FROM employee region, employee employee_info
WHERE employee_info.id >= region.manager_id;
where region.id = employee.region_no


7. (TCO A) Which character function would you use to return a
specified portion of a character string? (Points: 3)
      Concat
      Substr
      Length
      Initcap


8. (TCO A) Which character function would you use to return a
specified portion of a character string? (Points: 3)
      CONCAT
      SUBSTR
      LENGTH
      INITCAP


9. (TCO B) Review this SELECT statement:

SELECT ename, emp_number, salary
FROM employee
WHERE dept_number = (SELECT dept_number
FROM department
WHERE location IN('ORLANDO', 'CHICAGO'))

Why may this statement return an error? (Points: 3)
      A multiple-row subquery returns one row.
      A multiple-column subquery returns one column.
      A single-row subquery returns more than one row.
      A multiple-row query uses a single-row subquery.
      A single-row query uses a multiple-row subquery that returns
only one row.


10. (TCO B) You query the database with this command:

SELECT name, salary, dept_id
FROM employee
WHERE salary >
   (SELECT AVG(salary)
   FROM employee
   WHERE dept_no =
       (SELECT dept_no
       FROM employee
       WHERE last_name =
           (SELECT last_name
           FROM employee
           WHERE salary > 50000)));

Which SELECT clause is evaluated last? (Points: 3)
      SELECT dept_no
      SELECT last_name
      SELECT AVG(salary)
      SELECT name, salary, dept_id


11. (TCO B) In which situation would it be most appropriate to use a
self join query? (Points: 3)
      The EMPLOYEE and REGION tables have corresponding columns.
      The EMPLOYEE and REGION tables have no corresponding columns.
      The EMPLOYEE table has two columns that correspond to each other.
      The EMPLOYEE table column corresponding to the REGION table
column contains null values for rows that need to be displayed.


12. (TCO C) What happens when you update a table without a WHERE
clause? (Points: 3)
      The statement will not execute.
      Only the rows specified will be updated.
      All of the rows in the table will be updated.
      The statement will execute, but no changes will be made.


13. TCO C: You logged onto the database to update the INVENTORY table.
After your session began, you issued three UPDATE commands and then
you issued an ALTER table command to add a column constraint. You were
about to issue the COMMIT command when the system crashed. Which
changes were made to the INVENTORY table? (Points: 3)
      Only the UPDATE commands
      Only the ALTER TABLE command
      Both the UPDATE commands and the ALTER TABLE command
      None


14. (TCO C) Which command causes a transaction to end? (Points: 3)
      UPDATE
      DELETE
      INSERT
      ROLLBACK


15. (TCO C) Which statement would not cause an implicit COMMIT to
occur?  (Points: 3)
      CREATE
      DROP
      ALTER
      SAVE


16. TCO C: Which of the following is an advantage of using SAVEPOINTS?
(Points: 3)
      It prevents inconsistent data.
      It allows one to group code into manageable units.
      It prevents one from duplicating a primary key.
      It locks rows and prevents other users from updating the same row.


1. TCO D: Examine the table instance chart for the MANUFACTURER table.
Which command would you use to create an index for the
manufacturer_name column? (Points: 3)
      CREATE INDEX manufacturer(manufacturer_name);
      CREATE INDEX manufacturer_manufacturer_name_idx ON Manufacturer;
      CREATE INDEX manufacturer(manufacturer_name)
manufacturer_manufacturer_name_idx;
      CREATE INDEX manufacturer_manufacturer_name_idx ON
manufacturer(manufacturer_name);


2. TCO D: When creating a user in Oracle, which of the following
decisions should be made? (Points: 3)
      Assigning a temporary tablespace
      Assigning a default tablespace
      Assigning a password
      All of the above


3. TCO D: With which option could a view be created to prevent a user
from updating rows in the base table that are not accessible to the
view? (Points: 3)
      Group function
      GROUP BY clause
      DISTINCT command
      WITH CHECK OPTION


4. TCO D: What action could cause an integrity constraint error? (Points: 3)
      Adding a primary key
      Deleting a foreign key
      Adding a parent record
      Deleting a parent record


5. TCO D: The TRANSACTION table has six columns. Since you often query
the table with a join to the SALE table, you created an index on five
of the columns in the TRANSACTION table. Which result will occur?
(Points: 3)
      Inserts to the table will be slower.
      The speed of deletes will be increased.
      The size of the TRANSACTION table will be increased.
      All queries on the TRANSACTION table will be faster if the it
does not contain a large number of NULL values.


6. TCO D: Which command would you use to remove the MAN_CON_NAME_IDX
index? (Points: 3)
      DROP man_con_name_idx;
      DELETE man_con_name_idx;
      DROP INDEX man_con_name_idx;
      DELETE INDEX man_con_name_idx;


7. TCO D: Evaluate this command:

ALTER TABLE inventory

DISABLE CONSTRAINT inventory_id_number_pk CASCADE;

Which task would this command accomplish? (Points: 3)
      Delete only the primary key
      Disable only the primary key
      Alter all dependent integrity constraints
      Disable all dependent integrity constraint


8. (TCO D) You issue this command:

SELECT emp_id_seq.CURRVAL
FROM SYS.dual;

Which value(s) is displayed? (Points: 3)
      Values of the EMP_ID_SEQ column
      Current value of the EMP_ID_SEQ index
      Current value of the EMP_ID_SEQ cursor
      Current value of the EMP_ID_SEQ sequence


9. TCO D: You issue this command:

SELECT emp_id_seq.CURRVAL
FROM SYS.dual;

Which value(s) is displayed? (Points: 3)
      Values of the EMP_ID_SEQ column
      Current value of the EMP_ID_SEQ index
      Current value of the EMP_ID_SEQ cursor
      Current value of the EMP_ID_SEQ sequence


10. (TCO D) You issue this command:

ALTER USER BILL IDENTIFIED BY THE CAT;

Which task has been accomplished? (Points: 3)
      A new user has been added.
      The user name has been changed.
      The user password has been changed.
      A password has been added to the user account.


11. TCO E: Given this procedure:

PROCEDURE dept_salary
(v_bonusIN BOOLEAN,
v_raise IN BOOLEAN,
v_issue_check IN OUT BOOLEAN)
IS
BEGIN
v_issue_check := v_bonus OR v_raise;
END;

If v_bonus = TRUE and v_raise = NULL, which value is assigned to
v_issue_check? (Points: 3)
      True
      False
      Null
      None


12. (TCO E) Evaluate this PL/SQL block

DECLARE
v_lower NUMBER := 2;
v_upper NUMBER := 100;
v_count NUMBER := 1;
BEGIN
FOR i IN v_lower..v_lower LOOP
INSERT INTO test(results)
VALUES (v_count);
v_count := v_count + 1;
END LOOP;
END;

How many times will the executable statements inside the FOR LOOP
execute? (Points: 3)
      0
      1
      2
      98
      100


13. TCO E: Evaluate this PL/SQL block:

DECLARE
v_low NUMBER :=1;
v_high NUMBER :=6;
v_count NUMBER :=2;
BEGIN
FOR i IN v_low..v_high LOOP
v_count :=v_count +1;
END LOOP;
END;

How many times will the loop execute? (Points: 3)
      0
      1
      4
      6
      7


14. (TCO F) The TRANSACTION table has six columns. Since you often
query the table with a join to the SALE table, you created an index on
five of the columns in the TRANSACTION table. Which result will occur?
(Points: 6)
      Inserts to the table will be slower.
      The speed of deletes will be increased.
      The size of the TRANSACTION table will be increased.
      All queries on the TRANSACTION table will be faster if it does
not contain a large number of NULL values.


15. TCO H: Review this SELECT statement:


SELECT department "Departments", MAX(salary) "Max Salaries"
FROM employee
WHERE department IN(20, 30, 400)
GROUP BY Departments
HAVING MAX(salary) > 60000;


Why does this statement fail? (Points: 4)
      A GROUP BY clause cannot contain a column alias.
      The condition "MAX(salary) > 60000" should reside in the WHERE clause.
      The GROUP BY clause must contain the group function(s) used in
the SELECT list.
      The HAVING clause cannot contain the group function(s) used in
the SELECT list.


16. TCO H: Which clause could you use to restrict the values returned
by a group function? (Points: 4)
      WHERE
      HAVING
      ORDER BY
      A group function cannot be restricted.


17. (TCO H ) You create a Top-N query report for the Marketing
Department that displays the account numbers and new balance of the
1500 accounts that have the lowest new balance. The results are sorted
by the new balance from lowest to highest.

Which two SELECT statement clause is included in your query? (Points: 4)
      inner query: WHERE ROWNUM
      outer query: SELECT account_id "Account", new_balance "Current
Bal", ROWNUM "Rank"
       inner query: ORDER BY new_balance
      inner query: SELECT account_id, new_balance ROWNUM



1. (TCO A) Given the Zip column defined as VARCHAR2(5), what, if
anything, is wrong with the following SQL statement?

SELECT zip + 100
FROM zipcode;
(Points: 15)



2. (TCO B) Set operators allow specialized operations on tables.
Descibe the INTERSECT operator, and give an example how it could be
used effectively. Discuss or show by example how an equijoin would
accomplish the same result. (Points: 20)



3. (TCO C)  Your developers are complaining that the database is
losing data. You are assigned to investigate the problem. After
interviewing the team, you determine that the following actions
occurred.

Delete from emp where deptno = 10;
create table test as select * from emp;
rollback;
delete from dept where deptno = 20;
truncate table test;
rollback;

Write an explanation to management that explains why the data was lost
even though a rollback was executed.
(Points: 20)



4. (TCO D) What are the differences between an object privilege and a
system privilege with respect to grants? Include the WITH GRANT OPTION
and the WITH ADMIN option in your response. (Points: 20)



5. TCO D: You have just granted select access to your STUDENTS table
to all accounts. Your account is D111111. Johnny states he cannot find
your table with the query:

   Select * from Students;

1. Explain the reason for this issue.

2. Rewrite the query so Johnny can select from your table.

3.  Create an object that allows other users to access the students
table using the above command.
(Points: 20)



1. (TCO F) Evaluate the following query and explain the access method
that the optimizer will use to access the rows in each table.

UPDATE enrollment e
SET final_grade =
   (SELECT NVL(AVG(numeric_grade),0)
   FROM grade
   WHERE e.student_id = student_id AND e.section_id = section_id)
WHERE student_id = 1000 AND section_id = 2000

0 rows updated.

-------------------------------------------------------------------------------------------------
| Id           | Operation                                       |
Name                |
|------------------------------------------------------------------------------------------------|
| 0            | UPDATE STATEMENT                    |                         |
| 1            | UPDATE                                         | ENROLLMENT  |
| 2            | INDEX UNIQUE SCAN                     | ENR_PK           |
| 3            | SORT AGGREGATE                       |
        |
| 4            | TABLE ACCESS BY INDEX ROWID | GRADE            |
| 5            | INDEX RANGE SCAN                      | GR_PK            |
------------------------------------------------------------------------------------------------
(Points: 20)



2. (TCO F) You are the DBA of a development project. Your developers
are arguing about whether to write a query using a join, a subquery, a
correlated subquery, or a set operator. Each faction claims one method
performs better then the others. Write a brief synopsis that tells the
developer how to prove whether one query performs better than another.
Include the different factors that affect performance for queries.
(Points: 20)



3. TCO F: Explain in detail how indexes improve query performance for
large tables when the indexed column is used in the WHERE clause of
the query. (Points: 20)



4. TCO G: What are some significant issues when using triggers? Be
sure to explain your answers clearly, accurately, and concisely.
(Points: 20)



5.

(TCO H) A table called CUSTOMER_ORDER contains 5,993 rows with an
order total of $10,993,333.98 based on the orders from 4,500
customers. Based on this scenario, how many rows do you think the
following query returns?

SELECT SUM(order_amount) AS "Order Total" FROM customer_order;

Provide an explanation for your answer that indicates you understand
the concept of aggregations.

Database Programming and Applications Final Exercise

1. (TCO A) Which privilege is a system privilege? (Points: 3)
  
  
  
  


2. (TCO A) Which condition must be met for a user to give SELECT privileges on the CUSTOMER table to everyone using the PUBLIC keyword? (Points: 3)
  
  
  
  
  


3. (TCO A) Evaluate the SQL Script:

Create role payroll;
create role sales_dept;
create role inventory
create user scott identified by tiger;
grant select on employee to payroll;
grant select on sale to sales_dept;
grant payroll to sales_dept;
grant sales_dept to inventory;
grant inventory to scott;

What tables can SCOTT query?
 (Points: 3)
  
  
  
  


4. (TCO A) You are proposing to management that all employees receive a 10% salary increase and receive a commission that is 20% of this newly increased salary. You are writing a report that will display the proposed salary, commission, and total compensation.

Evaluate these SQL statements:

SELECT sal * 1.1 "Salary", sal * 1.1 * .2 "Commission", sal * 1.1 + sal * 1.1 * .2 "Compensation"
FROM emp;

SELECT sal * 1.1 "Salary", sal * 1.1 * .2 "Commission", (sal * 1.1) + (sal * .2) "Compensation"
FROM emp;

SELECT sal * 1.1 "Salary", sal * .2 "Commission", sal * 1.1 + sal * 1.1 * .2 "Compensation"
FROM emp;

What are the results of these statements? 
(Points: 3)
  
  
  
  
  


5. (TCO A) You must display a salary increase of 10% for each employee plus their total compensation, which includes their salary and commission. Some employees do not receive a commission.

Review this select statement:

SELECT sal * 1.10 "Salary", sal * 1.10 + comm "Total Compensation"
FROM emp;

What is the result of this statement?
 (Points: 3)
  
  
  
  


6. (TCO A) Which character function would you use to return a specified portion of a character string? (Points: 3)
  
  
  
  


7. (TCO A) Review this select statement:

SELECT ename, empno, sal
FROM emp
WHERE dept=(SELECT deptno
FROM dept
WHERE UPPER(loc)=UPPER(&loc))

When executing this statement, which statement is FALSE?
 (Points: 3)
  
  
  
 


8. (TCO A) Which character function would you use to return a specified portion of a character string? (Points: 3)
  
  
  
  


9. (TCO B) You are the DBA for a distributed database. One database is located on the east coast, and one is located on the west coast. Both databases have a Department Table for their respecitive territory. You manager wants a report of all the departments from both databases. He states he does not want to see duplicate values. Which set operator will solve this problem? (Points: 3)
  
  
  
  
  


10. (TCO B) Review this SELECT statement:

SELECT ename, emp_number, salary
FROM employee
WHERE dept_number = (SELECT dept_number
FROM department
WHERE location IN('ORLANDO', 'CHICAGO'))

Why may this statement return an error? 
(Points: 3)
  
  
  
  
  


11. (TCO B) You query the database with this command:

SELECT name, salary, dept_id
FROM employee
WHERE salary >
(SELECT AVG(salary)
FROM employee
WHERE dept_no =
(SELECT dept_no
FROM employee
WHERE last_name =
(SELECT last_name
FROM employee
WHERE salary > 50000)));

Which SELECT clause is evaluated last? 
(Points: 3)
  
  
  
  


12. (TCO C) Which command would change the CONTACT_NAME value of the Bozo company from Ignatz to Chester? (Points: 3)
  
  
  
  
  


13. (TCO C) You maintain two tables, CUSTOMER and PROSPECT, that have identical structures but different data. You want to synchronize these two tables by inserting records from the PROSPECT table into the CUSTOMER table if they do not exist. If the customer already exists in the CUSTOMER table, you want to update customer data. Which DML statement should you use to perform this task? (Points: 3)
  
  
  
  
  


14. (TCO C) Which command causes a transaction to end? (Points: 3)
  
  
  
  


15. (TCO C) Which command causes a transaction to end? (Points: 3)
  
  
  
  


16. (TCO C) Which statement would cause an implicit COMMIT to occur? (Points: 3)
  
  
  
  

1. TCO D: Which type of constraint can only be defined at the column level? (Points: 3)
  
  
  
  
  


2. TCO D: The TRANSACTION table has six columns. Since you often query the table with a join to the SALE table, you created an index on five of the columns in the TRANSACTION table. Which result will occur? (Points: 3)
  
  
  
  


3. (TCO D) With which contruct could a view be created that would prevent a user from deleting rows in the base table on which the view is based?(Points: 3)
  
 
  
  


4. (TCO D) Which DML statement could you use to modify the contents of the PRODUCT_NAME column of the existing PRODUCT table? (Points: 3)
  
  
  
  


5. (TCO D) You issue this command:


CREATE PUBLIC SYNONYM parts FOR marilyn.inventory; 
(Points: 3)
  
  
  
  


6. (TCO D) What action could cause an integrity constraint error? (Points: 3)
  
  
  
  


7. (TCO D) Which type of constraint can only be defined at the column level? (Points: 3)
  
  
  
  
  


8. (TCO D) With which option could a view be created to prevent a user from updating rows in the base table that are not accessible to the view? (Points: 3)
  
  
  
  


9. (TCO D) Which command would you use to assign a default value to an existing column? (Points: 3)
  
  
  
  
  


10. TCO D: You issue this command:

SELECT emp_id_seq.CURRVAL
FROM SYS.dual;

Which value(s) is displayed?
 (Points: 3)
  
  
  
  


11. (TCO E) Which program construct must return a value? (Points: 3)
  
  
  
  


12. (TCO E) Evaluate this PL/SQL block

DECLARE
v_lower NUMBER := 2;
v_upper NUMBER := 100;
v_count NUMBER := 1;
BEGIN
FOR i IN v_lower..v_lower LOOP
INSERT INTO test(results)
VALUES (v_count);
v_count := v_count + 1;
END LOOP;
END;

How many times will the executable statements inside the FOR LOOP execute? 
(Points: 3)
  
  
  
  
  


13. TCO E: Review this portion of a PL/SQL block:

DECLARE
. . .
BEGIN
SELECT last_name||', '||first_name
FROM employee
WHERE emp_number = 1132;
. . .
END;

Why does this SELECT statement fail when executed?
 (Points: 3)
  
  
  
  


14. (TCO F) Which circumstance would you create an index for a column of a table? (Points: 6)
  
  
  
  


15. (TCO H) Customer ( CustomerNum, CustomerName, Street, City, State, Zip, Balance, CreditLimit, RepNum)


Based on the code above, for each sales rep, list the rep number, the number of customers assigned to the rep, and the average balance of the rep’s customers. Group the records by rep number and order the records by rep number. 
(Points: 4)
  
  
  
  


16. TCO H: The PRODUCT table contains these columns:

IDNUMBER(7) PK
SALE_PRICE NUMBER(7,2)

Evaluate these two SQL statements:

SELECT MAX(sale_price), MIN(sale_price), AVG(sale_price)
FROM product;

SELECT ROUND(MAX(sale_price),2), ROUND(MIN(sale_price),2), ROUND(AVG(sale_price),2)
FROM product
GROUP BY sale_price;

How will the results differ?
 (Points: 4)
  
  
  
  


17. (TCO H ) You create a Top-N query report for the Marketing Department that displays the account numbers and new balance of the 1500 accounts that have the lowest new balance. The results are sorted by the new balance from lowest to highest.

Which two SELECT statement clause is included in your query?
 (Points: 4)
  
  
  
  


1. (Points: 15)



2.
(TCO B) If you have a choice to write either a correlated subquery or a simple subquery, which one would you choose? Why?
(Points: 20)



3. (Points: 20)



4.
(TCO D) Which columns in a table should you consider indexing? What are other considerations that should be considered before creating the index?
(Points: 20)



5. (Points: 20)



1.
UPDATE enrollment e
SET final_grade =
(SELECT NVL(AVG(numeric_grade),0)
FROM grade
WHERE e.student_id = student_id AND e.section_id = section_id)
WHERE student_id = 1000 AND section_id = 2000
0 rows updated.
-------------------------------------------------------------------------------------------------
| Id | Operation | Name |
|------------------------------------------------------------------------------------------------|
| 0 | UPDATE STATEMENT | |
| 1 | UPDATE | ENROLLMENT |
| 2 | INDEX UNIQUE SCAN | ENR_PK |
| 3 | SORT AGGREGATE | |
| 4 | TABLE ACCESS BY INDEX ROWID | GRADE |
| 5 | INDEX RANGE SCAN | GR_PK |
------------------------------------------------------------------------------------------------
(Points: 20)



2. (Points: 20)



3. (Points: 20)



4. (Points: 20)



5. (Points: 20)