M. E. Kabay, PhD, CISSP-ISSMP V:

Slides:



Advertisements
Similar presentations
1 All Powder Board and Ski Microsoft Access Workbook Chapter 5: Advanced Queries Jerry Post Copyright © 2007.
Advertisements

1 Copyright © 2010 Jerry Post with additions & narration by M. E. Kabay. All rights reserved. Advanced Queries IS240 – DBMS Lecture # 8 – M.
5 Chapter 5 Structured Query Language (SQL2) Revision.
Chapter Eight Managing Data Resources File Organization and Concepts.
DATABASE APPLICATION DEVELOPMENT SAK 3408 Query Statements.
Jerry Post McGraw-Hill/Irwin Copyright © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Database Management Systems Chapter 4 Data Queries.
1.NET Web Forms Database Queries © 2002 by Jerry Post.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
Copyright © 2010 M. E. Kabay. All rights reserved. Analyzing and Building Simple Queries in SQL IS240 – Database Management Supplement to Chapter 4 M.
Jerry Post Copyright © Database Management Systems Chapter 4 Queries.
Copyright 2007, Paradigm Publishing Inc. BACKNEXTEND 3-1 LINKS TO OBJECTIVES Save a Filter as a Query Save a Filter as a Query Parameter Query Inner, Left,
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
Chapter 3 Query Language Database Application SAK 3408.
Chapter 4 Querying Based on G. Post, DBMS: Designing & Building Business Applications University of Manitoba Asper School of Business 3500 DBMS Bob Travica.
Chapter 8 Newer Database Topics Based on G. Post, DBMS: Designing & Building Business Applications University of Manitoba Asper School of Business 3500.
1 All Powder Board and Ski Microsoft Access Workbook Chapter 4: Queries Jerry Post Copyright © 2007.
Chapter 5 Advanced Querying
1 All Powder Board and Ski Oracle 9i Workbook Chapter 7: Integrity and Transactions Jerry Post Copyright © 2003.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
1 All Powder Board and Ski SQL Server Workbook Chapter 2: Database Design Jerry Post Copyright © 2004.
Jerry Post Copyright © Database Management Systems Chapter 4 Queries.
School of Computer & Communication of LNPU 辽宁石油化工大学计算机与通信工程学院 刘旸 1 数据库管理系统 Database Management Systems Chapter4 Queries 第四章 数据查询.
Jerry Post Copyright © Database Management Systems Chapter 5 Advanced Queries.
劉 志 俊 (Chih-Chin Liu) 中華大學 資訊工程系 October 2001 Chap 9 SQL (III): Advanced Queries.
© 2002 by Prentice Hall 1 Database Processing with Microsoft Access David M. Kroenke Database Concepts 1e Appendix A.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
All Powder Board and Ski Microsoft Access Workbook Chapter 2: Database Design Jerry Post Copyright © 2003.
1 Copyright © 2010 Jerry Post & M. E. Kabay. All rights reserved. Queries: Part 2 of 2 IS240 – DBMS Lecture # 7 – M. E. Kabay, PhD, CISSP-ISSMP.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Aliya Farheen October 29,2015.
1 All Powder Board and Ski Microsoft Access Workbook Chapter 5: Advanced Queries Jerry Post Copyright © 2003.
1 Copyright © 2010 Jerry Post & M. E. Kabay. All rights reserved. Queries: Part 1 of 2 IS240 – DBMS Lecture # 6 – M. E. Kabay, PhD, CISSP-ISSMP.
Jerry Post Copyright © Database Management Systems Chapter 4 Queries.
Jerry Post McGraw-Hill/Irwin Copyright © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Database Management Systems Chapter 4 Data Queries.
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
Jerry Post Copyright © Database Management Systems Chapter 4 Queries.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
1 All Powder Board and Ski SQL Server Workbook Chapter 5: Advanced Queries Jerry Post Copyright © 2003.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
1 Ch4 Summary Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
Advanced Query Analysis
How to: SQL By: Sam Loch.
Web Systems & Technologies
Working with MS-ACCESS
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Prepared By: Bobby Wan Microsoft Access Prepared By: Bobby Wan
Chapter 1 Introduction.
Forms & Reports Part 1 of 2 IS240 – DBMS Lecture #9 –
Chapter 6: Forms, Reports and Applications All Powder Board and Ski
Queries: Part 1 of 2 IS240 – DBMS Lecture # 6 –
Introduction to Databases by Dr. Soper extended with more examples
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Chapter 2: Database Design All Powder Board and Ski
All Powder Board and Ski
All Powder Board and Ski
DB Integrity & Transactions Part 2
ISC440: Web Programming 2 Server-side Scripting PHP 3
Structured Query Language (SQL) William Klingelsmith
CHAPTER 7: ADVANCED SQL.
Supplement: Using the DBDesign System
Analyzing and Building Simple Queries in SQL
Insert, Update, Delete Manipulating Data.
Introduction to DBMS IS240 – DBMS Lecture #2 –
Exploring Microsoft® Access® 2016 Series Editor Mary Anne Poatsy
Advanced Joins IN ( ) Expression Subqueries with IN ( ) Expression
Presentation transcript:

