Upload
others
View
56
Download
0
Embed Size (px)
Citation preview
Database Programming - Section 9 Instructor Guide
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page i
Table of Contents
Database Programming - Section 9................................................................................................................1 Lesson 1 - Modifying a Table........................................................................................................................1 What Will I Learn? ........................................................................................................................................3 Why Learn It?................................................................................................................................................4 Tell Me / Show Me........................................................................................................................................5 Try It / Solve It ..............................................................................................................................................13 Lesson 2 - Practice Exercises ........................................................................................................................16 What Will I Learn? ........................................................................................................................................17 Why Learn It?................................................................................................................................................18 Tell Me / Show Me........................................................................................................................................19 Try It / Solve It ..............................................................................................................................................20 Lesson 3 - Practice Exercises and Quiz .........................................................................................................22 What Will I Learn? ........................................................................................................................................23 Why Learn It?................................................................................................................................................24 Tell Me / Show Me........................................................................................................................................25 Try It / Solve It ..............................................................................................................................................26 Lesson 4 - Exam 2 Review ............................................................................................................................27 What Will I Learn? ........................................................................................................................................28 Why Learn It?................................................................................................................................................29 Tell Me / Show Me........................................................................................................................................30 Try It / Solve It ..............................................................................................................................................31 Lesson 5 - Exam 2 Information .....................................................................................................................32 What Will I Learn? ........................................................................................................................................33 Why Learn It?................................................................................................................................................34 Tell Me / Show Me........................................................................................................................................35 Try It / Solve It ..............................................................................................................................................36
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 1
Lesson 1 - Modifying a Table
Lesson 1 - Modifying a Table
Lesson Preparation
The tables used for the practice exercises in this section were created in Section 8, Lesson 2 and 3. The tables were copied from the Oracle database and named o_employees, o_departments, and o_jobs. If students no longer have these tables in their schema, students should re-create them before proceeding to the Try It/Solve It practice exercises. If students need to re-create the tables, use the four steps below. This lesson does not require any more information than what is created in these four steps. 1. Create the three o_tables -- jobs, employees, and departments -- using the syntax: CREATE TABLE o_jobs AS (SELECT * FROM jobs); 2. Add the Human Resources job to the jobs table: INSERT INTO o_jobs (job_id, job_title, min_salary, max_salary) VALUES('HR_MAN', 'Human Resources Manager', 4500, 5500); 3. Add the three new employees to the employees table: INSERT INTO o_employees (employee_id, first_name, last_name, email, hire_date, job_id) VALUES(210, 'Ramon', 'Sanchez', 'RSANCHEZ', SYSDATE, 'HR_MAN');
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 2
4. Add Human Resources to the departments table: INSERT INTO o_departments(department_id, department_name) VALUES (210,'Human Resources');
What to Watch For Warn students that DROP COLUMN and DROP TABLE statements are permanent. When experimenting with these commands, remind students to create copies of their tables. Use the copy subquery command: CREATE TABLE copy_tablename AS (SELECT * FROM tablename);
Connections Relate the DDL statements learned to the school's student and/or grades database. Ask students to give an example for each of the following: CREATE TABLE - Possible Answer: School now needs to track absence data for each student using a database to replace an outdated manual system. ALTER TABLE - Possible Answer: An existing column may contain sensitive student data such as health conditions or criminal convictions that can no longer legally be in the student table. DROP TABLE - Possible Answer: The table that recorded lunchroom data has now been outsourced to a private company and the table is no longer needed. RENAME - Possible Answer: Another table has a similar name and the developers are often confusing the two. TRUNCATE - Possible Answer: The row data is no longer needed. Storage space is needed. COMMENT - Possible Answer: Many people access this table. Using comments to explain the relationships between tables is helpful for the programmer/developers. Using comments clarifies understanding.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 3
What Will I Learn?
What Will I Learn?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 4
Why Learn It?
Why Learn It?
Why Learn It?
Ask students how many of them have tables in their schema that they created but no longer want or would like to change. Explain that this lesson shows them how to make changes to tables.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 5
Tell Me / Show Me
Tell Me / Show Me
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 6
Tell Me / Show Me
Tell Me / Show Me
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 7
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
Begin this lesson with a review question. What data types would you use for the ‘MyTable’ columns: Id, Name, Hobbies, Favorite Book, Favorite Song, and Lucky Number? Possible answer: Id NUMBER(3), Name VARCHAR2(15), Hobbies VARCHAR2(30), Favorite Book VARCHAR2(15), Favorite Song VARCHAR2(15), Lucky Number NUMBER(3), The examples in this section use copy_f_staffs. Remind students to create copies of tables in the database that they can alter, add columns, update, and drop. The original tables remain the same. The CREATE TABLE from subquery syntax can be used: CREATE TABLE copy_tablename AS (SELECT * FROM tablename);
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 8
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
Ask students to think of a reason to set a column as unused. Possible responses: A business has a table that has a column that held inventory item numbers that are no longer used since being replaced with barcode numbers; department names have been replaced with department numbers.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 9
Tell Me / Show Me
Tell Me / Show Me
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 10
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
Students will learn about indexes, views, and synonyms in later sections. Explain that data generally appears in a table in the order in which it was originally entered. That order, however, may have nothing to do with the order in which you might later want to process or retrieve the data. In small tables, submitting a query to sort all the rows in some way may not take very long. What if you have a table with 100,000 rows? What if you have a table with a million rows? The query may take some time to process. Indexes are a way to speed up retrieval of rows by using a "pointer." The pointer provides direct and fast access to rows in a table by using an indexed path to locate data quickly. A view is a logical table based on a table or another view. A view contains no data of its own but is like a window through which data can be viewed or changed. Explain that you may not want all employees who have access to the database to be able to see the salary column in the EMPLOYEES table. A view could be created that excludes this column. Synonyms are just another name for an object such as a table. Synonyms eliminate the need to qualify the tablename with the schema name of the person who created the table. Instead of "may.employees," the table could be given a synonym such as "emp."
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 11
Tell Me / Show Me
Tell Me / Show Me
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 12
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
Review the ALTER TABLE graphic with students.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 13
Try It / Solve It
Try It / Solve It
Try It / Solve It
1. ALTER TABLE o_employees ADD (termination VARCHAR2(20) DEFAULT TO_CHAR(SYSDATE, 'Month ddth, YYYY')); 2. DELETE FROM o_employees WHERE employee_id BETWEEN 100 AND 150; 3. UPDATE o_employees SET termination = '01-AUG-04' WHERE employee_id = 206; 4. ALTER TABLE o_employees ADD start_date TIMESTAMP WITH LOCAL TIME ZONE;
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 14
Try It / Solve It
Try It / Solve It
Try It / Solve It
5. INSERT INTO o_employees (employee_id, first_name, last_name, email, hire_date, job_id, start_date) VALUES(220, 'Amy', 'Kimura','AKIMURA','15-SEP-04','HR_MAN','29-SEP-04 08:30:00 AM'); 6. ALTER TABLE o_employees SET UNUSED (commission_pct); The commission_pct column is marked so it can be dropped when system resources are lower. This column is treated as if it were dropped, even though the column data remains in the table's rows. Once marked UNUSED, this column will return no data when you run a select statement against it. 7. ALTER TABLE o_employees DROP UNUSED COLUMNS;
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 15
Try It / Solve It
Try It / Solve It
Try It / Solve It
8. COMMENT ON TABLE o_jobs IS 'New job description added'; SELECT * FROM USER_TAB_COMMENTS; 9. RENAME o_jobs to o_job_description; 10. TRUNCATE TABLE o_job_description; The TRUNCATE TABLE command removes all rows from the table and releases storage space used by the table. Ask students to write a SQL statement to SET UNUSED the birth_date column. ALTER TABLE emp_history SET UNUSED (birth_date);
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 16
Lesson 2 - Practice Exercises
Lesson 2 - Practice Exercises
Lesson Preparation
Review SQL syntax for creating tables, using data types, and modifying a table -- Section 8, Lessons 4 and 5; Section 9, Lesson 1.
What to Watch For When students can alter and drop tables, their work can disappear! Explain that they can save query commands in the HTML DB archive. This feature is useful in case students make a fatal mistake and need to start over.
Connections None.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 17
What Will I Learn?
What Will I Learn?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 18
Why Learn It?
Why Learn It?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 19
Tell Me / Show Me
Tell Me / Show Me
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 20
Try It / Solve It
Try It / Solve It
Try It / Solve It
Explain to students how you want the DJ on Demand artist's table assignment to be completed. Students should be able to list the term followed by the SQL statement they used. For example: CREATE TABLE artists (artist_id NUMBER(4), first_name VARCHAR2 (15), last_name VARCHAR2 (15), band_name VARCHAR2 (15), email VARCHAR2(15), hourly_rate NUMBER (8), song_id NUMBER(5)); Extension: Practice using the DDL and DML commands in the Worksheet from Cristy Charters. http://academy.oracle.com/pages/content/sql_course/sql_s12_l02_chambers_wksht.doc This example should be familiar because it involves banks and withdrawing money from accounts.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 21
Try It / Solve It
Try It / Solve It
Try It / Solve It
Explain to students how you want the DJ on Demand artist's table assignment to be completed. Students should be able to list the term followed by the SQL statement they used. For example: CREATE TABLE artists (artist_id NUMBER(4), first_name VARCHAR2 (15), last_name VARCHAR2 (15), band_name VARCHAR2 (15), email VARCHAR2(15), hourly_rate NUMBER (8), song_id NUMBER(5)); Extension: Practice using the DDL and DML commands in the Worksheet from Cristy Charters. http://academy.oracle.com/pages/content/sql_course/sql_s12_l02_chambers_wksht.doc This example should be familiar because it involves banks and withdrawing money from accounts.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 22
Lesson 3 - Practice Exercises and Quiz
Lesson 3 - Practice Exercises and Quiz
Lesson Preparation
Review Study Guide with students. Review Vocabulary Exercise with students. Review SQL syntax. Assign quiz after review.
What to Watch For Post-quiz review. Give a brief overview of the exam content. Students could begin the Exam Review in Lesson 4.
Connections None.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 23
What Will I Learn?
What Will I Learn?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 24
Why Learn It?
Why Learn It?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 25
Tell Me / Show Me
Tell Me / Show Me
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 26
Try It / Solve It
Try It / Solve It
Try It / Solve It
Assist students to complete assignments. Review Study Guide and Vocabulary.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 27
Lesson 4 - Exam 2 Review
Lesson 4 - Exam 2 Review
Lesson Preparation
Exam 2 tests content from Section 5, Lesson 2 "Group Functions" through Section 9, Lesson 1 "Modifying a Table." Use the study guides for review and Try It/Solve It review.
What to Watch For Students should receive a 70% or better on the quiz . Continue to help students see beyond just getting the answer. Can they tell you what concept is being tested and how they decided to solve the problem?
Connections None.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 28
What Will I Learn?
What Will I Learn?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 29
Why Learn It?
Why Learn It?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 30
Tell Me / Show Me
Tell Me / Show Me
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 31
Try It / Solve It
Try It / Solve It
Try It / Solve It
Section 9, Exam 2 Review -- Teacher Assign a few problems at a time. Review the process used to identify the required information and the order of operations to obtain the result. Avoid simply giving the answers. Relate each problem to the concept it illustrates.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 32
Lesson 5 - Exam 2 Information
Lesson 5 - Exam 2 Information
Lesson Preparation
Have students go to the Oracle iLearning outline and click SQL exam 1. This will be directly under Section 9, Lesson 5 (where we are right now). http://ilearning.oracle.com/ The time allowed for this exam is 45 minutes. Begin class by asking students if there are any last-minute questions. Answer these questions and have students begin the exam.
What to Watch For Students should achieve 70% or better on the exam. Students may retake the exam to improve their score and understanding. Post-Exam Review: Select several of the more difficult questions from the exam to review with students. Give a brief overview of what's next. After Exam 2, Section 10 deals with defining and managing primary and foreign-key contraints. Explain that referential integrity is managed using constraints. Students will learn to define constraints when tables are created or by using an ALTER TABLE statement.
Connections None.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 33
What Will I Learn?
What Will I Learn?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 34
Why Learn It?
Why Learn It?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 35
Tell Me / Show Me
Tell Me / Show Me
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 9 Page 36
Try It / Solve It
Try It / Solve It
Try It / Solve It
If all students finish the exam with class time remaining, give an overview of the content to be learned in the next section. Mention that Section 10 will address adding constraints or "rules" for entering data into tables.