Database Programming - Section 11. Instructor Guide

Similar documents
Creating Other Schema Objects

CIS 207 Oracle - Database Programming and SQL HOMEWORK: # 13

Creating Other Schema Objects. Copyright 2004, Oracle. All rights reserved.

Database Programming - Section 7. Instructor Guide

Database Programming - Section 10. Instructor Guide

Database Programming - Section 3. Instructor Guide

GIFT Department of Computing Science. CS-217/224: Database Systems. Lab-5 Manual. Displaying Data from Multiple Tables - SQL Joins

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

Database Foundations. 6-9 Joining Tables Using JOIN. Copyright 2014, Oracle and/or its affiliates. All rights reserved.

DUE: CD_NUMBER TITLE PRODUCER YEAR 97 Celebrate the Day R & B Inc Holiday Tunes for All Tunes are US 2004

Assignment Grading Rubric

Retrieving Data from Multiple Tables

CS 275 Winter 2011 Problem Set 3

ÇALIŞMA TEST SORULARI

Intermediate SQL: Aggregated Data, Joins and Set Operators

Updating Column Values and Deleting Rows. Copyright 2008, Oracle. All rights reserved.

Database Foundations. 6-4 Data Manipulation Language (DML) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

EXISTS NOT EXISTS WITH

Join, Sub queries and set operators

KORA. RDBMS Concepts II

Database Programming with SQL

Database Programming with SQL

Limit Rows Selected. Copyright 2008, Oracle. All rights reserved.

Introduction to Oracle9i: SQL

Retrieving Data Using the SQL SELECT Statement. Copyright 2009, Oracle. All rights reserved.

Database Programming - Section 8. Instructor Guide

Database Programming - Section 1. Instructor Guide

Additional Practice Solutions

Alkérdések II. Copyright 2004, Oracle. All rights reserved.

DUE: 9. Create a query that will return the average order total for all Global Fast Foods orders from January 1, 2002, to December 21, 2002.

Oracle Database SQL Basics

Introduction to Oracle9i: SQL

Course Outline and Objectives: Database Programming with SQL

ORACLE VIEWS ORACLE VIEWS. Techgoeasy.com

Database Design & Programming with SQL: Part 1 Learning Objectives

HR Database. Sample Output from TechWriter 2007 for Databases

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

SYSTEM CODE COURSE NAME DESCRIPTION SEM

School of Computing and Information Technology. Examination Paper Autumn Session 2017

Exam: 1Z Title : Introduction to Oracle9i: SQL. Ver :

Introduction to Computer Science and Business

Working with Columns, Characters and Rows. Copyright 2008, Oracle. All rights reserved.

1Z0-007 ineroduction to oracle9l:sql

Data Manipulation Language

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

Misc. Triggers Views Roles Sequences - Synonyms. Eng. Mohammed Alokshiya. Islamic University of Gaza. Faculty of Engineering

Objectives. After completing this lesson, you should be able to do the following:

Manipulating Data. Copyright 2004, Oracle. All rights reserved.

RETRIEVING DATA USING THE SQL SELECT STATEMENT

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

13: MOODLE WIKIS. Oklahoma Department of CareerTech WELCOME TO THE MOODLE WIKIS TUTORIAL! In this tutorial, you will learn:

Instructor: Craig Duckett. Lecture 03: Tuesday, April 3, 2018 SQL Sorting, Aggregates and Joining Tables

Fravo.com. Certification Made Easy. World No1 Cert Guides. Introduction to Oracle9i: SQL Exam 1Z Edition 1.0

Using DDL Statements to Create and Manage Tables. Copyright 2004, Oracle. All rights reserved.

Exam code: Exam name: Database Fundamentals. Version 16.0

Three types of sub queries are supported in SQL are Scalar, Row and Table sub queries.

Bsc (Hons) Software Engineering. Examinations for / Semester 1. Resit Examinations for BSE/15A/FT & BSE/16A/FT

Institute of Aga. Microsoft SQL Server LECTURER NIYAZ M. SALIH

DEFAULT Values, MERGE, and Multi-Table Inserts. Copyright 2009, Oracle. All rights reserved.

Database Programming - Section 18. Instructor Guide