M. E. Kabay, PhD, CISSP-ISSMP mailto:mekabay@gmail.com V: 802.479.7937 Queries: Part 2 of 2 IS240 – DBMS Lecture # 7 – 2010-02-22 M. E. Kabay, PhD, CISSP-ISSMP Assoc. Prof. Information Assurance School of Business & Management, Norwich University mailto:mekabay@gmail.com V: 802.479.7937 Class Notes Copyright © 2004 M. E. Kabay. All rights reserved. Page 1

Topics Multiple Tables (Intro & Distinct) Joining Tables SQL JOIN Syntax for Three Tables Multiple Tables (Many) Building a Query Joining Tables (Hints) Tables with Multiple Joins Table Alias Saved Query: Create View Updateable & Non-updateable Views As usual, this material was created by Prof Jerry Post and reformatted by Prof M. E. Kabay for use in IS240 at Norwich University Copyright © 2004 M. E. Kabay. All rights reserved. Page 2

Multiple Tables (Intro & Distinct) SaleID SaleDate EmployeeID CustomerID SalesTax Sale SELECT DISTINCT CustomerID FROM Sale WHERE (SaleDate Between ’01-Apr-2004’ And ’31-May-2004’) ORDER BY CustomerID; CustomerID 6 8 14 19 22 24 28 36 37 38 39 42 50 57 58 63 74 80 90 Field CustomerID SaleDate Table Sale Sort Ascending Criteria Between ’01-Apr-2004’ And ’31-May-2004’ Or List the CustomerID of everyone who bought something between 01-Apr-2004 and 31-May-2004. Copyright © 2004 M. E. Kabay. All rights reserved. Page 3

Problem: CustomerID not Ideal We would much rather see the name of the customer than the CustomerID only The Customer’s last name is in the Customer table We will create a VIEW that joins the information from the Sale table with the information from the Customer table Think of the JOIN operation as creating a synthetic table that combines all the records from both tables This allows us to SELECT the right information, including LastName, from the combined records Copyright © 2004 M. E. Kabay. All rights reserved. Page 4

Joining Tables SELECT DISTINCT Sale.CustomerID, Customer.LastName FROM Customer INNER JOIN Sale ON Customer.CustomerID = Sale.CustomerID WHERE (SaleDate Between ’01-Apr-2004’ And ’31-May-2004’) ORDER BY Customer.LastName; SaleID SaleDate EmployeeID CustomerID Sale Phone FirstName LastName Customer CustomerID LastName 22 Adkins 57 Carter 38 Franklin 42 Froedge 63 Grimes 74 Hinton 36 Holland 6 Hopkins 50 Lee 58 McCain … Field CustomerID LastName SaleDate Table Sale Customer Sort Ascending Criteria Between ’01-Apr-2004’ And ’31-May-2004’ Or List LastNames of Customers who bought between 4/1/2004 and 5/31/2004. Copyright © 2004 M. E. Kabay. All rights reserved. Page 5

SQL JOIN FROM table1 INNER JOIN table2 We use this syntax throughout IS240 FROM table1 INNER JOIN table2 ON table1.column = table2.column SQL 92 syntax (Access and SQL Server) FROM table1, table2 WHERE table1.column = table2.column Avoid using this syntax: can cause errors SQL 89 syntax (Oracle) FROM table1, table2 JOIN table1.column = table2.column Informal syntax Copyright © 2004 M. E. Kabay. All rights reserved. Page 6

Syntax for Three Tables SQL ‘92 syntax to join three tables FROM Table1 INNER JOIN (Table2 INNER JOIN Table3 ON Table2.ColA = Table3.ColA) ON Table1.ColB = Table2.ColB Copyright © 2004 M. E. Kabay. All rights reserved. Page 7

Multiple Tables (Many) SELECT DISTINCTROW Customer.LastName, Customer.Phone FROM Customer INNER JOIN (Sale INNER JOIN (Animal INNER JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID) ON Sale.SaleID = SaleAnimal.SaleID) ON Customer.CustomerID = Sale.CustomerID WHERE ((Animal.Category=‘Cat’) AND (Animal.Registered Is Not Null) AND (Color Like ‘%White%’) AND (SaleDate Between ’01-Jun-2004’ And ’31-Dec-2004’)); Animal SaleAnimal Sale Customer AnimalID Name Category Breed SaleID AnimalID SalePrice SaleID SaleDate EmployeeID CustomerID CustomerID Phone FirstName LastName Field LastName Phone Category Registered Color SaleDate Table Customer Animal Sale Sort Ascending Criteria ‘Cat’ Is Not Null Like ‘%White%’ Between ’01-Jun-2004’ And ’31-Dec-2004’ Or List the Last Name and Phone of anyone who bought a registered White cat between 6/1/2004 and 12/31/2004. Copyright © 2004 M. E. Kabay. All rights reserved. Page 8

