* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Query Joins
Microsoft Jet Database Engine wikipedia , lookup
Concurrency control wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Open Database Connectivity wikipedia , lookup
Clusterpoint wikipedia , lookup
Database model wikipedia , lookup
Relational model wikipedia , lookup
Inner Join vs. Outer Join Information Retrieval from Relational Databases 1 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 predefined 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. 2 SQL (Structured Query Language) • Each query statement follows the same structure: SELECT attribute name(s) FROM table name(s) WHERE criteria is met; 3 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 4 Join Types • Inner join(default in access) • 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 Right Outer Join • I.e., Left Outer Join 5 Example Tables Employee EmplD SocialSec# E1 123345678 E2 234456789 E3 345567890 E4 456678901 E5 567789101 E6 678891012 Department DepartmentID D1 D2 D3 D4 LastName Adams Boston Crabb Davis Engler Folkert FirstName Anita Benjamin Charlie Deborah Edward Fawn Street Address 144 Apple St. 255 Banana Rd. 366 Cherry Ave. 477 Dip Dr. 588 Eggplant St. 699 Fruity Ave. Pay rate Telephone $10.00 555-1234 $12.00 555-2345 $14.00 555-3456 $32.00 555-4567 $11.00 555-5678 $23.00 555-6789 DeptID D4 D2 D2 D1 D4 D3 TrainingCourse Name Executive Management Accounting Information Systems Operations CourseID AC1 AC2 IS1 IS2 MD100 Description Accounting Fundamentals Chart of Accounts Basic Information Systems Database Design ERP Systems Length 2 days 5 days 5 days 5 days 10 days CourseTaken EmplID E3 E3 E4 E6 E6 CourseID AC1 AC2 AC1 IS1 IS2 DateTaken May 1-2 June 24-28 Oct 14-15 June 24-28 July 8-12 6 Relationship View 7 Query to List ALL Employees and Description of any Training Courses Taken ( Inner Join) 8 Example Tables Note: We have 6 employees Employee EmplD SocialSec# E1 123345678 E2 234456789 E3 345567890 E4 456678901 E5 567789101 E6 678891012 Department DepartmentID D1 D2 D3 D4 LastName Adams Boston Crabb Davis Engler Folkert FirstName Anita Benjamin Charlie Deborah Edward Fawn Street Address 144 Apple St. 255 Banana Rd. 366 Cherry Ave. 477 Dip Dr. 588 Eggplant St. 699 Fruity Ave. Pay rate Telephone $10.00 555-1234 $12.00 555-2345 $14.00 555-3456 $32.00 555-4567 $11.00 555-5678 $23.00 555-6789 DeptID D4 D2 D2 D1 D4 D3 TrainingCourse Name Executive Management Accounting Information Systems Operations CourseID AC1 AC2 IS1 IS2 MD100 Description Accounting Fundamentals Chart of Accounts Basic Information Systems Database Design ERP Systems Length 2 days 5 days 5 days 5 days 10 days CourseTaken EmplID E3 E3 E4 E6 E6 CourseID AC1 AC2 AC1 IS1 IS2 DateTaken May 1-2 June 24-28 Oct 14-15 June 24-28 July 8-12 9 Does this This Give Us What we Need? LastName Crabb Crabb Folkert Folkert Davis FirstName Charlie Charlie Fawn Fawn Deborah CourseID AC1 AC2 IS1 IS2 AC1 Description Accounting Fundamentals Chart of Accounts Basic Information Systems Database Design Accounting Fundamentals The above based upon the default join which is the inner join and will not give us employees that have not taken training classes SQL code created by access for QBE on previous slide 10 Using Outer Joins Note change 11 LastName Adams Boston Crabb Crabb Davis Engler Folkert Folkert FirstName Anita Benjamin Charlie Charlie Deborah Edward Fawn Fawn CourseID Description AC1 AC2 AC1 Accounting Fundamentals Chart of Accounts Accounting Fundamentals IS1 IS2 Basic Information Systems Database Design SQL code created by access for QBE on previous slide 12 The Revenue Cycle of M&M M&M sells its agleclaps to customers through a network of company salespeople. Each type of agleclap is bought from a particular vendor and is given an initial list price. Each salesperson services a separate group of customers and is allowed to offer them various discounts from list to induce sales. Each sale can include one or more types of agleclaps and can be paid for in any one of three ways: (1) immediately in cash, (2) on the 15th of the following month, or (3) over the course of six months. When cash is received, a cashier deposits it into a company bank account. Sales are signaled by invoices; cash receipts by remittance advices 13 (0, N) M Sale-lineitem (1, N) (1, 1) SALE N N (0, N) SALE Inside party (0, N) HIGHEST-DEGREE NAME MONTHLYPAY EMPLOYEE # SALE-AMOUNT SALE-DATE INVOICE # ACTUAL-PRICE-OFEACH-AGLECLAP QUANTITY-SOLD QOH LIST PRICE DESCRIPTION AGLECLAP PRIMARY VENDOR INVENTORY M & M Revenue Cycle REA Model SALESPERSON QUARTERLY SALES EMPLOYEE 1 (0, N) (1, 1) (1, 1) 1 N N M SALEOutside Party Kind of Services CUSTOMER # (0, N) AMOUNT-OFRECEIPT-APPLIEDTO-SALE SALESPERSON COMMISSION RATE FIDELITY BOND RATING N Salepayment (1, 1) 1 CUSTOMERNAME CUSTOMER (0, N) A/RAMOUNT (0, N) 1 (0, N) N 1 CR Outside Party EMPLOYEE-CATEGORY NUMBER-OFEMPLOYEES (0, 1) MEDICAL-PLAN N N CASH RECEIPT RECEIPT-DATE (1, 1) CASH-RECEIPTAMOUNT 1 CashInflow REMITTANCEADVICE # (0, N) ACCOUNTTYPE ACCOUNTBALANCE BANK BANKACCOUNT # CASH (1, 1) N CR Inside Party (0, N) 1 EMPLOYEE EMPLOYEE CATEGORY Relationship View 15 Example Tables (Incomplete Enterprise Database) from Dunn & McCarthy (2004) working paper Inventory-Sale Stockflow Sale Salesperson Inventory Sale Actual Sale# Amount Date Cust# SalesRep# Employee Quarterly Comm ItemID Number Quantity Price S-1 7,200 1 July C-1 E-12 Number Sales $ rate A-4 S-1 2 600 S-2 10,000 21 July C-2 E-10 E-12 .12 A-1 S-1 3 2,000 S-3 16,000 22 July C-5 E-10 E-10 .10 A-6 S-2 2 5,000 S-4 10,000 26 July C-2 E-10 E-99 .10 A-1 S-3 1 2,000 S-5 16,600 31 July C-5 E-10 E-78 0 .15 A-5 S-3 2 4,000 S-6 35,000 15 Aug C-3 E-10 A-3 S-3 6 1,000 S-7 23,000 21 Aug C-4 E-99 A-6 S-4 2 5,000 Customer Sale-CashRecDuality A-2 S-5 2 3,000 Customer# Name A/R Amt SP# Sale# RA# Applied A-4 S-5 2 300 C-1 Bill E-12 S-2 RA-1 1,666 A-6 S-5 2 5,000 C-2 Mick E-10 S-4 RA-2 10,000 A-2 S-6 10 3,500 C-3 Keith E-10 S-1 RA-3 7,200 A-6 S-7 2 7,000 C-4 Charlie E-99 S-3 RA-4 16,000 A-5 S-7 3 3,000 C-5 Ron E-10 S-5 RA-4 16,600 S-2 RA-5 1,666 Cash Account# BA-6 BA-7 BA-8 BA-9 Type Checking Checking Draft Checking Bank Boston5 Shawmut Shawmut MassNat Cash Receipt Balance Remittance Advice# Amount RA-1 1,666 10,000 75,000 RA-2 7,200 0 RA-3 RA-4 32,600 RA-5 1,666 Bank Account# BA-6 BA-7 BA-7 BA-7 BA-6 Date 25 July 26 July 15 Aug 15 Aug 25 Aug Customer Number C-2 C-2 C-1 C-5 C-2 Cashier Number E-39 E-39 E-39 E-39 E-39 16 SQL and Relational Algebra Inner Join vs. Outer Join • Find all details of all sales and the cash receipt number and amount applied of any cash receipts related to those sales • Data we need for this example is in 2 tables • Summary sales totals have been stored in sales table • If summary figure not stored, you would need also need the sale-inventory relationship table to calculate sales totals • Note: there have been no cash receipts related to sales transaction 6 & 7. Sale Sale# S-1 S-2 S-3 S-4 S-5 S-6 S-7 Amount Date 7,200 1 July 10,000 21 July 16,000 22 July 10,000 26 July 16,600 31 July 35,000 15 Aug 23,000 21 Aug Cust# SalesRep# C-1 E-12 C-2 E-10 C-5 E-10 C-2 E-10 C-5 E-10 C-3 E-10 C-4 E-99 Sale-CashRecDuality Sale# RA# Applied S-2 RA-1 1,666 S-4 RA-2 10,000 S-1 RA-3 7,200 S-3 RA-4 16,000 S-5 RA-4 16,600 S-2 RA-5 1,666 17 Relational Algebra Inner Join in QBE Details of all sales, related cash receipts 18 Relational Algebra Inner Join in QBE Details of all sales, related cash receipts 19 Default join is inner Join If you double click on the join you will see the join properties box. Item 1, the inner join is the default 20 Inner Join Results • Sales transactions 6 & 7 are not listed since there was no cash receipts associated with them. • Does this create a potential problem?? SELECT Sale.SaleNumber, Sale.Amount, Sale.Date, Sale.CustomerNumber, [SaleCashRecDuality].RemittanceAdviceNumber, [Sale-CashRecDuality].Applied FROM Sale INNER JOIN [Sale-CashRecDuality] ON Sale.SaleNumber = [SaleCashRecDuality].SaleNumber; 21 Relational Algebra Outer Join in QBE Details of all sales, related cash receipts Double-click on the join line 22 Relational Algebra Outer Join in QBE Details of all sales, related cash receipts Click on appropriate join type Click OK 23 Relational Algebra Outer Join in QBE Details of all sales, related cash receipts Note change in Join line 24 Relational Algebra Outer Join in QBE Details of all sales, related cash receipts Result 25 Joins Compared Outer Join Inner Join 26