Exam : 1Z Title : Introduction to Oracle9i: SQL

Institute of Aga. Network Database LECTURER NIYAZ M. SALIH

Relational Database Development

Database Design - Section 18. Instructor Guide

Harlan County Public Schools. NTI Days Web Design, Advanced Multimedia. Grades 9-12, HCHS

Oracle Database 10g: Introduction to SQL

Data Management Lecture Outline 2 Part 2. Instructor: Trevor Nadeau

Introduction to Explicit Cursors. Copyright 2008, Oracle. All rights reserved.

Oracle MOOC: SQL Fundamentals

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Using DDL Statements to Create and Manage Tables. Copyright 2004, Oracle. All rights reserved.

Lesson Guides PRE-INTERMEDIATE

Lesson Plan. Course Title: Principles of Information Technology Session Title: Intro to Databases Lesson Duration: Varies dependent upon instructor

Database Programming with SQL

ITCertMaster. Safe, simple and fast. 100% Pass guarantee! IT Certification Guaranteed, The Easy Way!

STUDENT USER GUIDE FOR

Consistency The DBMS must ensure the database will always be in a consistent state. Whenever data is modified, the database will change from one

Course Overview. Copyright 2010, Oracle and/or its affiliates. All rights reserved.

Top 6 SQL Query Interview Questions and Answers

normalization are being violated o Apply the rule of Third Normal Form to resolve a violation in the model

Querying Microsoft SQL Server 2014

Advance Database Systems. Joining Concepts in Advanced SQL Lecture# 4

Out for Shopping-Understanding Linear Data Structures English

DATA Data and information are used in our daily life. Each type of data has its own importance that contribute toward useful information.

Using the Set Operators. Copyright 2006, Oracle. All rights reserved.

2018 Delegate Assembly

Database Management Systems,

Database Foundations. 6-3 Data Definition Language (DDL) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Lab # 6. Data Manipulation Language (DML)

Appendix A Practices and Solutions

Série n 6 Bis : Ateliers SQL Data Modeler (Oracle)

Sql Server Check If Global Temporary Table Exists

Content-Based Assessments. Project 12H Grand Opening

Reference: W3School -

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Rochester Institute of Technology Golisano College of Computing and Information Sciences Department of Information Sciences and Technologies

Lesson Guides ELEMENTARY

EE221 Databases Practicals Manual

LECTURE10: DATA MANIPULATION IN SQL, ADVANCED SQL QUERIES

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

Transcription:

Database Programming - Section 11 Instructor Guide

Table of Contents...1 Lesson 1 - In-Class Interview...1 What Will I Learn?...3 Why Learn It?...4...5 Try It / Solve It...9 Lesson 2 - Creating Views...12 What Will I Learn?...13 Why Learn It?...14...15 Try It / Solve It...24 Lesson 3 - DML Operations and Views...27 What Will I Learn?...29 Why Learn It?...30...31 Try It / Solve It...36 Lesson 4 - Managing Views...44 What Will I Learn?...45 Why Learn It?...46...47 Try It / Solve It...50 Lesson 5 - Practice Exercises and Review...52 What Will I Learn?...53 Why Learn It?...54...55 Try It / Solve It...56 Page i

Lesson 1 - In-Class Interview Lesson 1 - In-Class Interview Lesson Preparation There are numerous Internet resources that highlight types of interviews and interviewing skills. Use the search keywords "interviewing skills." Excellent resources that explain the traditional versus the behavioral interview are: http://jobsearch.about.com/cs/interviews/a/behavioral.htm http://careers.unc.edu/bhavint.html http://www.confidencecenter.com/art11.htm http://ccdme.org Ask someone at the school or in the community who is responsible for hiring employees to speak to the class about the interview process. Review the Interview Rubric. What to Watch For Most students do not like to be singled out in class. Evaluate the interview process, not individual students. Page 1

