Chapter 13 Subqueries and Views

Slides:



Advertisements
Similar presentations
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Advertisements

Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
With Microsoft Access 2010© 2011 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Access.
Introduction to Structured Query Language (SQL)
Fundamentals, Design, and Implementation, 9/e Chapter 7 Using SQL in Applications.
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.
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)
Concepts of Database Management Sixth Edition
Introduction to Oracle9i: SQL1 Views. Introduction to Oracle9i: SQL2 Chapter Objectives Create a view, using CREATE VIEW command or the CREATE OR REPLACE.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
Microsoft Access 2010 Chapter 7 Using SQL.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid.
Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Concepts of Database Management Seventh Edition
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter 9 Query-by-Example Pearson Education © 2009.
Database Development Tr ươ ng Quý Quỳnh. References UDEMY: SQL Database MasterClass: Go From Pupil To Master! Database Systems - A Practical Approach.
Database Management System (DBMS) an Introduction DeSiaMore 1.
Chapter 13 Subqueries and Views Part C. SQL Copyright 2005 Radian Publishing Co.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
Chapter 11 Functions and Groups Part C. SQL Copyright 2005 Radian Publishing Co.
Intro to SQL Management Studio. Please Be Sure!! Make sure that your access is read only. If it isn’t, you have the potential to change data within your.
Chapter 2 Views. Objectives ◦ Create simple and complex views ◦ Creating a view with a check constraint ◦ Retrieve data from views ◦ Data manipulation.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Indexes and Views Unit 7.
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.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Materialized views (snapshot tables)
CSC314 DAY 9 Intermediate SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall USING AND DEFINING VIEWS  Views provide users controlled.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
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.
IFS180 Intro. to Data Management Chapter 10 - Unions.
SQL Introduction SQL stands for “Structured Query Language” and can be pronounced as “SQL” or “sequel – (Structured English.
SQL Key Revision Points.
MySQL Subquery Source: Dev.MySql.com
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
Database Security.
Chapter 9 SQL and RDBMS Part C. SQL
Database Systems: Design, Implementation, and Management Tenth Edition
UNIT - V STORED PROCEDURE.
Database Security.
Basic Nested Queries and Views
Chapter 11 Functions and Groups
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter 2 Views.
Database Processing: David M. Kroenke’s Chapter Seven:
Chapter 7 Introduction to Structured Query Language (SQL)
Chapter 2 Views.
Chapter 7 Using SQL in Applications
Chapter 7 Using SQL in Applications
Chapter 8 Advanced SQL.
Chapter 9 Query-by-Example Pearson Education © 2009.
Database Systems: Design, Implementation, and Management Tenth Edition
So What are Views and Triggers anyway?
Query-by-Example Transparencies
Presentation transcript:

Chapter 13 Subqueries and Views Part C. SQL Chapter 13 Subqueries and Views Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co. Contents Chapter 13 Subqueries and Views 13.1 Introduction to Subqueries 13.2 Type 1: Single-valued Subqueries 13.2 A. Subqueries with Aggregate Functions 13.2 B. Involving two different tables 13.3 Type 2: Multi-valued Subqueries 13.3 A. The IN Operator 13.3 B. The = ANY Operator 13.3 C. The EXISTS Operator 13.4 Further Examples 13.5 Views 13.5 A. Creating a Simple view 13.5 B. Finding the Mode Copyright 2005 Radian Publishing Co.

Chapter 13 Subqueries and Views A subquery is a query which is embedded in another query. A view is a subset of the database that is presented to some users. It is a virtual table that does not take up as much physical space as an ordinary table does, yet it allows you to perform query and updating to the database. Copyright 2005 Radian Publishing Co.

13.1 Introduction to Subqueries (1/2) If a query consists of a subquery, the subquery will always be executed first. There are several variations in subqueries: The subquery and the main query may refer to the same table, or different tables. The result of a subquery may be a single value, or a set of values. The subquery may be placed in the WHERE clause, or in the FROM clause. A subquery may consist of another subquery, i.e. multi-sub-level. Copyright 2005 Radian Publishing Co.

13.1 Introduction to Subqueries (2/2) Singled-valued subqueries need relational operators, like =, >, >=, <>, <, <=; Multi-valued subqueries need operators like IN, ANY and EXISTS. Copyright 2005 Radian Publishing Co.

13.2 Type 1: Single-valued Subqueries Single-valued subquery: SELECT ... FROM Table1 WHERE FieldName1 =|>|>=|<|<=|<> (SELECT ... from Table2) The query and the subquery may use the same table or different tables. Copyright 2005 Radian Publishing Co.

13.2 A. Subqueries with Aggregate Functions A subquery with an aggregate function returns a single value. Copyright 2005 Radian Publishing Co.

13.2 B. Involving two different tables The query and the subquery may use different tables. Copyright 2005 Radian Publishing Co.

13.3 Type 2: Multi-valued Subqueries Some subqueries return a set of records. We cannot use simple relational operators, like =, >, <, <> etc. Special operators, like IN and EXISTS, are designed for multi-valued subqueries. Multi-valued subquery: SELECT ... FROM Table1 WHERE FieldName1 [IN | = ANY | EXISTS] (SELECT ... from Table2) Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co. 13.3 A. The IN Operator The IN operator compares a piece of data with a set of values and returns true if any one of the values in the set matches with the data. Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co. 13.3 B. The = ANY Operator The operator = ANY is the same as the IN operator. Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co. 13.3 C. The EXISTS Operator The function EXISTS() is a boolean function which tells whether the result of a subquery is successful or not. Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co. 13.4 Further Examples Refer to textbook P. 314 Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co. 13.5 Views (1/2) A view is a named query result from one or more tables in a database. A view is often called a virtual table. You can perform queries and updating on a view, yet it does not take up as much physical space as an ordinary table. When a view is changed, the tables that the view is built from will be changed. Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co. 13.5 Views (2/2) The major advantages of views are: A view can save the effort of entering the same complicated SQL statement every time. A view can enforce security. It can prevent users from viewing other data that may be private or confidential. Copyright 2005 Radian Publishing Co.

13.5 A. Creating a Simple view (1/2) The basic syntax creating a view is CREATE VIEW ViewName AS SELECT ... Copyright 2005 Radian Publishing Co.

13.5 A. Creating a Simple view (2/2) To make a query on the view SALESVIEW Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co. 13.5 B. Finding the Mode (1/3) We may use View to help finding the mode, which tells the most popular quantity. For example: Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co. 13.5 B. Finding the Mode (2/3) It is clear that “Central”, “Tai Kok Tsui” and “Tai Po” are popular district. Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co. 13.5 B. Finding the Mode (3/3) The results of the query show the most popular districts. Copyright 2005 Radian Publishing Co.