Uploaded by JEN SAN

Database Programming - Section 11 Instructor Guide

advertisement
Database Programming Section 11
Instructor Guide
Table of Contents
Database Programming - Section 11..............................................................................................................1
Lesson 1 - In-Class Interview ........................................................................................................................1
What Will I Learn? ........................................................................................................................................3
Why Learn It?................................................................................................................................................4
Tell Me / Show Me........................................................................................................................................5
Try It / Solve It ..............................................................................................................................................9
Lesson 2 - Creating Views.............................................................................................................................12
What Will I Learn? ........................................................................................................................................13
Why Learn It?................................................................................................................................................14
Tell Me / Show Me........................................................................................................................................15
Try It / Solve It ..............................................................................................................................................24
Lesson 3 - DML Operations and Views ........................................................................................................27
What Will I Learn? ........................................................................................................................................29
Why Learn It?................................................................................................................................................30
Tell Me / Show Me........................................................................................................................................31
Try It / Solve It ..............................................................................................................................................36
Lesson 4 - Managing Views ..........................................................................................................................44
What Will I Learn? ........................................................................................................................................45
Why Learn It?................................................................................................................................................46
Tell Me / Show Me........................................................................................................................................47
Try It / Solve It ..............................................................................................................................................50
Lesson 5 - Practice Exercises and Review.....................................................................................................52
What Will I Learn? ........................................................................................................................................53
Why Learn It?................................................................................................................................................54
Tell Me / Show Me........................................................................................................................................55
Try It / Solve It ..............................................................................................................................................56
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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."
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 4
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
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.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 5
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
Read and describe or role play each step in the interview process.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 6
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
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.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 7
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
- 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.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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?"
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 12
What Will I Learn?
What Will I Learn?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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!
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 14
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
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.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 15
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
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
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 16
Tell Me / Show Me
Tell Me / Show Me
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 17
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
Read aloud the syntax for a view. Use the graphic to explain the difference between a simple
view and a complex view.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 18
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
Read aloud the syntax for a view. Use the graphic to explain the difference between a simple
view and a complex view.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 19
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
To prevent frivolous examples, review with students your preference for naming views.
The NOFORCE option will not execute in HTML DB.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 20
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
To prevent frivolous examples, review with students your preference for naming views.
The NOFORCE option will not execute in HTML DB.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 21
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
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.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 22
Tell Me / Show Me
Tell Me / Show Me
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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;
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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;
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 29
Why Learn It?
Why Learn It?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 30
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
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.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 31
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
Explain that the WHERE clause restricted the view and now the WITH CHECK OPTION
restricts changes only to the department 50.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 32
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
Explain the WITH READ ONLY syntax. Are there folders or resources on the school network
that are READ ONLY?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 33
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
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.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 34
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
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.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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)
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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)
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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)
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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)
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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;
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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;
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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;
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 44
What Will I Learn?
What Will I Learn?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 45
Why Learn It?
Why Learn It?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 46
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
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;
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 47
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
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.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 48
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
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.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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;
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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;
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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.
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 52
What Will I Learn?
What Will I Learn?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 53
Why Learn It?
Why Learn It?
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 54
Tell Me / Show Me
Tell Me / Show Me
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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;
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 56
3. CREATE VIEW view_my_departments
AS SELECT department_id, department_name
FROM my_departments;
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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")
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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;
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
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;
Copyright © Oracle, 2004. All rights reserved.
Database Programming - Section 11
Page 60
Download