Date post: | 27-Dec-2015 |
Category: |
Documents |
Upload: | audra-cooper |
View: | 215 times |
Download: | 0 times |
CS&E 1111 AcQueries
Writing Simple Queries in AccessWriting Simple Queries in Access
• Displaying on specific data fields
• Filtering data using criteria
Objectives: Learn how to use the Access Query Design Tool to obtain specific information by -
CS&E 1111 AcQueries
Querying : The process of asking the database for specific information
A specific format is used to ask these questions The computer saves the format, not the results which can be generated upon demand (dynaset)
We will study the following types of queries: Filtering data using single and multiple criteria Sorting data Grouping and summarizing data Performing calculations on the data
CS&E 1111 AcQueries
QueryQuery A query is a question about the data in the
database Writing queries involves
Choosing the required tables Where is your information coming from
Choosing the output fields What information do you want to list
Choosing the criteria fields What selection criteria do you want to specify -
which records to “choose”
CS&E 1111 AcQueries
Writing a CriterionWriting a Criterion
Criteria is a collection of criterion or conditions:
Criterion - an expression that tells the DBMS which records to retrieve
Criteria with 2 conditions:List records from category BE with more than 40
units in stock
CS&E 1111 AcQueries
Foreign Key
Primary Key
Products:
Suppliers:
Product NameCategory
IDSupplier
IDProduct
IDUnits In Stock
Unit Price Discontinued
Mini-cookies BA 1 1 25 0.85 FALSEGranola Gems CA 2 2 10 0.37 FALSEPotato Chips CH 5 3 50 0.42 FALSENuts, Cashews, Dry Roast NS 4 9 13 0.74 FALSEMixed Nuts, Large NS 2 10 7 0.70 FALSENuts, Almonds, Smoked NS 2 11 20 0.75 FALSECookie, Peanut Butter BA 1 18 35 0.45 FALSECookie, Oatmeal Raisin BA 1 19 33 0.45 FALSECookie, Chocolate BA 4 20 16 0.45 FALSEBrownies, 2-pack BA 4 23 29 0.72 FALSEJelly Bean Assortment CA 1 27 33 0.30 FALSEMint Drops CA 2 28 54 0.30 FALSEGum, Sugarless CA 4 29 29 0.40 FALSELemon Drops CA 4 30 25 0.35 FALSECider mix, dry BE 5 31 34 0.12 FALSECocoa mix, dry BE 5 32 56 0.15 FALSE
SupplierID SupplierName ContactName PhonePayment
Terms
1 Elegance Bakery & More Helen Phars 1-223-444-5555 Net 302 Jessie's Junk Bill Overholts 206-888-9999 Net 103 Larsen Nuts Larry Larsen 1-233-445-5665 Net 304 Nuts are Us Jeanne Moser 383-0333 Net 305 Crackle Snacks Eliott Marti 1-800-111-2222 Net 10
Consider the following database :
CS&E 1111 AcQueries
Create a list of products by name and the number of units in stock for each
FIELD ProductName UnitsInStockTABLE Products ProductsSORTSHOWCRITERIAOROR
No conditions
ProductsCategoryIDProductNameUnitsinstockSupplierIDProductIDPrice
CS&E 1111 AcQueries
The resulting dynaset:
The resulting dynaset:
Product NameUnits In Stock
Mini-cookies 25Granola Gems 10Potato Chips 50Nuts, Cashews, Dry Roast 13Mixed Nuts, Large 7Nuts, Almonds, Smoked 20Cookie, Peanut Butter 35Cookie, Oatmeal Raisin 33Cookie, Chocolate 16Brownies, 2-pack 29Jelly Bean Assortment 33Mint Drops 54Gum, Sugarless 29Lemon Drops 25Cider mix, dry 34Cocoa mix, dry 56
What happens if we later change the units in stock for Mini-cookies on the Products Table an re-run the query?
CS&E 1111 AcQueries
List the products & units in stock for category BE
FIELD CategoryID ProductName UnitsInStockTABLE Products Products ProductsSORTSHOWCRITERIA "BE"OROR
Single condition criterion showing ProductName & Units
ProductsCategoryIDProductNameUnitsinstockSupplierIDProductIDPrice
CS&E 1111 AcQueries
The data table:The data table:
Product Name Units In StockCider mix, dry 34Cocoa mix, dry 56
The resulting dynaset:
Product NameCategory
IDSupplier
IDProduct
IDUnits In Stock
Unit Price Discontinued
Mini-cookies BA 1 1 25 0.85 FALSEGranola Gems CA 2 2 10 0.37 FALSEPotato Chips CH 5 3 50 0.42 FALSENuts, Cashews, Dry Roast NS 4 9 13 0.74 FALSEMixed Nuts, Large NS 2 10 7 0.70 FALSENuts, Almonds, Smoked NS 2 11 20 0.75 FALSECookie, Peanut Butter BA 1 18 35 0.45 FALSECookie, Oatmeal Raisin BA 1 19 33 0.45 FALSECookie, Chocolate BA 4 20 16 0.45 FALSEBrownies, 2-pack BA 4 23 29 0.72 FALSEJelly Bean Assortment CA 1 27 33 0.30 FALSEMint Drops CA 2 28 54 0.30 FALSEGum, Sugarless CA 4 29 29 0.40 FALSELemon Drops CA 4 30 25 0.35 FALSECider mix, dry BE 5 31 34 0.12 FALSECocoa mix, dry BE 5 32 56 0.15 FALSE
CS&E 1111 AcQueries
Relational operators in a query:
= , <= , >= , <> , < , > With Numbers
<= 10 values of less than or equal to 10 With Text
< “G” text beginning with letters A through F
> “Jones” text from Jones through end
The appropriate expression is placed in the field where this data if found.
CS&E 1111 AcQueries
List the product names for products with greater than 40 units in stock
FIELD ProductName UnitsInStockTABLE Products ProductsSORTSHOWCRITERIA >40OROR
You can use relational operators in your criteria
ProductsCategoryIDProductNameUnitsinstockSupplierIDProductIDPrice
CS&E 1111 AcQueries
Product NameCategory
IDSupplier
IDProduct
IDUnits In Stock
Unit Price Discontinued
Mini-cookies BA 1 1 25 0.85 FALSEGranola Gems CA 2 2 10 0.37 FALSEPotato Chips CH 5 3 50 0.42 FALSENuts, Cashews, Dry Roast NS 4 9 13 0.74 FALSEMixed Nuts, Large NS 2 10 7 0.70 FALSENuts, Almonds, Smoked NS 2 11 20 0.75 FALSECookie, Peanut Butter BA 1 18 35 0.45 FALSECookie, Oatmeal Raisin BA 1 19 33 0.45 FALSECookie, Chocolate BA 4 20 16 0.45 FALSEBrownies, 2-pack BA 4 23 29 0.72 FALSEJelly Bean Assortment CA 1 27 33 0.30 FALSEMint Drops CA 2 28 54 0.30 FALSEGum, Sugarless CA 4 29 29 0.40 FALSELemon Drops CA 4 30 25 0.35 FALSECider mix, dry BE 5 31 34 0.12 FALSECocoa mix, dry BE 5 32 56 0.15 FALSE
The data table:
The resulting dynaset:Product Name
Potato ChipsMint DropsCocoa mix, dry
CS&E 1111 AcQueries
List the product names and units in stock for products from the letters M to Z
FIELD ProductName UnitsInStockTABLE Products ProductsSORTSHOWCRITERIA >="m"OROR
You can use relational operators in your criteria
ProductsCategoryIDProductNameUnitsinstockSupplierIDProductIDPrice
CS&E 1111 AcQueries
Wild cards in Criteria: An asterisk * replaces any number of characters:
Like “C*” – in Product Name field will select c, Cookie, cake.
Like “*cookie*” - in Product Name field will select all records that include the word cookie in the Product Name field
A ? replaces a single character Like “B?” – in the Category field will select BE & BA
Would the criteria Like “432??” in the zipcode field select a record with the zipcode 43219-7888?
CS&E 1111 AcQueries
List the product names for products that start with the letter M
FIELD ProductNameTABLE ProductsSORTSHOW
CRITERIA Like "m*"OROR
You can use wild cards in your criteria - just need to type m* the computer will add the syntax Like “...”
ProductsCategoryIDProductNameUnitsinstockSupplierIDProductIDPrice
CS&E 1111 AcQueries
For multiple conditions in the same field use Boolean Operators - AND, OR, NOT
“BE” Or “BA” >5 AND <10
NOT “BE”
What dynaset would result from the criteria “BE” and “BA” in the category field?
No records would be selected since you cannot specify 2 mutually exclusive conditions with an And.
CS&E 1111 AcQueries
List by product the units in stock for both categories BE and BA
FIELD CategoryID ProductName UnitsInStockTABLE Products Products ProductsSORTSHOWCRITERIA "BE" Or "BA"OROR
The “OR” operator for a single field’s conditions
ProductsCategoryIDProductNameUnitsinstockSupplierIDProductIDPrice
CS&E 1111 AcQueries
The data table:
The resulting dynaset:
Product NameCategory
IDSupplier
IDProduct
IDUnits In Stock
Unit Price Discontinued
Mini-cookies BA 1 1 25 0.85 FALSEGranola Gems CA 2 2 10 0.37 FALSEPotato Chips CH 5 3 50 0.42 FALSENuts, Cashews, Dry Roast NS 4 9 13 0.74 FALSEMixed Nuts, Large NS 2 10 7 0.70 FALSENuts, Almonds, Smoked NS 2 11 20 0.75 FALSECookie, Peanut Butter BA 1 18 35 0.45 FALSECookie, Oatmeal Raisin BA 1 19 33 0.45 FALSECookie, Chocolate BA 4 20 16 0.45 FALSEBrownies, 2-pack BA 4 23 29 0.72 FALSEJelly Bean Assortment CA 1 27 33 0.30 FALSEMint Drops CA 2 28 54 0.30 FALSEGum, Sugarless CA 4 29 29 0.40 FALSELemon Drops CA 4 30 25 0.35 FALSECider mix, dry BE 5 31 34 0.12 FALSECocoa mix, dry BE 5 32 56 0.15 FALSE
Product Name Units In StockMini-cookies 25Cookie, Peanut Butter 35Cookie, Oatmeal Raisin 33Cookie, Chocolate 16Brownies, 2-pack 29Cider mix, dry 34Cocoa mix, dry 56
CS&E 1111 AcQueries
List by product the units in stock for all products but those in category BE
FIELD CategoryID ProductName UnitsInStockTABLE Products Products ProductsSORTSHOWCRITERIA Not "BE"OROR
You can use the “not” operator for a single field’s conditions
ProductsCategoryIDProductNameUnitsinstockSupplierIDProductIDPrice
CS&E 1111 AcQueries
List the products & units in stock for all items with at least 20 units in stock but no more than 30 units in stock
FIELD ProductName UnitsInStockTABLE Products ProductsSORTSHOWCRITERIA >=20 And <=30OROR
You can use the “AND” operator for a single field’s conditions
ProductsCategoryIDProductNameUnitsinstockSupplierIDProductIDPrice
CS&E 1111 AcQueries
FIELD ProductName UnitsInStockTABLE Products ProductsSORTSHOW
CRITERIA between 20 and 30OROR
The between syntax allows you to set lower & upper limits - inclusively
ProductsCategoryIDProductNameUnitsinstockSupplierIDProductIDPriceAn alternate syntax:
>=20 and <=30
List the products & units in stock for all items with at least 20 units in stock but no more than 30 units in stock
CS&E 1111 AcQueries
For conditions in multiple fields the placement of your arguments determines the Boolean relationship between those arguments:
If a criteria is on the same line it is automatically considered an AND
If criteria is on a separate line it is automatically considered an OR
CS&E 1111 AcQueries
List product names for products in category BA with less than 30 units in stock
FIELD CategoryID ProductName UnitsInStockTABLE Products Products ProductsSORTSHOWCRITERIA "BA" <30OROR
ProductsCategoryIDProductNameUnitsinstockSupplierIDProductIDPrice
The placement of both criteria on the same line implies an AND relationship
CS&E 1111 AcQueries
List product names for products in category BA with less than 30 units in stock
FIELD CategoryID ProductName UnitsInStockTABLE Products Products ProductsSORTSHOWCRITERIA "BA"OR <30OR
ProductsCategoryIDProductNameUnitsinstockSupplierIDProductIDPrice
The placement of both criteria on the same line implies an AND relationship
CS&E 1111 AcQueries
The data table:
The resulting dynaset:Product Name
Mini-cookiesCookie, ChocolateBrownies, 2-pack
Product NameCategory
IDSupplier
IDProduct
IDUnits In Stock
Unit Price Discontinued
Mini-cookies BA 1 1 25 0.85 FALSEGranola Gems CA 2 2 10 0.37 FALSEPotato Chips CH 5 3 50 0.42 FALSENuts, Cashews, Dry Roast NS 4 9 13 0.74 FALSEMixed Nuts, Large NS 2 10 7 0.70 FALSENuts, Almonds, Smoked NS 2 11 20 0.75 FALSECookie, Peanut Butter BA 1 18 35 0.45 FALSECookie, Oatmeal Raisin BA 1 19 33 0.45 FALSECookie, Chocolate BA 4 20 16 0.45 FALSEBrownies, 2-pack BA 4 23 29 0.72 FALSEJelly Bean Assortment CA 1 27 33 0.30 FALSEMint Drops CA 2 28 54 0.30 FALSEGum, Sugarless CA 4 29 29 0.40 FALSELemon Drops CA 4 30 25 0.35 FALSECider mix, dry BE 5 31 34 0.12 FALSECocoa mix, dry BE 5 32 56 0.15 FALSE
CS&E 1111 AcQueries
List product names for products in category BA with less than 30 units in stock or category BE with more than 31 units in stock
FIELD CategoryID ProductName UnitsInStockTABLE Products Products ProductsSORTSHOWCRITERIA "BA" <30OR "BE" >31OR
ProductsCategoryIDProductNameUnitsinstockSupplierIDProductIDPrice
Criterion on same line implies AND, Criterion on a separate line implies OR
CS&E 1111 AcQueries
How does the computer store your query?How does the computer store your query?
Access does not store the resulting table from your query - it simply stores the instructions to run the query The actual SQL instructions can be seen in the
VIEW menu This is “memory efficient” since it does not
require you to save all the information each time you want a different “view” of it.
It also insures that each time you use a query “view” you get the most current version.
CS&E 1111 AcQueries
Summarizing QueriesSummarizing Queries
A query is a set of instructions that specifies how to extract and organize data from a database
Access provides the QBE grid as a GUI interface to create the SQL code.
To properly use the QBE grid you must select the required data (table(s), query), the fields you wish to display and then specify the desired criterion, sorts, groupings and/or expressions.