Presentation is loading. Please wait.

Presentation is loading. Please wait.

Working with Queries in Microsoft Access The Access Workbench: Section Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 3 rd Edition.

Similar presentations


Presentation on theme: "Working with Queries in Microsoft Access The Access Workbench: Section Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 3 rd Edition."— Presentation transcript:

1 Working with Queries in Microsoft Access The Access Workbench: Section Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 3 rd Edition

2 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-2 Section Objectives  Learn how to use Microsoft Access to:  Run Access SQL statements  Run queries in single and multiple tables in using both SQL and Query By Example (QBE)  Manually set table and relationship properties not supported by Access SQL

3 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-3 Using Access QBE: The SQL Query to be Run SELECT* FROM CUSTOMER;

4 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-4 Using Access SQL: The Queries Pane Double-click Create query in Design view to create a new query The Queries Pane Click Queries to display the Queries Pane

5 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-5 Using Access SQL: The Show Table Dialog Box Show Table Dialog Box Query1: Select Query window

6 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-6 Using Access SQL: The Query1: Select Query Window in Design View SQL View button Query1: Select Query window in Design View

7 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-7 Using Access SQL: The Query1: Select Query Window in SQL View Query1: Select Query window in SQL View Design View button

8 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-8 Using Access SQL: The SQL Query The SQL query text The Execute Query button The Save Query button

9 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-9 Using Access SQL: The Query Results

10 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-10 Using Access SQL: The Save As Dialog Box The Save Query button The Save As dialog box Type new query name here

11 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-11 Using Access SQL: The SQLQuery-AW-3-01: Select Query Window The query has been renamed as SQLQuery-AW-3-01

12 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-12 Using Access SQL: The SQLQuery-AW-3-01 Object in the Queries Pane The SQLQuery-AW-3-01 query object

13 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-13 Using Access QBE: The SQL Query to be Run SELECT* FROM CUSTOMER;

14 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-14 Using Access QBE: The Show Table Dialog Box Show Table Dialog Box Query1: Select Query window

15 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-15 Using Access QBE: The QBE Query1 : Select Query Window Tables in the query appear in the top pane, together with a list of their columns (the “field list”) and an asterisk (*) meaning “all columns” Columns in the query are called fields and appear in the bottom pane, together with related property values

16 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-16 Using Access QBE: Adding Columns to the Query To add a column to the query, click the column name and drag it to a cell in the Field: row in the lower window pane The asterisk (*) symbol was dragged and dropped here to add the CUSTOMER.* field to the query The table name is automatically added to the query to specify the source of the column–this is important if there is more than one table in the query with the same column name

17 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-17 Using Access QBE: The Query Results The results are sorted by CustomerID As expected, the query results are identical to those shown in the SQL query

18 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-18 Using Access QBE: The SQL Query to be Run SELECT CustomerID, LastName, FirstName FROM CUSTOMER;

19 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-19 Using Access QBE: Selecting Individual Fields The CustomerID, LastName, and FirstName fields are in the query The results will be sorted by LastName in descending order (Z–A) The results will show only customers with a CustomerID greater than 2

20 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-20 Using Access QBE: The SQL Query to be Run SELECT LastName, FirstName, Date, Type, Remarks FROM CUSTOMER, CONTACT WHERE CUSTOMER.CustomerID = CONTACT.CustomerID AND CustomerID = 3 ORDER BY Date;

21 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-21 Using Access QBE: The Query Window with the Two Tables Both the CONTACT table and CUSTOMER table are in the query The relationship and referential integrity constrain between the two tables is automatically included in the query—this means that the SQL clause WHERE CUSTOMER.CustomerID = CONTACT.CustomerID is automatically part of the query

22 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-22 Using Access QBE: The Complete Two Table Query From CUSTOMER, the CustomerID, LastName, and FirstName columns are in the query From CONTACT, the Date, Type, and Remarks columns are in the query The results will be sorted by Date in ascending order (A–Z) The results will be shown only for the customer with a CustomerID of 3