Connections After having had the opportunity to participate in the in-class interview, relate the experience to meeting with a client in the data-modeling process. Are the meetings with the client more than just what business processes they want modeled? Do clients form a first impression? Do clients evaluate your work based on your speaking and writing skills? Extension: Ask students to look for newspaper articles, magazine articles, or Internet news about jobs and the job market. Assist students to heighten their awareness of the types of skills they need to succeed in a job. Students often compartmentalize what they learn in school and don't associate what they learn in one class with anything beyond that class. Relate what students learn in other classes to skills relevant to the world of work. Have students find a job posting that they think would interest them. Write a "preparedness plan" for a first interview. Look for plans that include elements such as, "research company via the web; write three questions to ask the interviewer at the end of the interview; list my current qualifications that would make me an excellent candidate; what I'll say when asked why I feel I am the best candidate." Page 2

What Will I Learn? What Will I Learn? What Will I Learn? Read the "What Will I Learn" objectives to students. Explain that this lesson discusses different types of interviews and techniques to develop successful interview skills. Students will have an opportunity to practice interviewing skills. Page 3

Why Learn It? Why Learn It? Why Learn It? Relate your own experiences with job interviews. How did you prepare for the interview? What was the interview like? Page 4

Discuss the differences between a traditional interview and a behavioral interview. In Try It/ Solve It, students should be able to compare and contrast the differences between the two methods of interviewing based on their research about these types of interviews. Use the search-engine keywords: "traditional interview" and "behavioral interview." Or look into the following Internet resources: http://jobsearch.about.com/cs/interviews/a/behavioral.htm and the University of North Carolina site http://careers.unc.edu/bhavint.html Traditional Interview: Focuses on what the candidate has accomplished such as completed college or worked as a graphic artist. Behavioral Interview: Focuses on past experiences and demonstrated competencies that relate to the job requirements, such as having been a team leader that completed an important project under extenuating circumstances. Page 5

Read and describe or role play each step in the interview process. Page 6

Depending on local customs, modify the list to meet your needs. Review the list of nonverbal communication skills that say "hire me" with the class. Page 7

- Establish an order for interviewing -- draw numbers or volunteer. - Tell students how the interview will be conducted, what they should wear, and what they should bring to the interview. - Review the Interview Rubric and how each student will be assessed. Let students know that evaluation is a means to make improvements, not a means to degrade them. Page 8

Try It / Solve It Try It / Solve It Try It / Solve It Using the Internet resources provided by the teacher, answer the following questions about interviews. 1. Traditional Interview: Focus on what the candidate has accomplished or wants to accomplish. Behavioral Interview: Focus on past experiences and demonstrated competencies that relate to the job requirements. 2. Rather then merely telling the interviewer what they would do in a situation, as in a regular interview, in a behavioral interview the candidate must describe, in detail, how they handled a situation in the past. 3. The purpose of any interview is to determine if the candidate can do the job and will fit into the business culture of the company. In both types of interview, there is always a process of introductions, company information, candidate response to questions, and closing comments. Page 9

Try It / Solve It Try It / Solve It Try It / Solve It Multipage: Choose a job description from the local newspaper or Internet or use the "IT Database Administrator Wanted" article. - Students will not be familiar with what the job may require. Discuss the details listed in the job description to understand what skills the company is looking for in a candidate. This may require research. The qualifications can be hard to figure out! - Share with students the concept that job descriptions are company "wish lists." A company cannot always hire someone with 3, 5, 10+ years experience, but it will accept a candidate with outstanding communication skills and a demonstrated technical background. - Review Section 9, Lesson 3, the kinds of questions that cannot be asked of a candidate. - Help students formulate traditional and behavioral questions that are appropriate for the job description chosen. Traditional Questions: Candidate is allowed to generalize and theorize. "What are your three major accomplishments?" "What have you disliked in your past jobs?" Page 10

"What kinds of people frustrate you?" "What is your ideal job?" "Tell me about your last job? Multipage: Behavioral Questions: Candidate must describe past experiences related to the question. "This job has deadlines that must be met. Describe a situation where you had an unexpected deadline and how you handled it." "Give an example of a situation where you were not supported by others when a job had to be done and describe how you accomplished the task and dealt with your fellow workers." - Use the questions generated by the students as the in-class interview questions. Choose 4-6 questions for each candidate. Change the questions so students don't prepare the same responses. - Explain to students they may not know the answers to the interview questions or, more likely, have not had work experience to answer them. They should, however, be able to relate a question to some other situation. For instance, Give an example of a deadline you had to meet as part of a team where not all members of the team helped or contributed." Most students have had these kinds of experiences through school or a job and can relate how they solved the problem. - Structure the interview to last approximately 5 minutes per student. Not all of the points on the Interview Rubric can be covered with each student. Choose one or two different points for each student. The interviews will need to be conducted over several days to accommodate an entire class of students. Other students can be working on the next lesson while interviews are being conducted. Discuss the Interview Rubric with students as a class. Use the rubric to provide each student with feedback about his or her interview. If time permits, discuss the rubric with individual students. If not, discuss the evaluation of the overall interview process with the class as a whole. Page 11

