Download Formulating Queries 1

Document related concepts

Relational algebra wikipedia , lookup

Microsoft Access wikipedia , lookup

SQL wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Database wikipedia , lookup

Ingres (database) wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Join (SQL) wikipedia , lookup

Versant Object Database wikipedia , lookup

Clusterpoint wikipedia , lookup

Relational model wikipedia , lookup

Database model wikipedia , lookup

Transcript
Part I
Open a Access Database
• Blank Database
• Database Wizard
• Existing Database
(last five databases
saved on the
computer appear in
the list box. If the
file you want does
not appear select
More files…)
The database window
• This window will
appear after
selecting the
database. Note it
has the 6
database objects
each on its own
tab.
Database Tables
• Used to store data
• holds field names,
field descriptions and
data for each field of
each record
• underlying structure
for data stored in a
database
Table Selection
•
•
•
•
•
Datasheet View
Design View
Table Wizard
Import Table
Link Table
Design View
Database Table
Columns in the Table Design
• Field Name
• Data Type - 10 types that are listed in a drop
down box by clicking the arrow button on
the column
• Description
Access Data Types
• Text - any combination of alphabetic and
numeric characters, such as names,
addresses, and telephone numbers, that are
not use in calculations (default)
• memo - long entries requiring multiple lines
of text, such as detailed descriptions and
performance notes
Access Data Types
• Number - numeric values, such as the
number of items or number of days worked
that might be used in calculations
• Date/Time - dates, such as date hired, and
times, such as 1:00
• Currency - monetary values, such as salary
Access Data Types
• AutoNumber - numbers assigned by Access
to uniquely identify each record; these
values cannot be changed, deleted, or edited
• Yes/No - Single-character entry fields that
are marked when the status of the field is
true (yes) or left blank when the status is
false (no)
Access Data Types
• OLE object - fields that may be linked to an
object, such as a picture or a document
• Hyperlink - fields linked to other objects,
Web pages, or documents that appear when
the field when the field is clicked.
• Lookup Wizard - fields that enable you to
access a value from a table or list of values
Filled in Table Design
Datasheet View
The Field Name(s)
There are no records for this Database (yet)
Records Added to Datasheet
Two Views
• Datasheet view - you see the field names as
column headings and records as row. View
multiple records on the screen.
• Form view - forms use the fields and data
that are stored in database tables to see one
record on-screen at a time.
Form View
Maintenance
• Change is inevitable. You will need add,
delete modify information in your db
• find, update, insert, replace, delete, sort, and
filter and query.
Find, Filter, Query
 Query language
 format
 examples
