database programming - section 2 - oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... ·...

72
Database Programming - Section 2 Instructor Guide

Upload: vuongdan

Post on 17-Apr-2018

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Database Programming - Section 2 Instructor Guide

Page 2: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type
Page 3: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page i

Table of Contents

Database Programming - Section 2................................................................................................................1 Lesson 1 - Conversion Functions ..................................................................................................................1 What Will I Learn? ........................................................................................................................................2 Why Learn It?................................................................................................................................................3 Tell Me / Show Me........................................................................................................................................4 Try It / Solve It ..............................................................................................................................................23 Lesson 2 - General Functions ........................................................................................................................27 What Will I Learn? ........................................................................................................................................29 Why Learn It?................................................................................................................................................30 Tell Me / Show Me........................................................................................................................................31 Try It / Solve It ..............................................................................................................................................37 Lesson 3 - Conditional Expressions ..............................................................................................................41 What Will I Learn? ........................................................................................................................................43 Why Learn It?................................................................................................................................................44 Tell Me / Show Me........................................................................................................................................45 Try It / Solve It ..............................................................................................................................................47 Lesson 4 - Practice Exercises ........................................................................................................................50 What Will I Learn? ........................................................................................................................................51 Why Learn It?................................................................................................................................................52 Tell Me / Show Me........................................................................................................................................53 Try It / Solve It ..............................................................................................................................................57 Lesson 5 - Practice Exercises ........................................................................................................................59 What Will I Learn? ........................................................................................................................................60 Why Learn It?................................................................................................................................................61 Tell Me / Show Me........................................................................................................................................62 Try It / Solve It ..............................................................................................................................................63

Page 4: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type
Page 5: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 1

Lesson 1 - Conversion Functions

Lesson 1 - Conversion Functions

Lesson Preparation

When there is extra time in class, have students work on the self-test software. What to Watch For

This is a lesson that requires considerable student practice. Check to make sure that, when students get output returned from a query, it is the correct output. Relate the idea of presenting information in a more-readable format to the process of preparing the data-modeling presentation for the clients. It was important to have the information in a format that was easily understood by everyone.

Connections Ask students to find examples of formatted data on the Internet or in the newspaper. Sources include the financial page, statistical information, grade reports, etc. Ask them to write a SQL statement to convert default format to the source format.

Page 6: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 2

What Will I Learn?

What Will I Learn?

Page 7: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 3

Why Learn It?

Why Learn It?

Why Learn It?

Ask students: How do you choose a book to read or buy? Does the cover help you make a decision? What if you open the book and there are no pictures and the type is very small. Are you still interested? Why do CDs and video tapes have colorful, interesting labels? Have you ever bought a new candy bar because the wrapper attracted your attention? As someone once said, "looks are everything"!

Page 8: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 4

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Begin this lesson with a review question: How can we find out what the date will be exactly five months from today? Answer: SELECT ADD_MONTHS(SYSDATE, 5) FROM DUAL; Before beginning this lesson's discussion of data-type conversions and the Oracle Server's capability to implicitly do data-type conversions, explain to students the difference between "implicit" and "explicit." Use the example, "When she rolled her eyes and sighed, Sue implicitly told Bill she didn't want to go the dance with him. Bill didn't understand her actions until she explicitly said, 'Bill, I already told you, I have a date for the dance!'" The dictionary defines "implicit" as something that is "implied but not directly expressed" and explicit as "clearly formulated or defined."

Page 9: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 5

In this lesson, students will explicitly define data-type conversions to ensure the reliability of SQL statements. Students may not be familiar with concept of data types. Explain that most programming languages require the programmer to declare the data type of every data object. For the data stored in a database, the SQL programmer defines a data type for every column in the database. Explain that later in the course, they will learn more about SQL data types, but for now they will be using VARCHAR2, CHAR, NUMBER, and DATE.

Page 10: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 6

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Begin this lesson with a review question: How can we find out what the date will be exactly five months from today? Answer: SELECT ADD_MONTHS(SYSDATE, 5) FROM DUAL; Before beginning this lesson's discussion of data-type conversions and the Oracle Server's capability to implicitly do data-type conversions, explain to students the difference between "implicit" and "explicit." Use the example, "When she rolled her eyes and sighed, Sue implicitly told Bill she didn't want to go the dance with him. Bill didn't understand her actions until she explicitly said, 'Bill, I already told you, I have a date for the dance!'" The dictionary defines "implicit" as something that is "implied but not directly expressed" and explicit as "clearly formulated or defined." In this lesson, students will explicitly define data-type conversions to ensure the reliability of SQL statements.