Lesson 2 - Creating Views Lesson 2 - Creating Views Lesson Preparation None. What to Watch For The idea of a view as a virtual representation of underlying table data may be difficult for students to understand. Use the window analogy. A view is a "window" into the database. The view presents data from one or more tables in one place. With simple views, DML statements can be used to make changes to the underlying base tables through the view. However, with complex views, DML statements are not always possible. Connections For review of previous material, ask students to create a table and a view based on that table. For example: - Table of holidays - name of the holiday, the date of the holiday, and the country that celebrates the holiday - Table of driving distances from their city to other cities - name of city, miles/kilometers to city, average driving time - Table of local eating places - name of business, address, phone number, type of food Page 12

What Will I Learn? What Will I Learn? Page 13

Why Learn It? Why Learn It? Why Learn It? Explain your school's access to information. Teachers, counselors, and secretaries use information stored in a database as part of their job. A guidance counselor has access to student grades; a teacher accesses the database to record daily class attendance; secretaries use information stored in the database for correspondence and office functions. But everyone does not have the same privileges to update, insert, or remove information stored in the database. A manager of the assembly line of a manufacturing plant has access to information needed to do his/her job. The manager doesn't have access to financial records or personal information for his/her employees. In business today, the objective is for people to see what they need to see to do their jobs, and not much more! Page 14

Use the graphic to give students a mental picture of a view being a window through which they can see a custom view of the underlying data tables. Use the graphic to compare what columns the view is able to see compared to the underlying table. Point out in the syntax example that the view was created using a subquery. Page 15

Begin this lesson with a review question. "How do you add a constraint to a table if the table has already been created?" ALTER TABLE table_name ADD CONSTRAINT constraint_name Page 16

Page 17

Read aloud the syntax for a view. Use the graphic to explain the difference between a simple view and a complex view. Page 18

Read aloud the syntax for a view. Use the graphic to explain the difference between a simple view and a complex view. Page 19

To prevent frivolous examples, review with students your preference for naming views. The NOFORCE option will not execute in HTML DB. Page 20

To prevent frivolous examples, review with students your preference for naming views. The NOFORCE option will not execute in HTML DB. Page 21

Review the GROUP BY rules and functions. All columns in the SELECT statement that are not group functions must be listed in the GROUP BY clause. Complex views use join statements. Students may need to DESCRIBE tables to identify the primary and foreign key relationships between the database tables in order to identify the primary key foreign key relationships for joins. Page 22

Page 23

Try It / Solve It Try It / Solve It Try It / Solve It Demonstrate naming conventions for views to avoid frivolous examples. In this section, views have been named view_copy_tablename. The copied tables will be used to practice DML statements without changing the original table structures. 1. CREATE VIEW view_d_songs AS SELECT id, title AS "Song Title", artist FROM d_songs WHERE type_code = 77; 2. SELECT * FROM view_d_songs; Page 24

3. CREATE OR REPLACE VIEW view_d_songs AS SELECT id AS "Identification", title AS "Song Title", artist AS "Recording Artist", type_code AS "Type Code" FROM d_songs WHERE type_code = 77; or use alias after the CREATE statement as shown. CREATE OR REPLACE VIEW view_d_songs(identification, Song, Singer, Type Code ) AS SELECT id, title, artist, type_code FROM d_songs Page 25

Try It / Solve It Try It / Solve It Try It / Solve It Answers: 4. Modify answer to 4: CREATE OR REPLACE VIEW view_events_dj ("Name of Event", "Event Date", "Theme Code", "Description") AS SELECT e.name, e.event_date, e.theme_code, t.description FROM d_events e, d_themes t WHERE e.theme_code = t.code; 5. CREATE VIEW dept_sum_vu (name, minsal, maxsal, avgsal) AS SELECT d.department_name, MIN(e.salary), MAX(e.salary),AVG(e.salary) FROM employees e, departments d WHERE e.department_id = d.department_id GROUP BY d.department_name; Page 26

