Top Banner
Database Programming - Section 7 Instructor Guide
70

Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Apr 04, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Database Programming - Section 7 Instructor Guide

Page 2: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study
Page 3: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page i

Table of Contents

Database Programming - Section 7................................................................................................................1 Lesson 1 - Multiple-Row Subqueries ............................................................................................................1 What Will I Learn? ........................................................................................................................................3 Why Learn It?................................................................................................................................................4 Tell Me / Show Me........................................................................................................................................5 Try It / Solve It ..............................................................................................................................................12 Lesson 2 - Practice Exercises and Quiz .........................................................................................................18 What Will I Learn? ........................................................................................................................................19 Why Learn It?................................................................................................................................................20 Tell Me / Show Me........................................................................................................................................21 Try It / Solve It ..............................................................................................................................................22 Lesson 3 - Practice Exercises and Quiz .........................................................................................................27 What Will I Learn? ........................................................................................................................................28 Why Learn It?................................................................................................................................................29 Tell Me / Show Me........................................................................................................................................30 Try It / Solve It ..............................................................................................................................................31 Lesson 4 - INSERT Statement.......................................................................................................................34 What Will I Learn? ........................................................................................................................................36 Why Learn It?................................................................................................................................................37 Tell Me / Show Me........................................................................................................................................38 Try It / Solve It ..............................................................................................................................................46 Lesson 5 - Updating Column Values and Deleting Rows ............................................................................50 What Will I Learn? ........................................................................................................................................52 Why Learn It?................................................................................................................................................53 Tell Me / Show Me........................................................................................................................................54 Try It / Solve It ..............................................................................................................................................61

Page 4: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study
Page 5: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 1

Lesson 1 - Multiple-Row Subqueries

Lesson 1 - Multiple-Row Subqueries

Lesson Preparation

Review Study Guide requirements with students. Have students complete the study guides as part of the Try It / Solve It portion of the content.

What to Watch For Students will have difficulty knowing how to break down a database request into the outer and inner query statements. Help them identify what information is needed before the outer query can execute. Finding this unknown information is the function of the inner query. Look for the keywords in the problem, such as greater than, in, less than all, equal to any. The keywords will be the operators for the outer query. These keywords can help divide the problem into the two queries.

Connections Having to find information before something else can be accomplished should be familiar to students. Relate subqueries to data modeling. How could students have designed database tables without knowing the entities, attributes, and relationships used to define the database? In fact, nothing can be done before knowing what the business needs are. Meeting with clients, interviewing key people, and examining business documents are all part of the process to find out what businesses do and what information is critical to know to build a database.

Page 6: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 2

Ask students, as data modelers, if they think that they could encounter a business that really isn't clear on what its business needs are? Possible answer: Whether selling tires or designing software, most businesses are good at the business they do. Knowing how to create an ERD or build a database is probably not something that most businesses have the time, expertise, or resources to accomplish. How far back do you have to go to find the information needed to solve a problem? Possible answer: As a data modeler, it is your job to model the information needs of the business. The more information you have about the business, the more accurately it could be modeled. However, usually time and resources limit an endless search. Somewhere in between is the compromise that becomes the database.

Page 7: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 3

What Will I Learn?

What Will I Learn?

Page 8: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 4

Why Learn It?

Why Learn It?

Why Learn It?

Use the graphic to review the structure of a subquery. Point out the IN operator. Explain in this lesson that students will use a new set of operators to process subqueries that return more than one value.

Page 9: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 5

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Read through the steps in "How to Write a Subquery." Use the example to demonstrate each step.

Page 10: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 6

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

To review previous material, ask students to write a query to find the name of the Global Fast Foods employee who makes the most money.

Page 11: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 7

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Point out to students the subquery results set and show the values returned by the subquery become the values the outer query uses to restrict the rows returned. Ask students what will be returned from the outer query if there are no CD numbers less than 94. Answer: No rows will be returned if a NULL value is returned by the subquery.

Page 12: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 8

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Point out to students the use of ANY. In this case, if there are years in the d_cds table that have null values, the subquery will not return null as long as there are other nonnull values in the year column.