Building a Query Identify the tables involved. List the Last Name and Phone of anyone who bought a registered White cat between 6/1/04 and 12/31/04. Identify the tables involved. Look at the columns you want to see. LastName, Phone: Customer Look at the columns used in the constraints. Registered, Color, Category: Animal Sale Date: Sale Find connector tables. To connect Animal to Sale: SaleAnimal Select the desired columns and test the query. Enter the constraints. Set Order By columns. Add Group By columns. Add summary computations to the SELECT statement. Copyright © 2004 M. E. Kabay. All rights reserved. Page 9

Joining Tables (Hints) Build Relationships First Drag and drop From one side to many side Avoid multiple ties between tables SQL FROM Table1 INNER JOIN Table2 ON Table1.ColA = Table2.ColB Join columns are often keys, but they can be any columns--as long as the domains (types of data) match. Multiple Tables FROM (Table1 INNER JOIN Table2 ON T1.ColA = T2.ColB ) INNER JOIN Table3 ON T3.ColC = T3.ColD Shorter Notation FROM T1, T2, T3 JOIN T1.ColA = T2.ColB T1.ColC = T3.ColD Shorter Notation is not correct syntax, but it is easier to write. Copyright © 2004 M. E. Kabay. All rights reserved. Page 10

Tables with Multiple Joins Potential problem with three or more tables. Access uses predefined relationships to automatically determine JOINs. JOINS might loop. Most queries will not work with loops. A query with these four tables with four JOINS would only return rows where the Employee had the same ZipCode as the Supplier. If you only need the Supplier city, just delete the JOIN between Employee and ZipCode. If you want both cities, add the ZipCode table again as a fifth table. Copyright © 2004 M. E. Kabay. All rights reserved. Page 11

Table Alias City Supplier AnimalOrder Employee City2 CityID ZipCode City State SupplierID Address ZipCode CityID OrderDate SupplierID ShippingCost EmployeeID EmployeeID LastName ZipCode CityID CityID ZipCode City State SELECT Supplier.SID, Supplier.CityID, City.City, Employee.EID, Employee.LastName, Employee.CityID, City2.City FROM (City INNER JOIN Supplier ON City.CityID = Supplier.CityID) INNER JOIN ((City AS City2 INNER JOIN Employee ON City2.CityID = Employee.CityID) INNER JOIN AnimalOrder ON Employee.EmployeeID = AnimalOrder.EmployeeID) ON Supplier.SupplierID = AnimalOrder.SupplierID; SID Supplier.CityID City.City EID LastName Employee.CityID City2.City 4 7972 Middlesboro 5 James 7083 Orlando 2 10896 Springfield 1 Reeves 9201 Lincoln 4 7972 Middlesboro 3 Reasoner 8313 Springfield 9 10740 Columbia 8 Carpenter 10592 Philadelphia 5 10893 Smyrna 3 Reasoner 8313 Springfield Copyright © 2004 M. E. Kabay. All rights reserved. Page 12

Saved Query: Create View Examples: Save a query Faster: only enter once Faster: only analyze once Any SELECT statement Can use the View within other SQL queries. CREATE VIEW Kittens AS SELECT * FROM Animal WHERE (Category = ‘Cat’) AND (Today - DateBorn < 180); SELECT Avg(ListPrice) FROM Kittens WHERE (Color LIKE ‘%Black%’); Copyright © 2004 M. E. Kabay. All rights reserved. Page 13

Updateable Views OrderItem(OrderID, ItemID, Quantity) Item(ItemID, Description) OrderLine(OrderID, ItemID, Description, Quantity) To be updateable, a view must focus on one primary table. (OrderItem) Goal is to change data in only one table. (OrderItem) Data can be displayed from other tables. (Item) Never include or attempt to change primary keys from more than one table. (Item.ItemID) Copyright © 2004 M. E. Kabay. All rights reserved. Page 14

Non-Updateable View OrderItem(OrderID, ItemID, Quantity) Item(ItemID, Description) 121 57 3 121 82 2 122 57 1 57 Cat food 58 Dog food 59 Bird food OrderLine(OrderID, Item.ItemID, Description, Quantity) 121 57 Cat food 3 121 82 Bird feeder 2 122 57 Cat food 1 32 If you attempt to change the Item.ItemID in the OrderLineView: You will simply change the primary key value in the Item table. It will not add a new row to the OrderItem table. Copyright © 2004 M. E. Kabay. All rights reserved. Page 15

Homework By Sunday 28 Feb 2010 Chapter 4 main text page 176: Sally’s Pet Store DB from your Student CD Questions 8,10,12,14,16,18, 20 Copyright © 2004 M. E. Kabay. All rights reserved. Page 16

DISCUSSION Copyright © 2004 M. E. Kabay. All rights reserved. Page 17