23 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-23 Using Access QBE: The Two Table Query Results The results are shown for the customer with the CustomerID of 3, which is Jessica Christman The results are sorted by Date in ascending order (earliest date to latest date)

24 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-24 Using Access QBE: The SQL Query to be Run SELECT LastName, FirstName, Date, Type, Remarks FROM CUSTOMER, CONTACT WHERE CUSTOMER.CustomerID = CONTACT.CustomerID AND CustomerID = [?] ORDER BY Date; The query now contains an unknown value for CustomerID—we will use a query with a prompt that will be displayed to allow the user to enter a parameter value when the query is run

25 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-25 Using Access QBE: Using a Parameterized Query The Criteria for the CustomerID column now contains the text for a prompt to be displayed in the Enter Parameter Value dialog box that will be displayed to get a parameter value from the user The CustomerID column is now shown last because it is not displayed in the query results

26 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-26 Using Access QBE: The Enter Parameter Value Dialog Box Enter the CustomerID number here Click the OK button to run the query This is the text that was entered into the criteria field for CustomerID

27 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-27 Using Access QBE: The Two Table Query Results The user input the parameter value of 3, so the results are shown for the customer with the CustomerID of 3, which is Jessica Christman

28 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-28 SALESPERSON Table Column Characteristics Column NameTypeKeyRequiredRemarks NickNameText (35)Primary KeyYes LastNameText (25)NoYes FirstNameText (25)NoYes HireDateDate/TimeNoYes WageRateNumberNoYesDouble, Default value = $12.50 CommissionRateNumberNoYesDouble PhoneText (12)NoYes EmailText (100)NoYesUnique SALESPERSON

29 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-29 SALESPERSON Data NickNameLastNameFirstNameHireDateWageRate Commission Rate TinaSmithTina10-AUG-0415.5012.5% Big BillJonesWilliam25-SEP-0415.5012.5% BillyJonesBill17-MAY-0512.5012.0% NickNamePhoneEmail Tina206-287-7010Tina@WM.com Big Bill206-287-7020BigBill@WM.com Billy206-287-7030Billy@WM.com

30 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-30 SQL to Create the SALESPERSON Table CREATE TABLE SALESPERSON( NickNameChar(35)NOT NULL, LastNameChar(25)NOT NULL, FirstNameChar(25)NOT NULL, HireDateDateTimeNOT NULL, WageRateNumeric(5,2)NOT NULL DEFAULT(12.50), CommissionRateNumeric(5,3)NOT NULL, PhoneChar(12)NOT NULL, EmailVarchar(100)NOT NULL UNIQUE, CONSTRAINTSALESPERSON_PK PRIMARY KEY(NickName) );

31 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-31 SQL to Create the SALESPERSON Table Modified for ACCESS ANSI-89 SQL CREATE TABLE SALESPERSON( NickNameChar(35)NOT NULL, LastNameChar(25)NOT NULL, FirstNameChar(25)NOT NULL, HireDateDateTimeNOT NULL, WageRateNumericNOT NULL, CommissionRateNumericNOT NULL, PhoneChar(12)NOT NULL, EmailVarchar(100)NOT NULL, CONSTRAINTSALESPERSON_PK PRIMARY KEY(NickName) );

32 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-32 The CREATE TABLE SALESPERSON SQL Statement

33 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-33 The Create-Table SALESPERSON Query in the Queries Pane The Create-Table-SALESPERSON query–note the Design icon that identifies this as a data definition query Click the Tables button to move to the Tables pane

34 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-34 The SALESPERSON Table in the Tables Pane The SALESPERSON table object–click the object to select it After selecting a table, click the Design View button to open the table in Design View

35 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-35 The SALESPERSON Table in Design View Move the mouse cursor here, then click to select the WageRate column (field) The General tab in the Field Properties section

36 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-36 The WageRate Field Properties The Decimal Places text box The Default Value text box The WageRate column is selected The Format text box