Page 13: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 9

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Explain the graphic. Ask students to state what =ALL , < ALL and <> ALL would return in the example shown in the graphic. =ALL The outer query would only return those titles that had years = ALL. In this case, no data is found. There is no entry with "The Music Man" producer, one year 2000 and the other 2001. <ALL The outer query would only return those titles that had years less than all the years from "The Music Man" producer. <>ALL The outer query would only return those titles that did not have any of the years returned by the subquery from "The Music Man" producer.

Page 14: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 10

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Review the idea of something being NOT IN. Use a simple example such as "I need to speak to all the students NOT IN jazz band." If an inner query returns a null, how can the WHERE clause have a value not in an absent or unavailable value?

Page 15: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 11

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Deconstruct the two almost identical queries for practice. Help students work through what happens in each type of subquery. Answer: The single-row subquery inner query found the lowest salary in department 50 (2500). The outer query then selected all the departments that had their lowest salary greater than that from department 50. The multiple-row inner query also selected the department 50 salary of 2500. No other department had a lowest salary the same as department 50.

Page 16: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 12

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 1. SELECT title, year FROM d_cds WHERE year < (SELECT year FROM d_cds WHERE title LIKE 'Carpe Diem'); 2. SELECT * FROM d_songs WHERE type_code IN (SELECT type_code FROM d_types WHERE description IN ('Jazz','Pop'));

Page 17: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 13

3. SELECT name, cost, package_code FROM d_events where cost <= (SELECT low_range FROM d_packages WHERE code = 200);

Page 18: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 14

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 4. SELECT last_name FROM employees WHERE salary = ANY (SELECT MIN(salary) FROM employees GROUP BY department_id);

Page 19: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 15

5. Point out that there can be more than one way of getting the required information. (multiple row subquery) SELECT first_name, last_name (single-row subquery) FROM f_staffs SELECT first_name, last_name WHERE salary <= ALL FROM f_staffs (SELECT salary WHERE salary= FROM f_staffs); (SELECT min(salary) FROM f_staffs); 6. a. < b. < ANY c. IN or = ANY d. > ALL

Page 20: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 16

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 7. a and d are True 8. SELECT department_id, MIN(salary) FROM employees GROUP BY department_id HAVING MIN(salary) < (SELECT MIN(salary) FROM employees WHERE department_id = 50);

Page 21: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 17

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 9. True: b, d 10. a, b, c, d could be returned

Page 22: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 18

Lesson 2 - Practice Exercises and Quiz

Lesson 2 - Practice Exercises and Quiz

Lesson Preparation

There are 13 review questions to prepare students for the quiz in Lesson 3. Questions 1-8 are listed in Try It / Solve It in this lesson. The remaining questions appear in Try It / Solve It in Lesson 3. The Study Guide for this section should also be completed by students before taking the quiz.

What to Watch For Students may not look carefully at the data returned from a query to verify that the results are desired. Spot-check for accuracy.

Connections None.

Page 23: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 19

What Will I Learn?

What Will I Learn?

Page 24: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 20

Why Learn It?

Why Learn It?

Page 25: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 21

Tell Me / Show Me

Tell Me / Show Me

Page 26: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 22

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: To check for understanding, assign one practice exercise at a time. Review the syntax and results with students. 1. a. SELECT last_name,job_id, salary FROM employees WHERE department_id in (10,30); b. SELECT last_name,job_id, salary FROM employees WHERE department_id = 10 or department_id=30; c. This is not the most efficient answer, but it does return the correct results: SELECT last_name,job_id,salary FROM employees WHERE department_id IN( SELECT department_id FROM departments WHERE department_id IN (10,30));

Page 27: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 23

2. SELECT department_name, location_id FROM departments ORDER BY department_id;

Page 28: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 24

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 3. SELECT e.last_name,e.job_id, TO_CHAR(salary, '$999,999.00'),d.location_id FROM employees e,departments d WHERE e.department_id=d.department_id ORDER BY job_id, salary DESC; 4. SELECT e.last_name,e.job_id,e.salary FROM employees e, job_grades s WHERE e.salary = s.highest_sal or e.salary =s.lowest_sal;