Lesson 3 - DML Operations and Views Lesson 3 - DML Operations and Views Lesson Preparation SQL queries using DML statements will execute. Only rows that meet the WITH CHECK OPTION will be deleted. Using the CREATE or REPLACE syntax is the most efficient way for students to re-create a view if they have made a mistake. What to Watch For None. Page 27

Connections Relate creating views to the other kinds of restraints set for databases, such as primary and foreign keys, unique constraints, and not null constraints. Where does the necessity for constraints originate? How are constraints related to relational database integrity? Successful database development and management are created out of the blend of business needs, data, and daily business processes. Ensuring data integrity and database security is the job of the database administrator. Constraints serve to balance the needs of business while maintaining the integrity of the database. Is it a business rule that all employees must be assigned an identification number before being entered into the database? Can anyone view the company salary table or the profit and loss information? Can data be entered into the database that is incomplete for some columns? Is it possible to delete data from a table that has related data in another table? Page 28

What Will I Learn? What Will I Learn? What Will I Learn? Verify access to Internet resources for the "What's Next in Computing" activity in Try It/ Solve It. Page 29

Why Learn It? Why Learn It? Page 30

Begin this lesson with a review question. How do you make changes to a view that already exists? CREATE or REPLACE VIEW viewname Explain the CREATE VIEW syntax and the WHERE clause statement that restricted what department data can be seen in the view. Page 31

Explain that the WHERE clause restricted the view and now the WITH CHECK OPTION restricts changes only to the department 50. Page 32

Explain the WITH READ ONLY syntax. Are there folders or resources on the school network that are READ ONLY? Page 33

Demonstrate the ROWNUM using the query shown in the graphic. Explain that ROWNUM is just a number value given to each row in the result set. Page 34

Discuss with students why it would be impossible to insert data through a view if the NOT NULL columns were missing from the view. NOT NULL means that the base columns must have a value. The user of a view will not see all the columns in the table and wouldn't know which columns in the base tables must have a value. Adding a new row must include data for all the NOT NULL columns for that row. Page 35

