SQL 資料庫查詢語言 取材自 EIS, 3 rd edition By Dunn et al..

Slides:



Advertisements
Similar presentations
Information Retrieval from Relational Databases
Advertisements

Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Concepts of Database Management Seventh Edition
Concepts of Database Management Sixth Edition
Concepts of Database Management Seventh Edition
Introduction to Structured Query Language (SQL)
The University of Akron Dept of Business Technology Computer Information Systems The Relational Model: Query-By-Example (QBE) 2440: 180 Database Concepts.
XP Chapter 3 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Analyzing Data For Effective Decision Making.
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
Chapter 2 Querying a Database
Concepts of Database Management Sixth Edition
Inner join, self join and Outer join Sen Zhang. Joining data together is one of the most significant strengths of a relational database. A join is a query.
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
Chapter 4 Relational Databases and Enterprise Systems
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
DAY 16: ACCESS CHAPTER 2 Tazin Afrin October 10,
Concepts of Database Management, Fifth Edition
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Chapter 3 Single-Table Queries
Excel Projects 5 & 6 Notes Mr. Ursone. Excel Project 5: Sorting a List  Sorting: Arranging records in a specific sequence  The Sort command is on the.
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
Access Queries Office 2013/ Queries Most common type of Query is selection(projection) Specify sources for data retrieval table(s) and/or query(ies)
Database Queries. Queries Queries are questions used to retrieve information from a database. Contain criteria to specify the records and fields to be.
Analyzing Data For Effective Decision Making Chapter 3.
Inner Join vs. Outer Join
1 By: Nour Hilal. Microsoft Access is a database software where data is stored in one or more Tables. A Database is a group of related Tables. Access.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
1 The Relational Database Model. 2 Learning Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Concepts of Database Management Seventh Edition
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
DATABASE TRANSACTION. Transaction It is a logical unit of work that must succeed or fail in its entirety. A transaction is an atomic operation which may.
Database Systems Microsoft Access Practical #3 Queries Nos 215.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
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.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
® Microsoft Access 2010 Tutorial 3 Maintaining and Querying a Database.
COMPREHENSIVE Access Tutorial 3 Maintaining and Querying a Database.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Database Applications – Microsoft Access Lesson 4 Working with Queries 36 Slides in Presentation.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
1 2 Concepts of Database Management, 4 th Edition, Pratt & Adamski Chapter 2 The Relational Model 1: Introduction, QBE, and Relational Algebra.
(SQL - Structured Query Language)
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
PeopleSoft Financials Advanced Query Training Financial Information Systems and Reporting Controller’s Division
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
SQL Query Getting to the data ……..
Database Systems: Design, Implementation, and Management Tenth Edition
GO! with Microsoft® Access e
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Database Queries.
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Structured Query Language
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
Shelly Cashman: Microsoft Access 2016
Database Systems: Design, Implementation, and Management
Presentation transcript:

SQL 資料庫查詢語言 取材自 EIS, 3 rd edition By Dunn et al.

2 Examples of Needs for Multiple Views of One Data Set Cash-basis versus Accrual Accounting Weighted Average versus FIFO or LIFO Double-Declining Balance Depreciation versus Straight Line Foreign Currency Translation How do we get these multiple views???????

3 Answer: Query the Data Set What is Querying? –It is asking questions about the data in the database and manipulating or combining the data in different ways –We can isolate certain rows in tables, we can isolate certain columns in tables, we can join tables together, we can create calculations based on various data items, etc.

4 Generating Reports Ad-hoc reports –The user can easily manipulate the base level tables to generate information on an ad hoc (as needed) basis. –This is the most effective way to provide data availability to users of the database when their information needs change on an ongoing basis. Regular Reports –We can actually create views of the data that look like additional tables, but are just alternative ways to view the data that already exists in the database. –The data are not copied to a second physical location in the database. –Instead, a view creates the appearance of a different set of tables for the user in the format the user wants to see. –The Schema for the Client Billing and Human Resources Portion of the Database is provided in Figure 6.17 –Creating a View of the Client Billing Detail with SQL is provided in Figure 6.18

5 Three Query Languages Relational Algebra –Three main operators: Select, Project, Join –Provides the conceptual basis for SQL and QBE Structured Query Language (SQL) –The user enters commands according to a pre-defined syntax to retrieve desired data. Query By Example (QBE) –The user starts with a sample of the table(s) columns and marks the fields he or she wants to include in the answer. Defaults are available for summarizing and manipulating the data.

6 Relational Algebra Select –includes only certain rows from a database table in its “answer”. Project –includes only certain columns from a database table in its “answer” Join –combines two or more database tables on the basis of one or more common attributes

