database programming - section 8 - overview | oracle...
TRANSCRIPT
Database Programming - Section 8 Instructor Guide
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page i
Table of Contents
Database Programming - Section 8................................................................................................................1 Lesson 1 - DEFAULT Values and the MERGE Statement ...........................................................................1 What Will I Learn? ........................................................................................................................................3 Why Learn It?................................................................................................................................................4 Tell Me / Show Me........................................................................................................................................5 Try It / Solve It ..............................................................................................................................................9 Lesson 2 - Practice Exercises ........................................................................................................................12 What Will I Learn? ........................................................................................................................................13 Why Learn It?................................................................................................................................................14 Tell Me / Show Me........................................................................................................................................15 Try It / Solve It ..............................................................................................................................................16 Lesson 3 - 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 4 - Creating Tables ............................................................................................................................24 What Will I Learn? ........................................................................................................................................25 Why Learn It?................................................................................................................................................26 Tell Me / Show Me........................................................................................................................................27 Try It / Solve It ..............................................................................................................................................33 Lesson 5 - Using Data Types.........................................................................................................................36 What Will I Learn? ........................................................................................................................................37 Why Learn It?................................................................................................................................................38 Tell Me / Show Me........................................................................................................................................39 Try It / Solve It ..............................................................................................................................................40
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 1
Lesson 1 - DEFAULT Values and the MERGE Statement
Lesson 1 - DEFAULT Values and the MERGE Statement
Lesson Preparation
Explain the term "schema" as a collection of database objects, such as tables, constraints, and privileges. Each student's work will be stored in his/her own schema. In order for others to view or alter objects in a user's schema, the owner must grant permissions. Tell students that permissions will be studied in an upcoming section. The Try It / Solve It activity is an extension of the history of the Internet from data modeling.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 2
What to Watch For The MERGE statement is challenging for students. Students will understand the concept of UPDATE or INSERT but the syntax structure is difficult. Break the statement into a description that has to tell what and where, a join, and an INSERT statement. MERGE INTO what table USING what table/view/subquery ON what join condition WHEN MATCHED THEN UPDATE SET col1 = col2 value.... WHEN NOT MATCHED THEN INSERT(column_list) VALUES(column_values)
Connections Extension Activity In the past few years, there has been increased legislation by governments related to issues of the Internet. In the United States, the http://www.cdt.org/legislation/ website provides an up-to-date guide of current legislation related to the Internet. Topics such as spam, security, piracy, and privacy, are current interests. In Europe, the search keywords "internet legislation Europe" or "internet legislation Asia" provide a more global picture of current legislation on a worldwide scale. Assign students to investigate what is being done by government to control crime and misconduct on the Internet.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 3
What Will I Learn?
What Will I Learn?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 4
Why Learn It?
Why Learn It?
Why Learn It?
A data warehouse is a collection of data designed to support business-management decision making. Data warehouses contain a wide variety of data, such as sales data, customer data, payroll, accounting, and personnel data, that presents a coherent picture of business conditions at a single point in time. Typically, a data warehouse is housed on an enterprise mainframe server.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 5
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
Students will need to do execute a DESCRIBE tablename to view column attributes in HTML DB. Column attributes can also be accessed by clicking the "Browse " tab on the top right of the main page. Select the tablename under "Database Object Results." If a column in a table does not have DEFAULT specified under the column attribute "Default," students will have to make changes to the column attributes before being able to set a default value. Practice 1 in Try It/Solve It steps through the process for the COPY_PROMOTIONAL_MENUS table. Do this practice before going on. **CURRVAL and NEXTVAL will be explained in Section 12.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 6
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
Students will need to do execute a DESCRIBE tablename to view column attributes in HTML DB. Column attributes can also be accessed by clicking the "Browse " tab on the top right of the main page. Select the tablename under "Database Object Results." If a column in a table does not have DEFAULT specified under the column attribute "Default," students will have to make changes to the column attributes before being able to set a default value. Practice 1 in Try It/Solve It steps through the process for the COPY_PROMOTIONAL_MENUS table. Do this practice before going on. **CURRVAL and NEXTVAL will be explained in Section 12.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 7
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
Explain the MERGE syntax and the use of aliases.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 8
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
Explain the MERGE syntax and the use of aliases.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 9
Try It / Solve It
Try It / Solve It
Try It / Solve It
1c. INSERT INTO copy_f_promotional_menus (code, name, start_date, end_date, give_away) VALUES( 120, 'New Customer', DEFAULT, '01-JUN-05', '10% discount coupon') 2b. CREATE TABLE manager_copy_d_cds AS (SELECT * FROM d_cds) 2c. INSERT INTO copy_d_cds (cd_number, title, producer, year) VALUES( 120, 'Hello World Here I Am', 'Middle Earth Records', '1998') 2d. MERGE INTO manager_copy_d_cds c USING d_cds d ON (c.cd_number = d.cd_number) WHEN MATCHED THEN UPDATE SET c.year = d.year, c.title = d.title, c.producer = d.producer WHEN NOT MATCHED THEN INSERT VALUES (d.cd_number, d.title, d.producer, d.year);
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 10
Try It / Solve It
Try It / Solve It
Try It / Solve It
1c. INSERT INTO copy_f_promotional_menus (code, name, start_date, end_date, give_away) VALUES( 120, 'New Customer', DEFAULT, '01-JUN-05', '10% discount coupon') 2b. CREATE TABLE manager_copy_d_cds AS (SELECT * FROM d_cds) 2c. INSERT INTO copy_d_cds (cd_number, title, producer, year) VALUES( 120, 'Hello World Here I Am', 'Middle Earth Records', '1998') 2d. MERGE INTO manager_copy_d_cds c USING d_cds d ON (c.cd_number = d.cd_number) WHEN MATCHED THEN UPDATE SET c.year = d.year, c.title = d.title, c.producer = d.producer WHEN NOT MATCHED THEN INSERT VALUES (d.cd_number, d.title, d.producer, d.year);
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 11
Try It / Solve It
Try It / Solve It
Try It / Solve It
The list of different things one can do using the Internet should not be duplicates of the same task. For example, buying a book from Amazon.com is the same as buying a book from Borders. Encourage students to think of different things such as: enable world market for business, communicate with their friends, send homework to their teacher, find information without having to leave the house, visit places around the world such as the Cairo Museum, see the Eiffel Tower, check the traffic on the Golden Gate Bridge, use virtual auctions such as eBay, search airline ticket fares, see live new clips from around the world, instant news, etc. Negative effects of the Internet include: wasting time, crime, pornography, exploitation, hacking databases, ability to outsource jobs to other countries, identity theft, privacy invasion.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 12
Lesson 2 - Practice Exercises
Lesson 2 - Practice Exercises
Lesson Preparation
Make copies of the database tables for Try It / Solve It problem 1 to use to discuss the practice exercises.
What to Watch For If students have issues adding this data, they may not have done a DESCRIBE tablename to verify what data types go into each column. Keep in mind that the copied tables have no constraints at this point. It is not important what order the data is added to the three tables. Constraints will be added in Section 10.
Connections None.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 13
What Will I Learn?
What Will I Learn?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 14
Why Learn It?
Why Learn It?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 15
Tell Me / Show Me
Tell Me / Show Me
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 16
Try It / Solve It
Try It / Solve It
Try It / Solve It
Answers: 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); Don't accept this assignment until the correct output is produced. Students should have added three employees to the o_employees table, added the 'HR_MAN' job to the o_jobs table, and added the 'Human Resources’ department to the o_departments table. Verify that students were successful in producing the correct output (ask them to print or show you their output) Changes to these tables will be done in Lesson 3.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 17
Try It / Solve It
Try It / Solve It
Try It / Solve It
Answers: 3. INSERT INTO o_employees (employee_id, first_name, last_name, email, hire_date, job_id) VALUES(210, 'Ramon', 'Sanchez', 'RSANCHEZ', SYSDATE, 'HR_MAN'); INSERT INTO o_employees (employee_id, first_name, last_name, email, hire_date, job_id) VALUES(211, 'Tai', 'Sugita', 'TSUGITA', SYSDATE, 'HR_MAN'); INSERT INTO o_employees (employee_id, first_name, last_name, email, hire_date, job_id) VALUES(212, 'Alina', 'Arcos', 'AARCOS', SYSDATE, 'HR_MAN'); 4. INSERT INTO o_departments(department_id, department_name) VALUES (210,'Human Resources'); Don't accept this assignment until the correct output is produced. Students should have added three employees to the o_employees table, added the 'HR_MAN' job to the o_jobs table, and added the 'Human Resources’ department to the o_departments table. Verify that students were successful in producing the correct output (ask them to print or show you their output). Changes to these tables will be done in Lesson 3.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 18
Lesson 3 - Practice Exercises and Quiz
Lesson 3 - Practice Exercises and Quiz
Lesson Preparation
None. What to Watch For
Students should achieve 70% or better on the quiz. Connections
None.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 19
What Will I Learn?
What Will I Learn?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 20
Why Learn It?
Why Learn It?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 21
Tell Me / Show Me
Tell Me / Show Me
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 22
Try It / Solve It
Try It / Solve It
Try It / Solve It
Answers: UPDATE o_employees SET phone_number = '360.509.7132' WHERE employee_id = 210; UPDATE o_departments SET location_id = 1700 WHERE department_id= 210; UPDATE o_employees SET department_id = 210 WHERE employee_id in (210,211,212); UPDATE o_employees SET salary = 5000 WHERE employee_id = 210; UPDATE o_employees SET salary = 5100
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 23
WHERE employee_id = 211; DELETE FROM o_employees WHERE employee_id = 212; Additional information: Verify that the new changes have been made to the o_tables as specified. Explain that DML statements should be made to primary-key values such as employee ID instead of last name or first name just in case two employees have the same name. Two employees won't have the same ID. For example, in a large company, two employees may be named Jennifer James, but each employee will have a different employee identification number. When changes are made to Jennifer James's email address, it is best to use the query shown on the left in the following example rather than the query shown on the right. UPDATE o_employees SET email ='[email protected]' WHERE employee_id = 210; UPDATE o_employees SET email ='[email protected]' WHERE last_name = 'James';
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 24
Lesson 4 - Creating Tables
Lesson 4 - Creating Tables
Lesson Preparation
The practice exercises for this section are based on tables that students create exclusively for this lesson. Tell students to be careful not to alter the other tables in their schema. Students should preface each new table they create with their name or initials -- for example, "gg_emp" or "marie_dept.“
What to Watch For Encourage students to use naming conventions. Demonstrate your requirements. Students will want to create all kinds of tables. Encourage appropriate business applications. Give extra credit for tables such as Personal CD Collection, Addresses, Rental Car Company, Collections, or tables noted in "Connections.“
Connections Using examples from data modeling, students should be able to create individual tables, add data, and execute queries. Suggested examples include: Entity Animals from Animal Shelter Activity, Entity Movies from Video Store, or Entity Members from International Oracle User's Group. There are endless examples of tables that students could make that can be found on the Internet. Ask students to bring in examples. This is a great way to practice DML, DCL, and DDL statements. Encourage students to bring in interesting table ideas.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 25
What Will I Learn?
What Will I Learn?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 26
Why Learn It?
Why Learn It?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 27
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
When students begin creating their own tables, it is important to reinforce naming conventions in SQL. Responsible programmers use best practices by keeping quality, performance, and maintainability in mind. It helps make code more readable and understandable by others. To prevent frivolous examples, demonstrate to students your policy for proper naming.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 28
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
Explain to students that they will learn more about data types in the next lesson. Read and briefly review CHAR (fixed-length character data), VARCHAR2 (variable-length character data), DATE, and NUMBER
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 29
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
Point out to students the need to create a column alias for those columns in the subquery that contain expressions. This subquery syntax provides an easy way for students to create copies of the database tables. Ask students why they think only the data-type definitions are passed on to the new table from a subquery. Suggested answer: The new table could be being used in a different context without association to existing PK -FK relationships. Point out that in this class, students will use copies of the original database tables so they retain the integrity of the original tables.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 30
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
Explain that the graphic is only a small part of the data dictionary. Ask students to access the data dictionary in HTML DB to view its extensive listings.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 31
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
Metadata is a component of data that describes the data. It is "data about data." Metadata describes the content, quality, condition, and other characteristics of data. Without proper documentation, a data set is incomplete. sparc.ecology.uga.edu/webdocs/1/glossary.htm
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 32
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
Before continuing with this lesson, ask students to query the data dictionary using the SQL query syntax. Also, use the HTML DB application to view the data dictionary. From the SQL Workshop screen, click the Browse menu tab, and click "Browse Data Dictionary" in the Tasks box on the right.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 33
Try It / Solve It
Try It / Solve It
Try It / Solve It
Answers: 1.
2. CREATE TABLE grad_candidates (student_id NUMBER(6), last_name VARCHAR2(15), first_name VARCHAR2(15), credits NUMBER (3), graduation_date DATE);
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 34
3. DESCRIBE grad_candidates; 4. CREATE TABLE smith_table AS (SELECT student_id,last_name, first_name, credits, graduation_date FROM grad_candidates);
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 35
Try It / Solve It
Try It / Solve It
Try It / Solve It
Answers: 5.
Make these questions that the students must answer, i.e. “what code would you use to see the names of all tables owned by you?” To see the names of tables owned by the user (you): SELECT table_name FROM user_tables To view distinct object types owned by the user: SELECT DISTINCT object_type FROM user_objects; To view all objects owned by the user: SELECT* FROM user_catalog; 6. INSERT INTO smith_table (student_id,last_name,first_name,credits,graduation_date) VALUES ( ANSWERS WILL VARY...)
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 36
Lesson 5 - Using Data Types
Lesson 5 - Using Data Types
Lesson Preparation
Review the Internet resources listed in the Try It/Solve It "What's in Your Future?" activity. What to Watch For
The time stamp and time zone data type needs strong emphasis and repetition in coding because the concept is confusing to some students.
Connections Relate data types to data-modeling entities. Ask students to identify the data types that would be associated with the attributes, if these attributes were those defined in a table. Use the Internet resource http://databaseanswers.com for examples of many different ERDs. Note the examples in the graphic.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 37
What Will I Learn?
What Will I Learn?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 38
Why Learn It?
Why Learn It?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 39
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
Begin this lesson with a question from previous material: How do you create a table called "MyTable" with the following columns: Id, Name, Hobbies, Favorite Book, Favorite Song, and Lucky Number? Answer: CREATE TABLE myTable (id NUMBER (4), name VARCHAR2 (15), hobbies VARCHAR2(30), favorite_book VARCHAR2(15), favorite_song VARCHAR2(15), lucky_num NUMBER (3)); The list of data types may be overwhelming. Briefly review them with students. Concentrate on the most common ones: VARCHAR2, NUMBER, and DATE. The TIMESTAMP data types will be discussed in depth with code examples provided to explain them.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 40
Try It / Solve It
Try It / Solve It
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 41
Try It / Solve It
Try It / Solve It
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 42
Try It / Solve It
Try It / Solve It
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 43
Try It / Solve It
Try It / Solve It
Tell Me / Show Me
Discuss with students how an organization might use time stamps and time zones. Ask students the following questions to pique their thinking about time and its significance in business transactions. Give extra credit to students who can find a definitive answer to any one of the questions. When do banks apply interest to your savings account? How do airlines schedule international flights across time zones? How do stock transactions take place in Hawaii at the New York Stock Exchange? Who or what determines the time that stock is bought or sold? If a baby is born on a ship somewhere on the ocean, how is the time recorded in an official birth database? If you are in a rocket traveling at the speed of light, what time is it on your watch? What time is it on earth? (Possible answer: Your space clock will slow down; the earth clock remains the same.) When is time important and when is time not important?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 44
Try It / Solve It
Try It / Solve It
Try It / Solve It
Use the examples in Tell Me/Show Me for practice exercise 1. Syntax for Question 1. Answers will vary. 1a. CREATE TABLE time_example (order_date TIMESTAMP WITH LOCAL TIME ZONE); INSERT INTO time_example VALUES('15-NOV-03 09:34:28 AM'); SELECT * FROM time_example; 1b. CREATE TABLE time_example2 (loan_duration INTERVAL YEAR (3) TO MONTH); INSERT INTO time_example2 (loan_duration) VALUES (INTERVAL '120' month(3)); SELECT TO_CHAR(sysdate+loan_duration, 'dd-mon-yyyy') FROM time_example2;
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 45
1c. CREATE TABLE time_example3 (day_duration INTERVAL DAY (3) TO SECOND); INSERT INTO time_example3 (day_duration) VALUES (INTERVAL '180' day(3)); SELECT sysdate + day_duration "Half Year" FROM time_example3;
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 8 Page 46
Try It / Solve It
Try It / Solve It
Try It / Solve It
In this activity, students take a futuristic view of technology and the jobs predicted over the next 20 years. How many times will they have changed jobs in 20 years? Is there a future job for their skills today? Explain that many of the jobs of the future haven't been invented yet. Keeping up with the rapidly changing workforce demands and requirements is a lifelong process. The Internet resources include: http://www.careerplanner.com/Career-Articles/Top_Jobs.htm http://www.readersdigest.ca/mag/2000/06/living_job.html A dozen jobs with NO future: by Susan Aaron The Learning Coach Type "a dozen jobs with no future" in a search engine to find this article. It appears on several sites. http://www.monsterlearning.monster.com/resources/articles/wnl/archives/nofuture.asp http://techrepublic.com.com/5100-6299-1041371.html "Half of the hottest jobs of the future don’t even exist yet."