1 Copyright © 2010 Jerry Post & M. E. Kabay. All rights reserved. Queries: Part 2 of 2 IS240 – DBMS Lecture # 7 – 2010-02-22 M. E. Kabay, PhD, CISSP-ISSMP.

Slides:



Advertisements
Similar presentations
All Powder Board and Ski
Advertisements

1 All Powder Board and Ski Microsoft Access Workbook Chapter 5: Advanced Queries Jerry Post Copyright © 2007.
Jerry Post Copyright © Database Management Systems Chapter 5 Advanced Queries.
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.
Chapter 2 Database System Design (part II)
8 Copyright © 2004, Oracle. All rights reserved. Creating LOVs and Editors.
DATABASE APPLICATION DEVELOPMENT SAK 3408 Query Statements.
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
Jerry Post McGraw-Hill/Irwin Copyright © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Database Management Systems Chapter 4 Data Queries.
Access Lecture 1 Database Overview and Creating Tables Create an Employee Table.
1.NET Web Forms Database Queries © 2002 by Jerry Post.
Structured Query Language SQL: An Introduction. SQL (Pronounced S.Q.L) The standard user and application program interface to a relational database is.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
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.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
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.
Working with MS-ACCESS IS 240 – Database Management Lecture #4 – Assoc. Prof. M. E. Kabay, PhD, CISSP-ISSMP Norwich University
1 All Powder Board and Ski Microsoft Access Workbook Chapter 4: Queries Jerry Post Copyright © 2007.
Chapter 5 Advanced Querying
DBSYSTEMS Chapter 3 Data Normalization Get data properly tabled! Based on G. Post, DBMS: Designing & Building Business Applications University of Manitoba.
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 第四章 数据查询.
1.NET Web Forms Business Forms © 2002 by Jerry Post.
Microsoft Access Intro Class 6 Relationships.
Jerry Post Copyright © Database Management Systems Chapter 5 Advanced Queries.
劉 志 俊 (Chih-Chin Liu) 中華大學 資訊工程系 October 2001 Chap 9 SQL (III): Advanced Queries.
1 All Powder Board and Ski Oracle 9i Workbook Chapter 4: Queries Jerry Post Copyright © 2003.
© 2002 by Prentice Hall 1 Database Processing with Microsoft Access David M. Kroenke Database Concepts 1e Appendix A.
Database Processing with Microsoft Access Appendix DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
1 All Powder Board and Ski Microsoft Access Workbook Chapter 6: Forms, Reports, and Applications Jerry Post Copyright © 2003.
All Powder Board and Ski Microsoft Access Workbook Chapter 2: Database Design Jerry Post Copyright © 2003.
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.
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.
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.
1 Ch4 Summary Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University.
Advanced Query Analysis
Chapter 1 Introduction.
Chapter 6: Forms, Reports and Applications All Powder Board and Ski
Queries: Part 1 of 2 IS240 – DBMS Lecture # 6 –
M. E. Kabay, PhD, CISSP-ISSMP V:
Chapter 2: Database Design All Powder Board and Ski
All Powder Board and Ski
All Powder Board and Ski
DB Integrity & Transactions Part 2
Structured Query Language (SQL) William Klingelsmith
Supplement: Using the DBDesign System
Analyzing and Building Simple Queries in SQL
Introduction to DBMS IS240 – DBMS Lecture #2 –
Advanced Joins IN ( ) Expression Subqueries with IN ( ) Expression
Presentation transcript:

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 Assoc. Prof. Information Assurance School of Business & Management, Norwich University V:

2 Copyright © 2010 Jerry Post & M. E. Kabay. All rights reserved. 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

3 Copyright © 2010 Jerry Post & M. E. Kabay. All rights reserved. Multiple Tables (Intro & Distinct) List the CustomerID of everyone who bought something between 01-Apr-2004 and 31-May CustomerID SELECT DISTINCT CustomerID FROM Sale WHERE (SaleDate Between ’01-Apr-2004’ And ’31-May-2004’) ORDER BY CustomerID; SaleID SaleDate EmployeeID CustomerID SalesTax Sale FieldCustomerIDSaleDate TableSale SortAscending CriteriaBetween ’01-Apr-2004’ And ’31-May-2004’ Or

4 Copyright © 2010 Jerry Post & M. E. Kabay. All rights reserved. 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

5 Copyright © 2010 Jerry Post & M. E. Kabay. All rights reserved. Joining Tables List LastNames of Customers who bought between 4/1/2004 and 5/31/2004. 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; FieldCustomerIDLastNameSaleDate TableSaleCustomerSale SortAscending CriteriaBetween ’01-Apr-2004’ And ’31-May-2004’ Or CustomerIDLastName 22Adkins 57Carter 38Franklin 42Froedge 63Grimes 74Hinton 36Holland 6Hopkins 50Lee 58McCain … SaleID SaleDate EmployeeID CustomerID Sale CustomerID Phone FirstName LastName Customer

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

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

8 Copyright © 2010 Jerry Post & M. E. Kabay. All rights reserved. Multiple Tables (Many)  List the Last Name and Phone of anyone who bought a registered White cat between 6/1/2004 and 12/31/2004. 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’)); SaleID SaleDate EmployeeID CustomerID Sale FieldLastNamePhoneCategoryRegisteredColorSaleDate TableCustomer Animal Sale SortAscending Criteria‘Cat’Is Not NullLike ‘%White%’Between ’01-Jun-2004’ And ’31-Dec-2004’ Or CustomerID Phone FirstName LastName Customer SaleID AnimalID SalePrice SaleAnimal AnimalID Name Category Breed Animal

9 Copyright © 2010 Jerry Post & M. E. Kabay. All rights reserved. Building a Query  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.

10 Copyright © 2010 Jerry Post & M. E. Kabay. All rights reserved. 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.

11 Copyright © 2010 Jerry Post & M. E. Kabay. All rights reserved. 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.

12 Copyright © 2010 Jerry Post & M. E. Kabay. All rights reserved. Table Alias SIDSupplier.CityIDCity.CityEIDLastNameEmployee.CityIDCity2.City 47972Middlesboro5James7083Orlando Springfield1Reeves9201Lincoln 47972Middlesboro3Reasoner8313Springfield Columbia8Carpenter10592Philadelphia Smyrna3Reasoner8313Springfield 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; EmployeeID LastName ZipCode CityID Employee CityID ZipCode City State City2 SupplierID Address ZipCode CityID Supplier CityID ZipCode City State City OrderDate SupplierID ShippingCost EmployeeID AnimalOrder

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

14 Copyright © 2010 Jerry Post & M. E. Kabay. All rights reserved. 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)

15 Copyright © 2010 Jerry Post & M. E. Kabay. All rights reserved. Non-Updateable View OrderItem(OrderID, ItemID, Quantity)Item(ItemID, Description) OrderLine(OrderID, Item.ItemID, Description, Quantity) Cat food 58Dog food 59Bird food 12157Cat food Bird feeder Cat food1 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. 32

16 Copyright © 2010 Jerry Post & M. E. Kabay. All rights reserved. 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

17 Copyright © 2010 Jerry Post & M. E. Kabay. All rights reserved. DISCUSSION