Page 29: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 25

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 5. SELECT e.department_id, d.department_name, COUNT(last_name), SUM(salary), AVG(salary) FROM employees e, departments d WHERE e.department_id = d.department_id GROUP BY e.department_id,d.department_name; 6. SELECT last_name,job_id,department_id FROM employees WHERE department_id IN (SELECT department_id FROM employees GROUP BY department_id HAVING count(*) > 3) ORDER BY department_id;

Page 30: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 26

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 7. SELECT last_name, job_id, salary, hire_date FROM employees WHERE hire_date = (SELECT MIN(hire_date) FROM employees); SELECT last_name, job_id, salary, hire_date FROM employees WHERE hire_date <=ALL (SELECT hire_date FROM employees) 8. SELECT last_name, job_id, salary, LPAD(NVL(TO_CHAR(commission_pct),'NA'),10) commission_pct FROM employees ORDER BY salary DESC;

Page 31: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 27

Lesson 3 - Practice Exercises and Quiz

Lesson 3 - Practice Exercises and Quiz

Lesson Preparation

Ask students if there are any outstanding questions. The questions in this section's practice exercises use subqueries to reinforce case manipulation, date, and number functions.

What to Watch For Successfully pass 70% or better on quiz.

Connections None.

Page 32: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 28

What Will I Learn?

What Will I Learn?

Page 33: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 29

Why Learn It?

Why Learn It?

Page 34: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 30

Tell Me / Show Me

Tell Me / Show Me

