Upload
tallis
View
50
Download
0
Embed Size (px)
DESCRIPTION
Basic Nested Queries and Views. Jason C. H. Chen , Ph.D. Professor of MIS School of Business Gonzaga University Spokane, WA 99258 USA [email protected]. Objectives. Learn what is nested query and how to create basic nested SQL queries - PowerPoint PPT Presentation
Citation preview
Dr. Chen, Oracle Database System (Oracle) 1
Basic Nested Queries and Views
Jason C. H. Chen, Ph.D.Professor of MIS
School of BusinessGonzaga University
Spokane, WA 99258 [email protected]
Dr. Chen, Oracle Database System (Oracle) 2
Objectives• Learn what is nested query and how to
create basic nested SQL queries• Learn what is database views and
how/why to create views– Help you to make your job much easier on
working with Queries for MVC mini-case (Phase III)
Dr. Chen, Oracle Database System (Oracle) 3
Subqueries and Their Uses
• Subquery – a query nested inside another query
• Used when a query is based on an unknown value
• Requires SELECT and FROM clauses• Must be enclosed in parentheses• Place on right side of comparison operator• We will study more in chapter 12
Dr. Chen, Oracle Database System (Oracle) 4
Creating Nested Queries
• Used to select results based on the result of a query• Consists of a main query and one or more subqueries.
– Main/outer query: first query that appears in the SELECT command
– Subquery retrieves values that the main query’s search condition must match
that return one valuethat returns
one value
Dr. Chen, Oracle Database System (Oracle) 5
Refresh the Northwoods University Database
• Create a new folder of nw_cw (under c:\oradata\) and download all script files from the Bb
• Re-run a script fileSQL> @ c:\oradata\NW_CW\northwoods.sql
Dr. Chen, Oracle Database System (Oracle) 6
Subquery Returns Single Value Query: List all students who have the same S_CLASS value as
student Jorge Perez.
SELECT s_last, s_firstFROM studentWHERE s_class =
S_LAST S_FIRST---------- ---------Jones TammyPerez Jorge
‘SR’;It is impossible for us to manually examine the value (e.g., ‘SR’) that we should put in. What is a right way?
Dr. Chen, Oracle Database System (Oracle) 7
Subquery Returns Single Value Query: List all students who have the same S_CLASS value as
student Jorge Perez.
SELECT s_class FROM student WHERE s_last = 'Perez' AND s_first = 'Jorge‘ ;
SELECT s_last, s_firstFROM studentWHERE s_class =
(
)
You need to run the following command to make the example work:@ c:\oradata\NW_CW\northwoods.sql
S_LAST S_FIRST---------- ---------Jones TammyPerez Jorge
Dr. Chen, Oracle Database System (Oracle) 8
Nested Query WhereSubquery Returns Multiple Values
• Syntax:SELECT column1, column2, …FROM table1, table2, …WHERE join conditionsAND search_column1 IN
(SELECT column1FROM table1, table2, …WHERE search and join conditions)
Subquerythat returnsmultiple values
Dr. Chen, Oracle Database System (Oracle) 9
Nested Query WhereSubquery Returns Multiple Values
• Display the names of all students who have enrolled in the same course sections as Jorge Perez.
(SELECT c_sec_id FROM student, enrollment WHERE student.s_id = enrollment.s_id
AND s_last = 'Perez' AND s_first = 'Jorge');
SELECT DISTINCT s_last, s_firstFROM student, enrollmentWHERE student.s_id = enrollment.s_idAND c_sec_id IN
S_LAST S_FIRST--------------- -------------Johnson LisaJones TammyMarsh JohnPerez Jorge
Dr. Chen, Oracle Database System (Oracle) 10
Database Views
Table-1 Table-NTable-2
database
Single view table
Query
Output:Report, Graphs
Database view
A database view is a logical (virtual) table based on a query.
It does not store data, but presents it in a format different from the one in which it is stored in the underlying tables.
With the database view, you can view database records, but you can’t insert new records or modify or delete exiting records in the view.
Dr. Chen, Oracle Database System (Oracle) 11
Views• Permanent objects (logical or virtual table)
that store queries but ____ data• Based on a source query that:
– can specify a subset of a single table’s fields or records– can join multiple tables
• Two purposes– Reduce complex query requirements – Restrict users’ access to sensitive data (enforce
security; user has access to view but not underlying table)
no
Dr. Chen, Oracle Database System (Oracle) 12
Creating and Using Database Views
• Views can be updateable if:– SELECT clause contains only fieldnames, no functions
or calculations– Can ____ contain the ORDER BY, DISTINCT, or
GROUP BY clauses, group functions, or set operators– search condition cannot contain a nested query
• Views are used like tables for selecting, inserting, updating and deleting data (only updatable views can be modified)
not
Dr. Chen, Oracle Database System (Oracle) 13
Database View (cont.)
h SELECT view_name FROM user_views;h SELECT view_name FROM ALL_VIEWS WHERE owner=‘SYSTEM’;h DROP VIEW <view_name>;h CREATE OR REPLACE VIEW <view_name> AS <view query specification>;
Dr. Chen, Oracle Database System (Oracle) 14
Database View (cont.)
What are gains?
--EXTRA for VIEWSELECT firstname || ' ' || lastname AS Name, order#, orderdateFROM customers_vu, ordersWHERE customers_vu.customer# = orders.order#ORDER BY Name;
i DELETING VIEWSDROP VIEW viewname;
DROP VIEW customers_vu;
CREATE VIEW <view_name> AS <view query specification>; e.g., SELECT view_name FROM user_views;
CREATE OR REPLACE VIEW customers_vu ASSELECT customer#, lastname, firstname, regionFROM customers;
Dr. Chen, Oracle Database System (Oracle) 15
CREATE OR REPLACE VIEW book_info_view ASSELECT title, contact, phoneFROM books, publisherWHERE books.pubid = publisher.pubid;
SELECT *FROM book_info_viewORDER by title;
Database View (cont.)
Anything new on the book_info_view ?
Ans: The view is created using two tables
DROP VIEW book_info_view;
Dr. Chen, Oracle Database System (Oracle) 16
Creating a Simple View
• Only references one table – no group functions, GROUP BY clause, or expressions
Figure 13-4 Selecting all records from the INVENTORY view
-- chapter 13, Figure 13-4(b); p.477-- a more meaningful view nameCREATE VIEW books_inventory_vuAS SELECT isbn, title, retail price FROM booksWITH READ ONLY;SELECT *FROM books_inventory_vu;
-- chapter 13, Figure 13-4; p.477CREATE VIEW inventoryAS SELECT isbn, title, retail price FROM booksWITH READ ONLY;
SELECT *FROM inventory;
DROP VIEW inventory;
SELECT *FROM inventory;
What happen to “Database” after “DROP” view?
SELECT *FROM books;-- You may REcreate inventory view
Dr. Chen, Oracle Database System (Oracle) 17
CREATE OR REPLACE VIEW book_vu ASSELECT isbn, title, categoryFROM booksWHERE category = 'COOKING';
SELECT * FROM book_vu;
SQL> SELECT * 2 FROM book_vu;
ISBN TITLE CATEGORY---------- ------------------------------ ---------3437212490 COOKING WITH MUSHROOMS COOKING0299282519 THE WOK WAY TO COOK COOKING
See next slide for detailed process
Dr. Chen, Oracle Database System (Oracle) 18
Figure 13-1 View Processing
Dr. Chen, Oracle Database System (Oracle) 19
Database View (continued)
• CREATE VIEW <view_name> AS <view query specification>; e.g., SELECT view_name FROM user_views;
CREATE OR REPLACE VIEW customers_view ASSELECT customer#, Lastname, FirstnameFROM customers;
SELECT view_name FROM user_views;SELECT * FROM customers_view;
What are gains?
--more example for VIEWSELECT Lastname, Firstname, cv.customer#, order#FROM customers_view cv, ordersWHERE cv.customer# = orders.customer#AND cv.customer#=1001;
i DELETING VIEWSDROP VIEW viewname;
DROP VIEW customers_view;
Dr. Chen, Oracle Database System (Oracle) 20
More to go …
• More Nested Queries and Subqueries will be studied in chapter 12.
• More views will be studied in chapter 13.• You will apply the “view” [an efficient way
to produce the solution] to the mini case (MVC mini-case) that will be assigned later.
Dr. Chen, Oracle Database System (Oracle) 21
• END OF VIEWS