Page 11: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 7

Students may not be familiar with concept of data types. Explain that most programming languages require the programmer to declare the data type of every data object. For the data stored in a database, the SQL programmer defines a data type for every column in the database. Explain that later in the course, they will learn more about SQL data types, but for now they will be using VARCHAR2, CHAR, NUMBER, and DATE.

Page 12: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 8

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

After introducing each of the four data-type conversions, explain the format model table and the use of "fm" and "spth." STOP and allow students time to practice. Conversion answers: 1. DATE CONVERSION TO CHARACTER DATA June 19th, 2004 TO_CHAR(hire_date, 'Month ddth, YYYY') January 1, 2000 TO_CHAR(hire_date, 'fmMonth dd, YYYY') MAR 5, 2001 TO_CHAR(hire_date, 'fmMON dd, YYYY') June 17th Wednesday Nineteen Eighty-Seven TO_CHAR(hire_date, 'Month ddth Day YyYYSP')

Page 13: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 9

SELECT id, TO_CHAR(event_date, 'MONTH DD, YYYY') FROM d_events; returns: 100 MAY 14, 2004 105 APRIL 28, 2004

Page 14: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 10

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

After introducing each of the four data-type conversions, explain the format model table and the use of "fm" and "spth." STOP and allow students time to practice. Conversion answers: 1. DATE CONVERSION TO CHARACTER DATA June 19th, 2004 TO_CHAR(hire_date, 'Month ddth, YYYY') January 1, 2000 TO_CHAR(hire_date, 'fmMonth dd, YYYY') MAR 5, 2001 TO_CHAR(hire_date, 'fmMON dd, YYYY') June 17th Wednesday Nineteen Eighty-Seven TO_CHAR(hire_date, 'Month ddth Day YyYYSP')

Page 15: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 11

SELECT id, TO_CHAR(event_date, 'MONTH DD, YYYY') FROM d_events; returns: 100 MAY 14, 2004 105 APRIL 28, 2004

Page 16: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 12

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

After introducing each of the four data-type conversions, explain the format model table and the use of "fm" and "th." STOP and allow students time to practice. Conversion answers: (The date will vary depending on the SYSDATE.) SELECT TO_CHAR(SYSDATE, 'fmMonth ddth, YYYY' ) FROM DUAL; returns: August 6th, 2004 SELECT TO_CHAR(SYSDATE, 'Month dd, YYYY' ) FROM DUAL; returns: August 06, 2004

Page 17: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 13

SELECT TO_CHAR(SYSDATE, 'fmMON dd, YYYY' ) FROM DUAL; returns: AUG 6, 2004 SELECT TO_CHAR(SYSDATE, 'fmMonth ddth, Day, YyYY' ) FROM DUAL; returns: August 6th, Friday, Two Thousand Four

Page 18: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 14

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Conversion answers: $3000.00 TO_CHAR(salary, '$9999.99') 4,500 TO_CHAR(salary, '9,999') 9,000.00 TO_CHAR(salary, '9,999.99') 0004422 TO_CHAR(salary, '0009999')

Page 19: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 15

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Ask students to use the DUAL table to convert their age to a number.

Page 20: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 16

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

The fx format may require more practice. Remind students to think of fx as "format exact" so the fxdate of the format model must exactly match the format of the date being converted. Ask students to use the fx modifier to convert each of the following to the default date format. June19 2004 SELECT TO_DATE('June19 2004', 'fxMonthDD RRRR')AS Convert FROM DUAL; July312004 SELECT TO_DATE('July312004', 'fxMonthDDRRRR')AS Convert FROM DUAL;

Page 21: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 17

Using the DUAL table, ask students to format their birthday in five different ways. Remind students that they will first have to convert their "character" data to date data and then format it. If they were using a DATE data-type column from a table such as hire_date, the conversion TO_DATE before formatting would not be necessary. SELECT TO_CHAR(TO_DATE('June 19, 1990','Month dd, YYYY'),'MON DD YYYY')AS Birthday FROM DUAL;

Page 22: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 18

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

The fx format may require more practice. Remind students to think of fx as "format exact" so the fxdate of the format model must exactly match the format of the date being converted. Ask students to use the fx modifier to convert each of the following to the default date format. June19 2004 SELECT TO_DATE('June19 2004', 'fxMonthDD RRRR')AS Convert FROM DUAL; July312004 SELECT TO_DATE('July312004', 'fxMonthDDRRRR')AS Convert FROM DUAL;