Page 35: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 31

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 1. SELECT 'The artist of "' ||title ||'"'||' is ' ||artist AS "DJ On Demand Songs" FROM d_songs WHERE type_code = (SELECT type_code FROM d_songs WHERE title LIKE 'Im Going to Miss My Teacher'); 2. SELECT last_name, TO_CHAR(salary, '$99,999.99')AS "Monthly Salary" FROM employees WHERE salary < (SELECT MAX(salary) FROM employees WHERE job_id LIKE 'MK\_REP'ESCAPE'\');

Page 36: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 32

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 3. SELECT song_id FROM d_play_list_items WHERE event_id= (SELECT event_id FROM d_play_list_items WHERE song_id = 45); 4. SELECT c.first_name, c.last_name, d.name FROM d_clients c, d_events d WHERE c.client_number = d.client_number and d.cost > (SELECT cost FROM d_events WHERE id = 100);

Page 37: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 33

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 5. SELECT last_name FROM f_staffs WHERE salary > (SELECT salary FROM f_staffs WHERE id = 12);

Page 38: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 34

Lesson 4 - INSERT Statement

Lesson 4 - INSERT Statement

Lesson Preparation

This section begins the SQL syntax commonly referred to as data manipulation language (DML). Before beginning the section, it would be beneficial for students to do a DESCRIBE tablename for each table in the DJs on Demand and Global Fast Foods databases. Even though data may appear to be a number value in a row, it may be stored as CHAR or VARCHAR2 and require single quotation marks in the DML statements. This may eliminate problems that students can encounter, such as “it doesn't run!“

What to Watch For Students will try to insert values into a table without checking the data types or null value requirements. Discourage frivolous data! Use student names to generate interest.

Page 39: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 35

Connections Extension: Students can begin creating an address-book table of their family, relatives, and friends. The table would be useful for addressing graduation announcements, holiday cards, birthdays, or party invitations. Use the CREATE TABLE format example shown. Don't worry about null values or primary keys. The idea is to create a table and use the INSERT statement to populate it. CREATE TABLE ADDRESS_BOOK ( first_name VARCHAR2(15), last_name VARCHAR2(15), street_address VARCHAR2(20), city VARCHAR2(15), state VARCHAR2(15), country VARCHAR2(15), zip_postal_code VARCHAR2(15), phone_number VARCHAR2(15), email VARCHAR2(20), birthday DATE) ;

Page 40: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 36

What Will I Learn?

What Will I Learn?

What Will I Learn?

Students will be excited about being able to make changes to the databases. Let them know that, if they alter or delete tables in their schema, they will be responsible for restoring them. Help them start thinking like real DBAs. To discourage students from creating frivolous examples, give them the specific data provided in the practice problems for each example. This will make checking for understanding easier. To keep their schema tables in their original state, have students make a copy of each table. Use the following statement to accomplish this task. Name each table copy_tablename. Students should use the copy tables for the DML practices in the following lessons. The table copies do not inherit the associated primary- to foreign-key integrity rules (relationship constraints) of the original tables. The column data types, however, are inherited in the copied tables. Using copies of the original tables will eliminate having to deal with errors when trying to enter foreign-key values before primary keys are established or deleting primary keys when foreign keys exist. In subsequent sections, the constraints will be added to the copy tables. CREATE TABLE copy_employees AS (SELECT * FROM employees); To verify and view the copy_tablename table, use the following SELECT statement: SELECT * FROM copy_employees;

Page 41: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 37

Why Learn It?

Why Learn It?

Why Learn It?

Use the graphic to review the structure of a subquery. Point out the IN operator. Explain that in this lesson students will use a new set of operators to process subqueries that return more than one value.

Page 42: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 38

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Begin this lesson with a review question. Why do we use ANY and ALL with subqueries? Answer: When the inner query is expected to return more than one row, IN, ANY, and ALL are used in the WHERE clause of the outer query. The statement WHERE id = multiple values doesn't make sense: ID can be equal to only one value. If multiple values are expected from the inner query, then IN, ANY, and ALL are appropriate.

Page 43: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 39

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Remind students that if they are unsure about the data type of a column, they can do a DESCRIBE tablename. This is important because number values should not be enclosed in single quotes. Implicit conversion may take place for numeric values assigned to NUMBER data type columns if single quotes are included. For example, although the literal string '10' has data type CHAR, Oracle automatically converts it to the NUMBER data type if it appears in a numeric expression. This conversion may not be what you wanted. You may want 10 to remain a CHAR data type.

Page 44: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 40

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Review the graphic with students. Briefly explain the Type column and the different data types. Point out the Length column and the Precision column for numbers: precision is the total number of decimal digits, and the scale is the number of digits to the right of the decimal place.

Page 45: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 41

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

The example attempts to enter data into the table, but other columns that cannot have null values have been left out of the INSERT statement.

Page 46: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 42

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Ask students to insert the copy_employees data and then execute a SELECT * FROM copy_employees for employee_id 1001 only. Compare student results with the example shown in the graphic.

Page 47: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 43

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Discuss the graphic with students.

Page 48: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 44

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Explain syntax to students.

Page 49: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 45

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Explain syntax to students.

Page 50: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 46

Try It / Solve It

Try It / Solve It

Try It / Solve It

Students should execute DESC tablename before doing this INSERT to view the data types for each column. VARCHAR2 data-type entries need single quotation marks in the VALUES statement. 1. Students will enter the four new rows to the copy_d_cds table using an explicit INSERT statement. Demonstrate inserting the first CD using an explicit INSERT statement: INSERT INTO copy_d_cds(cd_number, title, producer, year) VALUES (97, 'Celebrate The Day', 'R&B Inc.', 2003); To verify the entry, SELECT* FROM copy_d_cds/ 2. Demonstrate inserting the first song using an implicit INSERT statement: INSERT INTO copy_d_songs VALUES(52, 'Surfing Summer', null, null, 12); 3. Demonstrate inserting a new client using: INSERT INTO copy_d_clients VALUES(6655, 'Ayako', 'Dahish', 3608859030, '[email protected]');

Page 51: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 47

4. The COST column is mandatory, but the cost is not known at the time of insert. Zero (0) will have to be inserted as the default cost. Demonstrate inserting a new events using: INSERT INTO copy_d_events (ID, NAME, EVENT_DATE, DESCRIPTION, COST, VALUE_ID, PACKAGE_CODE, THEME_CODE, CLIENT_NUMBER) VALUES(110, 'Ayako Anniversary', TO_DATE('07-JUL-04','DD-MON-RR'), 'Party for 50, sixties dress, decorations', NULL, 0, 245,79,240,6655);

Page 52: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 48

Try It / Solve It

Try It / Solve It

Try It / Solve It

Students should execute DESC tablename before doing this INSERT to view the data types for each column. VARCHAR2 data-type entries need single quotation marks in the VALUES statement. 1. Students will enter the four new rows to the copy_d_cds table using an explicit INSERT statement. Demonstrate inserting the first CD using an explicit INSERT statement: INSERT INTO copy_d_cds(cd_number, title, producer, year) VALUES (97, 'Celebrate The Day', 'R&B Inc.', 2003); To verify the entry, SELECT* FROM copy_d_cds/ 2. Demonstrate inserting the first song using an implicit INSERT statement: INSERT INTO copy_d_songs VALUES(52, 'Surfing Summer', null, null, 12); 3. Demonstrate inserting a new client using: INSERT INTO copy_d_clients VALUES(6655, 'Ayako', 'Dahish', 3608859030, '[email protected]');

Page 53: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 49

4. The COST column is mandatory, but the cost is not known at the time of insert. Zero (0) will have to be inserted as the default cost. Demonstrate inserting a new events using: INSERT INTO copy_d_events (ID, NAME, EVENT_DATE, DESCRIPTION, COST, VALUE_ID, PACKAGE_CODE, THEME_CODE, CLIENT_NUMBER) VALUES(110, 'Ayako Anniversary', TO_DATE('07-JUL-04','DD-MON-RR'), 'Party for 50, sixties dress, decorations', NULL, 0, 245,79,240,6655);

Page 54: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 50

Lesson 5 - Updating Column Values and Deleting Rows

Lesson 5 - Updating Column Values and Deleting Rows

Lesson Preparation

Any time you have extra time in class, have students work on the self-test software questions in the section called "Manipulating Data." Remind students to use the copy tables for the following practices.

What to Watch For Differentiating between each of the DML statements that use subqueries to make database changes can be challenging. Do the practice problems and identify which type of subquery is needed: - Subquery to insert rows - Subquery in place of a table name - Subquery to copy rows from another table - Subquery to update rows based on another table - Subquery to delete rows based on another table

Page 55: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 51

Connections Students can continue adding, updating, and deleting entries in their address-book table. Ask them to put you in as a teacher with your school address. Don't give all your information at one time. Students can do UPDATE statements as you add information.

Page 56: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 52

What Will I Learn?

What Will I Learn?

What Will I Learn?

Students are using copies of the original database tables. The foreign-key and primary-key integrity constraints were not carried over when the copies were made. Discuss what would occur in tables where the constraints were established. If the primary/foreign-key constraints were carried over, a value in a foreign-key column could not be inserted into a table if a corresponding primary-key value did not exist. Conversely, a primary-key value could not be deleted if foreign-key dependencies existed (except for foreign keys defined with ON DELETE CASCADE). Ask students why they think a primary-key value can't be deleted if foreign-key values are present. It would violate referential integrity. In Section 10, students will add primary and foreign-key constraints to the copied tables. At that point, they will do INSERT and DELETE statements that will have to take primary and foreign-key constraints into consideration. This section's objective is for students to be able to execute UPDATE and DELETE statements only.

Page 57: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 53

Why Learn It?

Why Learn It?

Why Learn It?

Encourage students to "think like a professional." This will help curb problems with students deliberately deleting tables in their schema and depending on you to help restore them. Also, remind them to use only their copy_tablename tables in all DML transactions.

Page 58: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 54

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Discuss the question: What do you predict would be updated in the table in the following transaction? UPDATE copy_f_customers SET phone_number='9876543210'; Answer: All rows in the table will be changed! Ask students what is different between the INSERT statement and the UPDATE statement. Answers include: INSERT uses a VALUES list; UPDATE uses a SET statement

Page 59: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 55

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Step through the purpose of the subquery. The UPDATE statement is designed to change the salary and staff type of an employee (ID = 12) to the same information as another employee (ID = 9). The subquery retrieves the "other employee" information first, which is then used in the UPDATE and SET clauses. Ask students to write a query for the graphic. UPDATE copy_f_food_items SET price =1.20 WHERE food_item_number =90; UPDATE copy_f_food_items SET price = 3.85 WHERE food_item_number =93;

Page 60: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 56

Tell Me / Show Me

Tell Me / Show Me

Page 61: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 57

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Integrity constraints will be discussed in depth in subsequent sections.

Page 62: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 58

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

What do you predict will be deleted if the WHERE clause is eliminated from the prior example? Answer: All rows in the table are deleted if you omit the WHERE clause.

Page 63: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 59

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Warn students of the power of the DELETE statement and the effect it can have on their schema tables and rows. Encourage students to make copies of each table for experimentation purposes.

Page 64: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 60

Tell Me / Show Me

Tell Me / Show Me

Page 65: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 61

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 1. UPDATE copy_f_food_items SET price = 3.75 WHERE description = 'Strawberry Shake'; UPDATE copy_f_food_items SET price =1.20 WHERE description = 'Fries'; 2. UPDATE copy_f_staffs SET overtime_rate = NVL(overtime_rate,0) + .75 WHERE id = (SELECT id FROM copy_f_staffs WHERE first_name = 'Bob' and last_name = 'Miller'); or

Page 66: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 62

UPDATE copy_f_staffs SET overtime_rate = NVL(overtime_rate,0) + 0.75 WHERE last_name = 'Miller' AND first_name = 'Bob';

UPDATE copy_f_staffs SET overtime_rate = overtime_rate + .85 WHERE id = (SELECT id FROM copy_f_staffs WHERE first_name = 'Sue' and last_name = 'Doe');

UPDATE copy_f_staffs SET overtime_rate = overtime_rate + .85 WHERE last_name = 'Doe' AND first_name = 'Sue';

Page 67: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 63

Try It / Solve It

Try It / Solve It

Try It / Solve It

4. INSERT INTO copy_f_customers(id, first_name, last_name, address, city, state, zip, phone_number) VALUES (145, 'Katie', 'Hernandez', '92 Chico Way', 'Los Angeles', 'CA', 98008, 8586667641) INSERT INTO copy_f_customers(id, first_name, last_name, address, city, state, zip, phone_number) VALUES (225, 'Daniel', 'Spode', '1923 Silverado Street', 'Denver', 'CO', 98107, 4258879009) You cannot insert the Adam Zum because the zip cannot be null. 5. UPDATE copy_f_staffs SET salary = (SELECT salary FROM copy_f_staffs WHERE first_name = 'Bob' and last_name = 'Miller') WHERE first_name = 'Sue' and last_name ='Doe';

Page 68: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 64

6. INSERT INTO copy_f_staffs(id, first_name, last_name,birthdate,salary, overtime_rate, training, staff_type, manager_id, manager_budget, manager_target) VALUES (25, 'Kai', 'Kim', '03-NOV-88', 6.75, null, null, 'Order Taker', null, null, null)

Page 69: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 65

Try It / Solve It

Try It / Solve It

Try It / Solve It

7. UPDATE copy_f_staffs SET manager_id = (SELECT manager_id FROM copy_f_staffs WHERE first_name = 'Sue' AND last_name = 'Doe') WHERE ID = 25; 8. Department 60 cannot be deleted because it contains a primary key that is used as a foreign key in another table. Explain: This is a violation of an integrity constraint. 9. DELETE from copy_f_staffs WHERE ID = 25; OR

Page 70: Database Programming - Section 7€¦ · Database Programming - Section 7 Page 1 Lesson 1 - Multiple-Row Subqueries Lesson 1 - Multiple-Row Subqueries Lesson Preparation Review Study

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 7 Page 66

DELETE FROM copy_f_staffs WHERE last_name = 'Kai' AND first_name = 'Kim' SELECT * FROM copy_f_staffs;