Basic Nested Queries and Views

Slides:



Advertisements
Similar presentations
Advanced SQL Topics Edward Wu.
Advertisements

© Abdou Illia MIS Spring 2014
Dr. Chen, Oracle Database System (Oracle) 1 Chapter 12 Subqueries and Merge Statements (up to p.451) Jason C. H. Chen, Ph.D. Professor of MIS School of.
Chapter 11 Group Functions (up to p.402)
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
Introduction to Structured Query Language (SQL)
A Guide to Oracle9i1 Using SQL Queries to Insert, Update, Delete, and View Data Chapter 3.
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
View Sen Zhang. Views are very common in business systems users view of data is simplified a form of security - user sees only the data he/she needs to.
Concepts of Database Management, 4th Edition, Pratt & Adamski
A Guide to MySQL 7. 2 Objectives Understand, define, and drop views Recognize the benefits of using views Use a view to update data Grant and revoke users’
View and Materialized view. What is a view? Logically represents subset of data from one or more table. In sql, a view is a virtual relation based on.
Introduction to Structured Query Language (SQL)
A Guide to SQL, Seventh Edition. Objectives Understand, create, and drop views Recognize the benefits of using views Grant and revoke user’s database.
Introduction to Oracle9i: SQL1 Views. Introduction to Oracle9i: SQL2 Chapter Objectives Create a view, using CREATE VIEW command or the CREATE OR REPLACE.
Microsoft Access 2010 Chapter 7 Using SQL.
Guide to Oracle10G1 Using SQL Queries to Insert, Update, Delete, and View Data Chapter 3.
Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.
Introduction to Databases Chapter 7: Data Access and Manipulation.
Dr. Chen, Oracle Database System (Oracle) 1 Database Development DDL DML DCL JL_D.B. ORACLE (SQL Components) (Retrieve Data and Produce Information from.
Dr. Chen, Oracle Database System (Oracle) 1 Chapter 4 Constraints Jason C. H. Chen, Ph.D. Professor of MIS School of Business Gonzaga University Spokane,
Concepts of Database Management, Fifth Edition Chapter 4: The Relational Model 3: Advanced Topics.
Chapter 5 Data Manipulation and Transaction Control
SQL/Lesson 4/Slide 1 of 45 Using Subqueries and Managing Databases Objectives In this lesson, you will learn to: *Use subqueries * Use subqueries with.
ITBIS373 Database Development
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
Dr. Chen, Oracle Database System (Oracle) 1 HW Discussion Chapter 5 (#9) –Why need two “deletes”? SQL> --9. SQL> SQL> DELETE FROM orderitems 2 WHERE order#
Dr. Chen, Oracle Database System (Oracle) 1 Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file) Jason C. H. Chen, Ph.D. Professor.
Using Special Operators (LIKE and IN)
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
ADVANCED SQL SELECT QUERIES CS 260 Database Systems.
Views In some cases, it is not desirable for all users to see the entire logical model (that is, all the actual relations stored in the database.) In some.
CpSc 3220 The Language of SQL The Language of SQL Chapters
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Inserting Data.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Chapter 12 Subqueries and Merge Statements
1 Creating and Maintaining Database Objects Part 1 Database Systems.
Chapter 13 Views Oracle 10g: SQL. Oracle 10g: SQL2 Objectives Create a view, using CREATE VIEW command or the CREATE OR REPLACE VIEW command Employ the.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
Dr. Chen, Oracle Database System (Oracle) 1 Chapter 7 User Creation and Management Jason C. H. Chen, Ph.D. Professor of MIS School of Business Gonzaga.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Dr. Chen, Oracle Database System (Oracle) 1 Basic Nested Queries and Views Jason C. H. Chen, Ph.D. Professor of MIS School of Business Gonzaga University.
CpSc 3220 The Language of SQL
MySQL Subquery Source: Dev.MySql.com
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
 2012 Pearson Education, Inc. All rights reserved.
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
Subqueries Schedule: Timing Topic 25 minutes Lecture
Database Systems: Design, Implementation, and Management Tenth Edition
Introduction to Oracle9i: SQL
David M. Kroenke and David J
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Chapter 2 Views.
Creating and Maintaining
A Guide to SQL, Eighth Edition
Chapter 2 Views.
Chapter 1 Introduction.
Contents Preface I Introduction Lesson Objectives I-2
Introduction to Access
Chapter 8 Advanced SQL.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Database Systems: Design, Implementation, and Management Tenth Edition
Structured Query Language – The Basics
IST 318 Database Administration
Subqueries Schedule: Timing Topic 25 minutes Lecture
Manipulating Data Lesson 3.
Presentation transcript:

Basic Nested Queries and Views Jason C. H. Chen, Ph.D. Professor of MIS School of Business Gonzaga University Spokane, WA 99258 USA chen@gonzaga.edu

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)

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

You need to run the following command to make the example work: Refresh Database You need to run the following command to make the example work: Download files from NW_CW folder @ c:\oradata\NW_CW\northwoods.sql Open the following script file: NW_CW&Nested_Query.sql

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 value that returns one value

Subquery Returns Single Value Query: List all students who have the same S_CLASS value as student Jorge Perez. SELECT s_last, s_first FROM student WHERE s_class = You need to run the following command to make the example work: @ c:\oradata\NW_CW\northwoods.sql ‘SR’; It is impossible for us to manually examine the value (e.g., ‘SR’) that we should put in. What is a right way? S_LAST S_FIRST ---------- --------- Jones Tammy Perez Jorge