Page 23: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 19

Using the DUAL table, ask students to format their birthday in five different ways. Remind students that they will first have to convert their "character" data to date data and then format it. If they were using a DATE data-type column from a table such as hire_date, the conversion TO_DATE before formatting would not be necessary. SELECT TO_CHAR(TO_DATE('June 19, 1990','Month dd, YYYY'),'MON DD YYYY')AS Birthday FROM DUAL;

Page 24: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 20

Tell Me / Show Me

Tell Me / Show Me

Page 25: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 21

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Use the following example to show the effects of the RRRR and YYYY formats: SELECT last_name, TO_CHAR(hire_date, 'DD-Mon-YYYY') FROM employees WHERE hire_date < TO_DATE('01-Jan-90', 'DD-Mon-RR'); You'll get about three rows. Examine the WHERE clause. It asks to return only those hire dates less than (before) those of 01-Jan-90. The RR used in the format model here makes sure that the date is seen as 01-Jan-90. The three rows returned were those few people who were hired before 01-Jan -90. Reformat the query: SELECT last_name, TO_CHAR(hire_date, 'DD-Mon-YYYY') FROM employees WHERE hire_date < TO_DATE('01-Jan-90', 'DD-Mon-YY');

Page 26: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 22

Now 20 rows were returned. Again, examine the format model in the WHERE clause. Using the YY format causes the date to default to the current century, and since we are in the 2000s, 01-JAN-90 is seen as 01-JAN-2090. Everyone was hired before that date, right? Ask students to research the "Millennium Bug" or "Y2K" and the concern people had about how databases recorded their information. Expect students to be a bit confused learning the RR and YY formats. Emphasize that the problem exists when character data such as 01-Jan-90 is being formatted into a default date format. Instead of using RR in the format model, YY is used. Because we are in the 21st century, the YY is interpreted in the current century as 2090. Provide additional practice by choosing a random date in either the 20th or 21st century, and ask students to provide the correct result.

Page 27: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 23

Try It / Solve It

Try It / Solve It

Try It / Solve It

1. SELECT last_name, TO_CHAR(birthdate, 'Month fmDD, RRRR')AS "Birthday" FROM f_staffs; 2. SELECT TO_DATE('January 3, 2004', 'Month dd, YYYY')as "Date" FROM DUAL; 3. SELECT 'The promotion began on the '|| TO_CHAR(start_date, 'ddspth "of" Month YYYY')as "Date" FROM f_promotional_menus WHERE code = 110; 4. SELECT 'Today is the ' ||TO_CHAR(SYSDATE, 'Ddspth "of" Month, Yyyysp') FROM DUAL;

Page 28: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 24

5. SELECT id, first_name, TO_CHAR(salary, '$999.99') FROM f_staffs; 6. SELECT first_name, last_name, TO_CHAR(salary, '$99,999.99'), TO_CHAR(salary + 2000, '$99,999.99') AS "New Salary" FROM f_staffs; 7. SELECT TO_CHAR(start_date, 'Day Month ddth') AS "Valentine's" FROM f_promotional_menus WHERE code = 110;

Page 29: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 25

Try It / Solve It

Try It / Solve It

Try It / Solve It

8. SELECT TO_CHAR(TO_DATE('25-DEC-04','dd-MON-yy'),'Month ddth, YYYY')Convert,TO_CHAR(TO_DATE('25-DEC-04','dd-MON-yy'),'MONTH DDth, YYYY')Convert,TO_CHAR(TO_DATE('25-DEC-04','dd-MON-yy'),'month ddth, YYYY')AS Convert FROM DUAL; 9. SELECT TO_CHAR(low_range, '$99999.99')AS LOW, TO_CHAR(high_range, '$99999.99')AS HIGH FROM d_packages;

Page 30: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 26

10. SELECT TO_DATE('JUNE192004','fxMONTHDDYYYY') AS BIRTHDAY FROM DUAL; A common error when formatting dates is to omit the single quotation mark. The error message is missing a right parenthesis. Demonstrate this error to students. Discuss with students why it is important, from a business perspective, for a language to have built-in data conversion capabilities. Possible answers: - International business must convert currency values. - The date and time are recorded differently across the world. Military uses a 24-hour clock, date formats are recorded as mm/dd/yyyy and as dd/mm/yyyy. - Ability to present data in more readable format is important. - Ability to enter data in one format and have the database application convert it to storage format is important. - Being able to convert data types makes the language more flexible.

Page 31: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 27