37 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-37 The Format Text Box Click in the Format text box to select it Click the Format text box drop-down arrow to display the drop-down list Select Currency

38 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-38 The Default Value Text Box The Property Update Options icon— you can simply ignore it The Expression Builder icon—we will not use it in this situation Click in the Default Value text box to select it

39 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-39 The Completed WageRate Field Properties Click the Save button to save the change to the SALESPERSON table design Data format is set to Currency Number of decimal places is set to 2 The default value is set to 12.50

40 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-40 The Email Field Properties The Email column is selected The Indexed text box Select Yes (No Duplicates)

41 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-41 Specifying a Validation Rule Enter the possible values for the column in the Validation Rule text box

42 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-42 SQL to Insert Data into the SALESPERSON Table INSERT INTO SALESPERSON VALUES('Tina', 'Smith', 'Tina', '10-AUG-04', 15.50,.125, '206-287-7010', 'Tina@WM.com');Tina@WM.com INSERT INTO SALESPERSON VALUES('Big Bill', 'Jones', 'William', '25-SEP-04', 15.50,.125, '206-287-7020', 'BigBill@WM.com');BigBill@WM.com INSERT INTO SALESPERSON VALUES('Billy', 'Jones', 'Bill', '17-MAY-05', 12.50,.120, '206-287-7030', 'Billy@WM.com');Billy@WM.com

43 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-43 Inserting Data Into The SALESPERSON Table The INSERT command The dialog box confirming the INSERT Click the Yes button to complete the INSERT

44 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-44 The Data in the SALESPERSON Table The data is sorted by NickName (the primary key value) in ascending order

45 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-45 CUSTOMER.NickName Column Characteristics Column NameTypeKeyRequiredRemarks NickNameText (35)Foreign KeyYes CUSTOMER

46 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-46 CUSTOMER.NickName Data CustomerIDLastNameFirstName...NickName 1GriffeyBen...Big Bill 3ChristmanJessica...Billy 4ChristmanRob...Tina 5HayesJudy...Tina

47 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-47 SQL to Alter the CUSTOMER Table ALTER TABLE CUSTOMER ADD NickName Char(35) NULL; UPDATE CUSTOMER SETNickName = 'Big Bill' WHERECustomerID = 1; UPDATE CUSTOMER SETNickName = 'Billy' WHERECustomerID = 3; UPDATE CUSTOMER SETNickName = 'Tina' WHERECustomerID = 4; UPDATE CUSTOMER SETNickName = 'Tina' WHERECustomerID = 5; ALTER TABLE CUSTOMER ALTER COLUMN NickName Char(35) NOT NULL;

48 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-48 The Altered CUSTOMER Table The added NickName column The column is required, which is the Access equivalent of NOT NULL

49 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-49 The Relationships Window with the Current Relationship Diagram This is the relationship diagram as we created it in Section 2’s “The Access Workbench” The Show Table button

50 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-50 The Updated Relationship Diagram The relationship between SALESPERSON and CUSTOMER already exists—it was created using SQL—so right-click the relationship line and then click Edit Relationship in the shortcut menu that appears to display the Edit Relationships dialog box

51 KROENKE and AUER - DATABASE CONCEPTS (3 rd Edition) © 2008 Pearson Prentice Hall AW-3-51 The Completed Edit Relationships Dialog Box The Enforce Referential Integrity check box is already selected because of the SQL CONSTRAINT FOREIGN KEY statement Check the Cascade Update Related Fields check box to set the equivalent of the SQL ON UPDATE CASCADE statement Leave the Cascade Delete Related Fields check box unchecked—this is equivalent to the SQL ON DELETE NO ACTION statement

52 Working with Queries in Microsoft Access End of Presentation on The Access Workbench: Section Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 3 rd Edition


Download ppt "Working with Queries in Microsoft Access The Access Workbench: Section Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 3 rd Edition."

Similar presentations


Ads by Google