Subquery Returns Single Value Query: List all students who have the same S_CLASS value as student Jorge Perez. SELECT s_last, s_first FROM student WHERE s_class = You need to run the following command to make the example work: @ c:\oradata\NW_CW\northwoods.sql SELECT s_class FROM student WHERE s_last = 'Perez' AND s_first = 'Jorge‘ ; ( ) S_LAST S_FIRST ---------- --------- Jones Tammy Perez Jorge

Nested Query Where Subquery Returns Multiple Values Syntax: SELECT column1, column2, … FROM table1, table2, … WHERE join conditions AND search_column1 IN (SELECT column1 FROM table1, table2, … WHERE search and join conditions) Subquery that returns multiple values

Nested Query Where Subquery Returns Multiple Values Display the names of all students who have enrolled in the same course sections as Jorge Perez. SELECT DISTINCT s_last, s_first FROM student, enrollment WHERE student.s_id = enrollment.s_id AND c_sec_id IN (SELECT c_sec_id FROM student, enrollment WHERE student.s_id = enrollment.s_id AND s_last = 'Perez' AND s_first = 'Jorge'); S_LAST S_FIRST --------------- ------------- Johnson Lisa Jones Tammy Marsh John Perez Jorge

Database Views Table-1 Table-2 Table-N database Database view Query A database view is a logical (virtual) table based on a query. Table-1 Table-2 Table-N 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. database Single view table Database view Query Output: Report, Graphs

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

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

Database View (cont.) SELECT view_name FROM user_views; SELECT view_name FROM ALL_VIEWS WHERE owner=‘SYSTEM’; DROP VIEW <view_name>; CREATE OR REPLACE VIEW <view_name> AS <view query specification>;

Database View What are gains? Anything New? CREATE VIEW <view_name> AS <view query specification>; SELECT view_name FROM user_views; e.g., CREATE OR REPLACE VIEW customers_vu AS SELECT customer#, lastname, firstname, region FROM customers; SELECT * FROM customers_vu; DESCRIBE customers_vu; DELETING VIEWS DROP VIEW viewname; DROP VIEW customers_vu; Anything New? --EXTRA for VIEW SELECT firstname || ' ' || lastname AS Name, order#, orderdate FROM customers_vu, orders WHERE customers_vu.customer# = orders.customer# ORDER BY Name; CREATE OR REPLACE VIEW customers_vu2 (customer_no, Last_name, First_name, Region) AS SELECT customer#, lastname, firstname, region FROM customers; SELECT * FROM customers_vu2; DESCRIBE customers_vu2;

Database View (cont.) CREATE OR REPLACE VIEW book_info_view AS SELECT title, contact, phone FROM books, publisher WHERE books.pubid = publisher.pubid; Anything new on the book_info_view ? Ans: The view is created using two tables SELECT * FROM book_info_view ORDER by title;

Creating a Simple View Only references one table – no group functions, GROUP BY clause, or expressions -- chapter 13, Figure 13-4(b); p.477 CREATE VIEW books_inventory_vu AS SELECT isbn, title, retail price FROM books WITH READ ONLY; -- chapter 13, Figure 13-4; p.477 CREATE VIEW inventory AS SELECT isbn, title, retail price FROM books WITH READ ONLY; SELECT * FROM inventory; SELECT * FROM books_inventory_vu; Figure 13-4 Selecting all records from the INVENTORY view What happen to “Database” after “DROP” view? DROP VIEW inventory; SELECT * FROM inventory; SELECT * FROM books; -- REcreate inventory view

CREATE OR REPLACE VIEW book_vu AS SELECT isbn, title, category FROM books WHERE category = 'COOKING'; SELECT * FROM book_vu; SQL> SELECT * 2 FROM book_vu; ISBN TITLE CATEGORY ---------- ------------------------------ --------- 3437212490 COOKING WITH MUSHROOMS COOKING 0299282519 THE WOK WAY TO COOK COOKING See next slide for detailed process

Figure 13-1 View Processing

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 AS SELECT customer#, Lastname, Firstname FROM customers; SELECT view_name FROM user_views; SELECT * FROM customers_view; What are gains? DELETING VIEWS DROP VIEW viewname; DROP VIEW customers_view; --more example for VIEW SELECT Lastname, Firstname, order# FROM customers_view cv, orders WHERE cv.customer# = orders.customer# AND cv.customer#=1001;

Advanced Database View (continued) Using SET Operators (e.g., UNION, INTERSECT etc.) Highly recommended for MVC mini-case CREATE VIEW <view_name> AS <view query specification> <SET operator> <view query specification>; e.g., CREATE OR REPLACE VIEW customers_view (f-1, f-2, …) AS SELECT <field-1> <field-2> … FROM <table-1> <table-2> … WHERE <condition> SELECT <field-3> <field-4> … FROM <table-3> <table-4> … WHERE <condition>; CREATE OR REPLACE VIEW merged_view (f-1, f-2, …) AS SELECT FROM View_1 SELECT FROM View_2; /*Note: View_1 and View_2 have been created previously. */ <SET Operator> <SET Operator>

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.