Lesson 2 - General Functions

Lesson 2 - General Functions

Lesson Preparation

None. What to Watch For

Students may be suffering from "information overload" at this point. Keep encouraging practice and use the self-test software as a break and reinforcement. Ask students to generate query examples and challenge the class.

Connections Many of the functions students learned in this lesson, they do every day. Ask: Have you ever gone to a store to buy a something you were looking for only to find out that the store didn't have it? Have you been in the lunch line only to find out that your favorite item was sold out? Did you choose something else? If you've ever had this experience, you actually were doing a NVL2 or COALESCE function. If the first choice was null, choose the second or the third.

Page 32: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 28

A lot of the power of SQL is to emulate decisions that we need to make every day. We need the date format changed to comply with how another country writes it. We need to issue payroll checks that have currency signs instead of just numbers. Ask students to draw parallels between the functions and syntax they are learning and why the function was developed in the first place. After all, if no one ever needed to give a column another name in a report, would aliases ever have been developed?

Page 33: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 29

What Will I Learn?

What Will I Learn?

Page 34: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 30

Why Learn It?

Why Learn It?

Page 35: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 31

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Begin the lesson with a review question: How would you display today’s date as: 15 January 2003? SELECT TO_CHAR(SYSDATE, 'dd Month RRRR') FROM DUAL; Students should be familiar with nested functions taught in mathematics classes. In the example, ask students to list the order of processing. Step 1: The hire date is going to have six months added to it. Step 2: The first Friday following the future day will be identified. Step 3: The default date format will be formatted to read and display the Friday in a format similar to: Friday, December 18TH, 1987, and will appear in the output under the column name "Next Evaluation."

Page 36: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 32

Tell Me / Show Me

Tell Me / Show Me

Page 37: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 33

Tell Me / Show Me

Tell Me / Show Me

Page 38: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 34

Tell Me / Show Me

Tell Me / Show Me

Page 39: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 35

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Students have not formally studied data types. Tell them that LONG is variable-length character data up to 2 gigabytes in size. The NVL2 sample code illustrates a SQL statement returning an employee's salary and income. For employees who work on commission, the income is equivalent to the salary plus the salary multiplied by the commission percentage. For those who do not work on commission, the income is equivalent to the salary. SELECT last_name, salary, NVL2(commission_pct, salary + (salary * commission_pct), salary) income FROM employees;

Page 40: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 36

Tell Me / Show Me

Tell Me / Show Me

Page 41: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 37

Try It / Solve It

Try It / Solve It

Page 42: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 38

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: Encourage the students to use aliases to make the output more readable. 1. SELECT name, NVL2(end_date,'end in two weeks', SYSDATE)AS Promotion FROM f_promotional_menus; 2. SELECT last_name,NVL(overtime_rate,0)AS "Overtime Status" FROM f_staffs; 3. SELECT last_name, TO_CHAR(NVL(overtime_rate,5.00), '$9999.99')AS "Overtime Rate" FROM f_staffs; 4. SELECT last_name, NVL(manager_id, 9999) FROM f_staffs;

Page 43: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 39

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 5. c 6.

7. Part 1: SELECT first_name||' '||last_name "Name", to_char(hire_date,'Month') "Anniversary Month" FROM employees; Part 2: SELECT first_name||' '||last_name "Name", nullif(to_char(hire_date,'Month'),'September') "Anniversary Month" FROM employees;

Page 44: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 40

8. SELECT first_name, NVL(specialty,'No Specialty')AS Specialty FROM d_partners; 9. SELECT last_name, SUBSTR(phone, 4,10) FROM d_clients; Extension: Show/discuss Demo: The Basics of Using Functions

Page 45: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 41

Lesson 3 - Conditional Expressions

Lesson 3 - Conditional Expressions

Lesson Preparation

None. What to Watch For

The CASE and DECODE functions need practice. Remind students to think of these functions as IF-THEN-ELSE decision operations. Read the syntax to them in this way.

Page 46: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 42

Connections Other programming languages use similar IF-ELSE or IF-THEN-ELSE processes. Understanding their function and use will make learning other programming languages easier. Write the following statement from Java on the board and see if students can decipher it based on the functions learned in this lesson. What will be printed out? Answer: The program starts setting "b" to FALSE; the code asks...is "b" True? If it is True, then print out "hello"; else print "goodbye." boolean b; b = false; if(b)

{ System.out.println("hello");

} else { System.out.println("goodbye");

}

Page 47: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 43

What Will I Learn?