Try It / Solve It Try It / Solve It Try It / Solve It Assign or let students choose a topic. Assist them in finding Internet resources to explore the topic. Groups could report their findings to the class. Internet resources for this activity: (search-engine keywords: Moore's Law) - Wireless technologies -- when can we pull the plug? (search-engine keywords: future of wireless technologies, wireless technologies) - How big is big? What technologies are being developed to store large quantities of information? (search-engine keywords: big databases) - How much is too much? What are the trends in storing personal data and what are the issues being addressed related to personal privacy? (search-engine keywords: personal privacy and technology) - What new technologies are being developed to ensure personal identification? (search-engine keywords: biometric identification) - What is data mining? How can businesses target product advertising gleaned from data stored about your buying habits or Internet browsing preferences? (search-engine keywords: data mining) Page 36

- How can we make computers know how we see and feel? (search-engine keywords: IBM Project Blue Eyes "How Computers Know How We Feel") What technologies are being developed to protect copyrighted material? (search-engine keywords: digital copyright protection) How small is small? What are the limits to miniaturizing computer technologies? Can a phone, computer, and camera be integrated into a wrist watch? (search-engine keywords: miniaturizing computer technologies) Page 37

Try It / Solve It Try It / Solve It Try It / Solve It Assign or let students choose a topic. Assist them in finding Internet resources to explore the topic. Groups could report their findings to the class. Internet resources for this activity: (search-engine keywords: Moore's Law) - Wireless technologies -- when can we pull the plug? (search-engine keywords: future of wireless technologies, wireless technologies) - How big is big? What technologies are being developed to store large quantities of information? (search-engine keywords: big databases) - How much is too much? What are the trends in storing personal data and what are the issues being addressed related to personal privacy? (search-engine keywords: personal privacy and technology) - What new technologies are being developed to ensure personal identification? (search-engine keywords: biometric identification) - What is data mining? How can businesses target product advertising gleaned from data stored about your buying habits or Internet browsing preferences? (search-engine keywords: data mining) Page 38

- How can we make computers know how we see and feel? (search-engine keywords: IBM Project Blue Eyes "How Computers Know How We Feel") What technologies are being developed to protect copyrighted material? (search-engine keywords: digital copyright protection) How small is small? What are the limits to miniaturizing computer technologies? Can a phone, computer, and camera be integrated into a wrist watch? (search-engine keywords: miniaturizing computer technologies) Page 39

Try It / Solve It Try It / Solve It Try It / Solve It Demonstrate a SELECT statement to view USER_UPDATABLE_COLUMNS in the data dictionary or use the "Browse Data Dictionary" feature in HTML DB. Note that the tablenames are stored as uppercase letters. SELECT * FROM USER_UPDATABLE_COLUMNS WHERE table_name = 'COPY_D_SONGS'; Use the DESCRIBE statement to verify that you have tables named copy_d_songs, copy_d_events, copy_d_cds, and copy_d_clients in your schema. If you don't, use a subquery to create a copy of each. CREATE TABLE copy_tablename AS SELECT * FROM basetable_name; Page 40

Answers: 1. SELECT * FROM USER_UPDATABLE_COLUMNS WHERE table_name = 'COPY_D_SONGS'; Use the same syntax but change table_name for the other tables. 2. CREATE VIEW view_copy_d_songs AS SELECT * FROM copy_d_songs; 3. INSERT INTO view_copy_d_songs(id, title, duration, artist, type_code) VALUES(88, 'Mellow Jello', '2', 'The What', 4); SELECT * FROM view_copy_d_songs; SELECT * FROM copy_d_songs; Page 41

Try It / Solve It Try It / Solve It Try It / Solve It 4. CREATE VIEW read_copy_d_cds AS SELECT * FROM copy_d_cds WHERE year = 2000 WITH READ ONLY; 5. DELETE FROM read_copy_d_cds WHERE cd_number = 90; **fail to execute as view is read only ORA-01752: cannot delete from view without exactly one key-preserved table 6. CREATE OR REPLACE VIEW read_copy_d_cds AS SELECT * FROM copy_d_cds WHERE year = 2000 WITH CHECK OPTION CONSTRAINT ck_read_copy_d_cds; Page 42

Try It / Solve It Try It / Solve It Try It / Solve It 7. DELETE FROM read_copy_d_cds WHERE year = 2000; **executes as it meets the CHECK OPTION criteria 8. DELETE FROM read_copy_d_cds WHERE cd_number = 90; **query executes with 0 rows deleted 9. DELETE FROM read_copy_d_cds WHERE year = 2001; **query executes with 0 rows deleted 10. The base tables should have all original rows with no year 2000 data. Page 43

Lesson 4 - Managing Views Lesson 4 - Managing Views Lesson Preparation None. What to Watch For Inline views are complicated. Break down the subquery and its result set. Show how the outer query uses the data in the result set. Connections Inline subqueries can be used to reinforce students' understanding of subqueries in general. Relate top-n-analysis to a simple ORDER BY clause. The outcome of both expressions is to order data. The top-n-analysis specifically numbers each row returned and can limit the number of rankings displayed. Page 44

What Will I Learn? What Will I Learn? Page 45

Why Learn It? Why Learn It? Page 46

Ask students to query the data dictionary for a view they created in the previous lesson. Ask them to issue a DROP VIEW statement. In HTML DB, query the data dictionary to confirm that the view was deleted. If students don t have a view, use the following: CREATE VIEW view_employees AS SELECT first_name, last_name, email FROM employees WHERE employee_id BETWEEN 100 and 124; DROP VIEW view_employees; Page 47

Explain the inline query in two steps: - The subquery in the FROM clause and the result set it returns. Students could execute this query (omit the aliases) to verify the result that it returns. - The outer query and its use of the subquery information in the join statement. Page 48

Use the graphic to explain top-n-analysis by first showing what the subquery is designed to return. The ROWNUM uses the subquery ordered result set to assign a sequential value starting with 1 to each of the rows returned by the subquery. The outer WHERE clause is used to restrict the number of rows returned and must use a < or <= operator. Page 49

Try It / Solve It Try It / Solve It Try It / Solve It 1. CREATE OR REPLACE VIEW view_copy_d_songs AS SELECT title, artist FROM copy_d_songs; SELECT * FROM view_copy_d_songs; 2. DROP VIEW view_copy_d_songs; SELECT * FROM view_copy_d_songs; 3. SELECT ROWNUM as RANK, last_name, salary FROM (SELECT last_name,salary FROM employees ORDER BY salary DESC) WHERE ROWNUM <= 3; Page 50

Try It / Solve It Try It / Solve It Try It / Solve It 4. SELECT a.last_name, a.salary, a.department_id, b.maxsal FROM employees a, (SELECT department_id, max(salary) maxsal FROM employees GROUP BY department_id) b WHERE a.department_id = b.department_id AND a.salary < b.maxsal; 5. SELECT ROWNUM as RANK, last_name, salary FROM (SELECT last_name,salary FROM f_staffs ORDER BY salary ) WHERE ROWNUM <= 3; Page 51

Lesson 5 - Practice Exercises and Review Lesson 5 - Practice Exercises and Review Lesson Preparation None. What to Watch For Students can "get lost" and frustrated in sequential processes. Check to make sure students complete practice 1 correctly before proceeding to the next question. Discuss briefly the errors generated by the queries. Understanding the errors is as important as understanding when code works. Connections None. Page 52

What Will I Learn? What Will I Learn? Page 53

Why Learn It? Why Learn It? Page 54

Page 55

Try It / Solve It Try It / Solve It Try It / Solve It Students should complete each practice exercise in the order presented. Each subsequent exercise is an extension of the prior exercise. If students make a mistake, a view can be recreated easily with a CREATE or REPLACE VIEW statement. If students have difficulty, do one question at a time and review the process for setting up the query and the result set. Review with students the Study Guide and Vocabulary. 1. CREATE TABLE my_departments AS SELECT * FROM departments; SELECT * FROM my_departments; 2. DESCRIBE my_departments; **department_name is NOT NULL, constraint name is SYS_C001868830 (this name could vary) ALTER TABLE my_departments DISABLE CONSTRAINT SYS_C001868830; Page 56

3. CREATE VIEW view_my_departments AS SELECT department_id, department_name FROM my_departments; Page 57

Try It / Solve It Try It / Solve It Try It / Solve It 4. INSERT INTO view_my_departments(department_id, department_name) VALUES(105, 'Advertising'); INSERT INTO view_my_departments(department_id, department_name) VALUES(120, Custodial'); INSERT INTO view_my_departments(department_id, department_name) VALUES(130, 'Planning'); 5. ALTER TABLE my_departments ADD CONSTRAINT my_department_id_pk PRIMARY KEY (department_id); 6. INSERT INTO view_my_departments (department_name) VALUES('Human Resources'); ORA-01400: cannot insert NULL into ("USWA_SKHS_SQL01_T01"."DEPARTMENTS"."DEPARTMENT_ID") Page 58

Try It / Solve It Try It / Solve It Try It / Solve It 7. INSERT INTO view_my_departments (department_name, department_id) VALUES('Human Resources', 220); 8. SELECT * FROM view_my_departments; 9. CREATE OR REPLACE VIEW view_my_departments(department_name, department_id, location_id, manager_id) AS SELECT department_name, department_id, location_id, manager_id FROM my_departments; Page 59

Try It / Solve It Try It / Solve It Try It / Solve It 10. ALTER TABLE my_departments ADD CONSTRAINT loc_id_my_dept_nn CHECK (location_id IS NOT NULL); ORA-02293: cannot validate (USCA_ORACLE_SQL01_S01.LOC_ID_MY_DEPT_NN) - check constraint violated ** adding the three new columns to the my_departments table inserted null values in the location_id column. A NOT NULL constraint can only be added to a column if the table is empty or if the column has a value for every row. 11. CREATE VIEW view_dept_locations AS SELECT d.department_name, l.street_address, l.city, l.state_province FROM departments d, locations l WHERE d.location_id = l.location_id AND l.country_id LIKE 'US'; SELECT * From view_dept_locations; Page 60