7 Example Tables (Incomplete Enterprise Database) from Dunn & McCarthy (2004) working paper

8 Relational Algebra SELECT Find the cash receipts from Customer #2 (keeping all the details of those cash receipts) Select Cash Receipt Where Customer Number = C-2 Giving Answer

9 Relational Algebra PROJECT Find the customer number, name, and salesperson number for all customers Project Customer Over (Customer#, Name, SP#) Giving Answer

10 Join Types Inner join –includes only the records from both tables that have the exact same values in the fields that are joined –I.e., Outer join –includes all records from one table, and matches those records from the other table for which values in the joined fields are equal –I.e., Left Outer JoinRight Outer Join

11 Relational Algebra Inner Join Find all details of all customers and all available details of each customer’s salesperson Join Customer, Salesperson Where Customer.SP# = [Salesperson.Employee Number] Giving Answer

12 Relational Algebra Left Outer Join Find all details of all sales and the cash receipt number and amount applied of any cash receipts related to those sales Left Outer Join Sale, [Sale - CashRecDuality] Where [Sale.Sale#] = [Sale - CashRecDuality.Sale#] Giving Answer

13 SQL (Structured Query Language) Each query statement follows the same structure: SELECT attribute name(s) FROM table name(s) WHERE criteria is met;

14 SQL Statements and Relational Algebra SQL’s SELECT component isolates columns –i.e., relational algebra’s project SQL’s FROM component is used for identifying the table(s) involved –if >1 table, helps accomplish relational algebra’s join (together with WHERE component that specifies equal fields) SQL’s WHERE component isolates rows –i.e., relational algebra’s select –also helps accomplish relational algebra’s join –may be left blank for single-table queries that retrieve all rows

15 Find the cash receipts from Customer #2 (keeping all the details of those cash receipts) SQL and Relational Algebra SELECT Select * From [Cash Receipt] Where [Customer Number] = C-2; (note: the brackets are needed because of spaces in the table and field names; also note * is a wild card indicating all columns should be included)

16 SQL and Relational Algebra PROJECT Select Customer#, Name, SP# From Customer; Find the customer number, name, and salesperson number for all customers

17 SQL and Relational Algebra Inner Join Select * From Customer, Salesperson Where Customer.SP# = [Salesperson.Employee Number]; Find all details of all customers and all available details of each customer’s salesperson

18 SQL and Relational Algebra Outer Join Find all details of all sales and the cash receipt number and amount applied of any cash receipts related to those sales Select * From Sale LeftJoin [Sale-CashRecDuality] Where [Sale.Sale#]=[Sale-CashRecDuality.Sale#];

19 Mathematical Comparison Operators SQL Queries may include mathematical comparison operators such as –= equal to –< less than –<= less than or equal to –> greater than –>= greater than or equal to –<> not equal to (or != in some software) Mathematical comparison operators are typically included in the WHERE clause of the SQL statement, and may be used on all types of fields –For date fields, dates that are earlier in time are “less than” dates that are later in time. –For text fields, A < B < C, etc.

20 SQL Mathematical Comparison Operators Select Account#, Balance From Cash Where Balance>=50000;

21 SQL Mathematical Comparison Operators on Character Attributes Select Sale#, Amount From Sale Where SalesRep# <> E- 10;

22 Queries with Logical Operators Queries may include logical operators AND, OR, and NOT –AND accomplishes a set intersection – answer includes all instances that meet BOTH conditions –OR accomplishes a set union – answer includes all instances that meet one condition and all instances that meet the other condition –NOT identifies instances that do not meet one or more conditions

23 Queries with Special Operators BETWEEN is used to define the range limits. –The end points of the range are included Select Sale#, Amount, Date From Sale Where Date BETWEEN 7/1 and 7/31;

24 Queries with Special Operators IS NULL is used to retrieve attributes for which the value is null. Select * From Cash Where Balance IS NULL;

25 Queries with Special Operators EXISTS is used to retrieve attributes for which the value is not null. Select * From Cash Where Balance EXISTS;

26 Aggregation Functions in Queries An aggregation function summarizes the data values within a field (column) –COUNT summarizes the number of rows that contain a given value in the field –AVERAGE computes the arithmetic mean value of all rows included in the answer –SUM computes the arithmetic sum of all rows included in the answer –MIN identifies the minimum (lowest) attribute value for the field –MAX identifies the maximum (greatest) attribute value for the field

27 Queries with Horizontal Calculations “Horizontal” calculations mathematically combine values from different fields for each row –Horizontal calculations should NOT be included in the same query as an aggregation function One query may perform a horizontal calculation and another query that builds on the first query may perform the aggregation function, or vice versa –The “correct” order for the queries depends on the goal