What Will I Learn?

Page 48: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 44

Why Learn It?

Why Learn It?

Page 49: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 45

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Begin this lesson with a review question from previous material. Ask students to write a SELECT statement to show "No Comment" if an event_id on the D_PLAY_LIST_ITEMS table does not have a comment. Answer: SELECT NVL(comments, 'No Comment') FROM D_PLAY_LIST_ITEMS;

Page 50: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 46

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Students will have difficulty distinguishing between CASE and DECODE. The CASE expression is a more flexible version of the DECODE function. The CASE expression complies with ANSI SQL; DECODE is specific to Oracle syntax.

Page 51: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 47

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 1. SELECT name,ROUND(MONTHS_BETWEEN(SYSDATE, end_date),0) AS "Past Promos" FROM f_promotional_menus; 2. SELECT INITCAP(first_name) ||' ' ||INITCAP(last_name)||' '||'earns'||TO_CHAR(salary, '$99999.99')||' ' ||'but wants ' ||TO_CHAR((salary + 3000),'$99999.99') as "Wish Salary" FROM employees WHERE last_name LIKE 'Abel'; 3. SELECT id, title, duration, DECODE(duration, '2 min', 'shortest', '10 min', 'longest') AS "Play Times" FROM d_songs;

Page 52: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 48

4. SELECT last_name, department_id, salary, CASE department_id WHEN 10 THEN salary*1.25 WHEN 90 THEN salary*1.50 WHEN 130 THEN salary*1.75 ELSE salary END As "No Change" FROM employees;

Page 53: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 49

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 5. SELECT first_name, last_name, manager_id, commission_pct, COALESCE(manager_id, commission_pct, 99999) AS "Review" FROM employees WHERE department_id IN( 80,90);

Page 54: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 50

Lesson 4 - Practice Exercises

Lesson 4 - Practice Exercises

Lesson Preparation

The practice exercises in Lesson 4 and Lesson 5 prepare students for the Lesson 5 Quiz measuring the objectives for SQL functions -- case, character, number, date, conversion, general, and conditional expressions. Students should complete all practices. Review answers with students.

What to Watch For The practice exercises are more challenging. Check for understanding.

Connections None.

Page 55: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 51

What Will I Learn?

What Will I Learn?

Page 56: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 52

Why Learn It?

Why Learn It?

Page 57: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 53

Tell Me / Show Me

Tell Me / Show Me

Page 58: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 54

Tell Me / Show Me

Tell Me / Show Me

Try It / Solve It

Students will find these practices more challenging. Assist them in dissecting the problem into the information needed and the order of operations for accomplishing the task. Encourage students to use aliases to make the output more readable. Answers: 1. SELECT first_name, last_name,zip FROM f_customers WHERE LENGTH(zip) < 10; 2. a. LPAD (CDN) b. ROUND (DN) c. TRUNC (DN) d. LENGTH (C) e. LAST_DAY (D) f. INSTR (CDN) g. CONCAT (CDN)

Page 59: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 55

3. SELECT first_name, last_name, NVL(TO_CHAR(auth_expense_amt,'99999999'),'Not Approved')As "Authorization Status" FROM d_partners;

Page 60: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 56

Tell Me / Show Me

Tell Me / Show Me

Try It / Solve It

Answers: 4. Jason and Jamie forgot that the data types of the NVL elements must be the same data type! A TO_CHAR function is needed to convert the number overtime_rate to a character value. SELECT first_name, last_name, NVL(TO_CHAR(overtime_rate, '9999999'),'no overtime')As "Payrate" FROM f_staffs; 5. SELECT first_name, last_name, birthdate, SUBSTR(TO_CHAR(birthdate,'Month dd,YYYY'), 1,9)|| ' 2005' As "Send Card" FROM f_staffs;

Page 61: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 57

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 6. ___T__a. TO_CHAR is required to convert the date '03-JUN-04' to June 3, 2004 ___F__b. TO_NUMBER will convert '23-NOV-02' to use with ADD_MONTHS ___F__c. TO_DATE will convert SYSDATE to today's date ___F__d. TO_NUMBER('101', '$99999') will convert 101 to a number ___T__e. TO_CHAR(salary, '$9999.99') will convert number to character format ___T__f. TO_NUM(varchar2 column) will convert character data to a number ___T__g. TO_CHAR(SYSDATE, 'Month fmdd, yyyy') will format the date 7. SELECT first_name, last_name,ROUND(MONTHS_BETWEEN(SYSDATE,hire_date)/12 ,1)AS "Years Worked" FROM employees;