Query Language
• ask questions about the database
• find the records which match a certain
criteria
Find or Filter or Query
• Which one? Depends on preciseness.
• All display or filter only the record(s) that
match a specific criteria
• note that there is also a FIND command in
the word processing and spreadsheet tools.
Find
• usually used for error correction
• used in searching for a particular value in a
record. This command works the same way
as it did for the spreadsheet.
• you type in the dialog box
• Find what: the entry you want to find.
Depending on whether you unselected
“current field”, Find searches each field (in
the table) to look for a match.
To Find Records in a DB Table
• The binoculars on the toolbar
• also can do find/replace (Edit->Replace)
Find
• Find can a search criterion with text or
values anywhere in the table. This can be
inexact. For example, if you used the find
command to find an employee named
Brown, the records of employees who live
in the town Brown or on the street Brown
could also be displayed.
Filters
• A Filter is an exact match. In a filter,
Access only matches within a certain field.
So in our last example, you would tell
Access to find only NAMES (the field
name) that are equal to Brown. Thus the
other Brown matches are eliminated.
Filters
• To display (filter) only the record or records
that satisfy a criterion.
• Filter by selection is the simplest type of
filter.
• All you need to do is give Access an
example of the data you want by selecting
the data within the table, and then clicking
the Filter by Selection button on the Form
View toolbar (the sieve with the lighting
bolt)
Filter
• In order to have all the records once again
displayed, you remove the filter by clicking
the Remove Filter button on the form
toolbar. ( the sieve by itself icon)
Filter/Query
• Not saved
• only within a single
table
• displays all the fields
of “filtered” records
• no other functions
• Can be saved (it is one
of the 6 objects)
• multiple tables
• displays only the
fields you specified
• queries can also be
used to insert new
values, modify field
values and perform
calculations on field
values
Database Window
Queries Tab
Previously Saved queries
Three
options you
can do in
queries
section
Queries
• Click on tab marked Queries on the
Database Window
• There are three button on this window:
– Open - If a previously saved query is highlighted,
clicking this button will display the results of this
query.
– Design - If a previously saved query is highlighted,
clicking this button will display the design query
window filled in with the settings for this query.
– New - An empty design query window will be
displayed
Queries
• Note the table(s) used in the query display
at the top.
• If you clicked the Design button, the
table(s) used in this saved query will be
displayed.
• If you clicked the New button, you will be
given the opportunity of selecting the tables
through the Show Table.
Show Table
Tables
saved for
this
database
To add
tables to the
query,
highlight the
table name
and click
Add. When
finished
click Close
Query Design Window
Back to
Queries
slide
The Query Design Window
• A Window that isolates the important parts
of a query. (Design view)
• Two Important parts to the Window:
– Table(s) used in the query (top half)
– The Query Grid
...
Parts of the Query Design
Window
the table(s) used in the query
Customers
*
Example of a selected table
to use in a query, appears in
the top half of the Query
Design Window
Customer Id
First Name
Table name
Last Name
Address
Field Names
Parts of the Query Design
Window
the query grid
Drop down box of the Field Names
from the selected tables
Field:
Table:
Sort:
Show:
Criteria
Or:
Field
• Drop down box, depends on the tables you
selected to include either when the show
table dialog box appeared (New) or from
the original query (Design) . Once selected,
the fields from the selected tables will be
used in the query.
Table:
• You can select several tables for a query
operation.
• Once you have selected the Field , this entry
is automatically entered by Access. Access
will enter the table name of the field you
selected.
Sort:
• You have the option of sorting the matching
records
• Just left click on the box and a drop down
menu will appear,
• Ascending, Descending and none
• Can sort on more than one field, the first
field listed (the leftmost) is the major key.
• Remember only the query is sorted not the
original table(s)
Show:
• If you want this field data to appear in the
results
• An empty Show box indicates that the field
data will not appear in query results.
• A checked Show box indicates that the field
data will appear in query results.
• Objective: remove unnecessary or duplicate
fields
Criteria
• The final part, the criteria, (the comparison),
determines how the query will do its
comparisons.
• Using expressions (just like Excel),
combinations of field names, constant
values, arithmetic and logic operators, you
can restrict the number of records returned
by a query
Our Question
(Query)?
Who are our customers with the first name
of John? (list them alphabetically)
Customers
*
Customer Id
First Name
Last Name
Address
Select the Customers
database
This can be answered by a
completing a Query Design
Window.
Customers
Filled in Query Design Window
*
to Answer our Question
Customer Id
Display in alphabetical order the last names
of customers with the first name of John
First Name
Last Name
Address
Field:
First Name
Customers
Table:
Sort:
Show:
Criteria “John”
Or:
Last Name
Customers
Ascending
The Results
• A list will appear when the run command is
clicked, the ! Icon.
Last Name
Black
Braithwaite
Dewey
Dickenson
Dobbins
Duran
*
Only Last Name field
data displays
Also note that the
Johnaton(s) are not
listed
Must use wild cards *
“John*”
Access Automatic “FILL”
• When you entered in the string John, Access
will automatically place the quotes
• John
“John”
• If you entered the string John*, Access will
automatically change the field to
• John*
Like “John*”
• Or you can put these in yourself.
String Criteria
• We can “match” strings for text type fields
• We can use the wild cards * and ? for substitution
of characters
• * - 0 or more character substitution
• Like “John*”
– matches John, Johnaton, Johns, Johnstone
– but not Jon, Jjohn
• ? only one match and not anymore
• Like “285?5???”
– matches 285 5555, 285- 5555
– but not 765-285-5555, 1-285-5555, 285-51111
Our Next Question?
Who are our customers with the first name
of John or Susan? (list them alphabetically)
Customers
*
Our database
Customer Id
First Name
Last Name
Address
This can be answered by a
completing a Query Design
Window.
Customers
Design Query
*
Customer Id
Display in alphabetical order the last names
of customers with the first name of John or
Susan
First Name
Last Name
Address
Field:
First Name
Customers
Table:
Sort:
Show:
Criteria “John”
Or:
“Susan”
Last Name
Customers
Ascending
The Results
• A list will appear when the run command is
clicked, the ! Icon.
Last Name
Black
Braithwaite
Dewey
Dickenson
Dobbins
Duran
Doolittle
*
Only Last Name field
data displays
Also you note that the
Johnaton(s) and
Susanne(s) are not
listed.
Must use wild cards *
in strings “John*”
“Susan*”
OR
• connectors
• allows you to link comparisons to make it
possible for you to analyze data in various
ways
• logical operators, because they act in
comparisons the way mathematical
operators work in the Spreadsheet
Or Conjunction
• when one or more of the conditions needs to
be “true” we use the or conjunction
• color equal to green OR color equal to
yellow
• color equal to green OR part number equal
to 948
• one listed in the criteria row, the other in
the or row
Numeric Criteria - Logic
Operators
•
•
•
•
•
•
•
Greater than (>)
Less than (<)
equals (=)
greater than or equal to (>=)
less than or equal to (<=
not equal to (<>
(same as Excel If expressions)
Customers
Design Query
*
Table
name
Customer Id
First Name
Last Name
Address
Total Purchase
Age
Display in descending order the total purchase of
customers who are under 30 years of age
Field:
Table:
Sort:
Show:
Criteria
Or:
Total Purchase Last Name
Customers
Customers
Descending
Age
Customers
<30
The Results
• A list will appear when the run command is
clicked, the ! Icon.
*
Total Purchase Last Name
Age
$1900.50
Smith
24
$1410.26
Buy
21
$1167.88
Jones
26
$900.88
Smith
20
$469.34
Feather
14
$367.99
Johnson
29
The query is
sorted by the
amount of the
total purchase
Orders
Design Query
*
Table
name
Customer Id
Order #
Product ID #
Quantity
Display all orders which have not been paid
by oldest date
Field:
Unit Price
Table:
Date of Purchase Sort:
Date of Payment Show:
Criteria
Or:
Order #
Orders
Date of Purchase Date of Payment
Orders
Orders
Ascending
Is Null
Criteria
• Null - the absence of data - an empty field.
• If a field has no entry, it is called a null
value. This is not the same as a space or
zero!
• The computer treats dates as if they were
sequential numbers that increase with time
The Results
• A list will appear when the run command is
clicked, the ! Icon.
*
Order #
Date of Purchase Date of Payment
146678
3/10/2000
145778
3/12/2000
145807
3/15/2000
145900
3/19/2000
145910
3/21/2000
145950
3/24/2000
The query is
sorted by the date
of purchase
Note that the
Date of Payment
could have been
marked to not
display.
Orders
Design Query
*
Table
name
Customer Id
Order #
Display all orders which were paid this month
Product ID #
Quantity
Field:
Unit Price
Table:
Date of Purchase Sort:
Date of Payment Show:
Criteria
Or:
Order #
Orders
Date of Purchase Date of Payment
Orders
Orders
Ascending
>=4/1/2000
The Results
• A list will appear when the run command is
clicked, the ! Icon.
*
Order #
Date of Purchase Date of Payment
146578
3/02/2000
4/2/2000
145578
3/02/2000
4/2/2000
145607
3/05/2000
4/4/2000
145600
3/05/2000
4/5/2000
145710
3/06/2000
4/6/2000
145750
3/06/2000
4/6/2000
The query is
sorted by the date
of purchase
Note that the
Date of Payment
could have been
marked to not
display.
Access Automatic “FILL”
• When you entered in the string >4/1/2000,
Access will automatically place the # s
• >=4/1/2000
>=#4/1/2000#
• Or you can put these in yourself.
Simple Selection
Query
Customers
*
Customer Id
Display all of the customers with the name
John Smith
First Name
Last Name
Address
Field:
First Name
Customers
Table:
Sort:
Show:
Criteria “John”
Or:
Last Name
Customers
“Smith”
And Conjunction
• The AND conjunction says that both
conditions must be present for the record
(row) to be queried
• to combine criteria with AND, place the
criteria on the same line
Combining Multiple
Conjunctions
• when combining with all Ors - no problem
• when combining with all Ands - no problem
• when combining with both Ands and Ors order is important
Customers
Design Query
*
Customer Id
First Name
Display in alphabetical order the last names
of customers with the first name of John or
Susan that are older than 50 years of age.
Last Name
Age
Field:
First Name
Table:
Customers
Sort:
Show:
Criteria “John”
Or:
“Susan”
Last Name
Customers
Ascending
Age
Customers
>50
The Records in the Database
First Name
Last Name
Age
David
Done
33
Fred
Flinstone
55
Susan
Johnson
43
Susan
Peters
34
John
Peters
39
John
Smith
55
Susan
Smith
52
Result of Query
First Name
Last Name
Age
Susan
Johnson
43
Susan
Peters
34
John
Smith
55
Susan
Smith
52
Note that the records matched any record with the first name of
Susan and records that had the first name of John and were older
than 50
(FirstName = “John” AND Age > 50) OR FirstName = “Susan”
But what if wanted only those records with
first name of Susan who were age 50 or
older or records with the first name of John
who were age 50 or older … or
First Name
Last Name
Age
John
Smith
55
Susan
Smith
52
Note that the records matched any record with the first name of
Susan and records that had the first name of John and were older
than 50
(FirstName = “John” OR FirstName = “Susan”) AND age > 50
Customers
Design Query
*
Customer Id
First Name
Display in alphabetical order the last names
of customers with the first name of John or
Susan that are older than 50 years of age.
Last Name
Age
Field:
First Name
Table:
Customers
Sort:
Show:
Criteria “John”
Or:
“Susan”
Last Name
Customers
Ascending
Age
Customers
>50
>50
Result:
First Name
Last Name
Age
John
Smith
55
Susan
Smith
52
Note that the records matched any record with the first name of
Susan who was older than 50 years of age or records that had the
first name of John who were older than 50 years of age.
(FirstName = “John” and age > 50) OR
( FirstName = “Susan” and age > 50)
Three fields using AND and OR
Meaning
• Be careful of the wording when designing
queries.
• For example if I asked you to give me a list
of classes that are held at 9:00 A.M and
10:00 A.M, I am using the word AND to
mean both (not the connective AND)
• BOTH-> OR
Questions