Page 62: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 58

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 8. SELECT SUBSTR(address, -5,5)AS "Old Zip", SUBSTR(address, -5,5)||'-2345' AS "New Zip" FROM d_venues WHERE ID= 105; 9. SELECT ROUND(SYSDATE, 'YEAR') AS YEAR, ROUND(SYSDATE, 'MONTH')AS MONTH, TRUNC(SYSDATE, 'YEAR')AS YEAR, TRUNC(SYSDATE, 'MONTH')AS MONTH FROM DUAL; 10. SELECT name, ROUND((SYSDATE - START_DATE),0) AS "Days" FROM f_promotional_menus; 11. SELECT LOWER(CONCAT(LOWER(SUBSTR(job_title, 1, 5)),'* ' )) "Job Description" FROM jobs; 12. The expression will be evaluated from the innermost expression outward.

Page 63: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 59

Lesson 5 - Practice Exercises

Lesson 5 - Practice Exercises

Lesson Preparation

The practice exercises are challenging. Students should achieve 70% or better on the quiz. Provide enough time to review for the quiz. After the quiz, review the most difficult questions. Encourage students to practice their skills using self-test software. Give a brief overview of what's next: Section 3, Lesson 1, "Career Explorations" -- develop a thirteenth-year plan and look into what you want your future to be. Review Section 2 of the Study Guide and Vocabulary with students. Review practice exercise answers with students. Focus on the method for solving the problem, not just the answer.

What to Watch For Students are asked to apply concepts learned in the practice exercises. The difficulty of the problems may be challenging for some students. Provide enough time to review for the quiz. After the quiz, review the most difficult questions.

Connections None.

Page 64: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 60

What Will I Learn?

What Will I Learn?

Page 65: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 61

Why Learn It?

Why Learn It?

Page 66: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 62

Tell Me / Show Me

Tell Me / Show Me

Page 67: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 63

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: These are just a few example queries. Students should run their queries against the database to check their accuracy. 1. SELECT employee_id, last_name, department_id FROM employees WHERE LOWER(last_name) = 'ernst'; SELECT employee_id, CONCAT(first_name, last_name) NAME, job_id, LENGTH (last_name), INSTR(last_name, 'i') "Contains 'i'?" FROM employees WHERE SUBSTR(job_id, 4) = 'PRES'; SELECT ROUND(45.923,2), ROUND(45.923,0), ROUND(45.923,-1) FROM DUAL;

Page 68: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 64

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 2. SELECT UPPER(CONCAT(cd_number, title)) FROM d_cds WHERE cd_number = 94; SELECT UPPER(cd_number)||UPPER(title) FROM d_cds WHERE cd_number = 94; 3. ___F__a. LOWER converts numbers to lowercase. ___F__b. Use RPAD to move numbers to the right to place an * on the left. ___F__c. TRIM can be used to trim one or more characters from a string. ___T__d. LENGTH returns a number. ___F__e. SUBSTR is used to substitute one string for another. ___T__f. CONCAT is limited to using two parameters. ___T__g. TRUNC will return zero decimal places if a decimal value is omitted.

Page 69: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 65

4. SELECT TO_CHAR(cost, '$99999.99') FROM d_events; 5. SELECT '*'||ID AS "New Id" FROM f_staffs WHERE LENGTH(ID) = 1;

Page 70: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 66

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 6. SELECT TO_DATE('December 15, 1995', 'Month dd, RRRR') FROM DUAL; 7. SELECT LOWER(TO_CHAR(TO_DATE('19-JUN-04','DD-MON-YY'), 'ddth "of" Month YYYYsp')) FROM DUAL ; 8. SELECT SUBSTR('Oracle Academy', -7,7) FROM DUAL; 9. There is nothing wrong with the syntax; there is no matching data in the database.

Page 71: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 67

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 10. _C__a. To convert varchar2 to number data _C__b. To format a date to other than the default format _C__c. To convert a date such as June 19, 2000 to default format _N__d. To format a number to appear as currency _G__e. To substitute a value in a table for null _CE_f. To do an IF-THEN-ELSE statement _G__g. To find the first not null expression among a list of expressions _C__h. To replace a section of a string with another string _C__i. To format a 20th-century date _C__j. To present output all in uppercase _C__k. To find the numeric position of a character in a string _D__l. To find the last day of the month

Page 72: Database Programming - Section 2 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005... · Database Programming - Section 2 ... Before beginning this lesson's discussion of data-type

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 2 Page 68