Download presentation
Presentation is loading. Please wait.
Published byTyrone Merritt Modified over 9 years ago
1
Computer Science & Engineering 2111 Lecture 11 Querying a Database 1
2
What is a Database Query? 2 A request for information from a database To extract information from the Database you must use a Query which is a “question” or “request” – Criterion An expression that tells the DBMS which records to retrieve Make up of conditions – Can be one conditions or many When you run the query a dynaset, or subset of the database is displayed. You can make changes to this dynaset and the changes will be reflected in your database, because the dynaset is just a view of your database.
3
SQL is the language you use to talk to the database MS Access 2010 supplies a graphical user interface (GUI) called the Query By Example Grid or (QBE) grid MS Access 2010 creates the SQL for you. 3 Structured Query Language (SQL)
4
4 Query By Example (QBE) Grid SQL Created from QBE Grid Dynaset created when Query is Run
5
Create a list of clients by first and and last name 5
6
The resulting dynaset: What happens if we later change the last name of John Smith to Jones on the Client Table and re-run the query? 6
7
Write a query to list the First Name and Last Name and state for all clients who live in Ohio. Field FirstNameLastNameState Table Client Sort Show xxx Criteria “oh” OR 7 Query Name: Ohio Residents Tables Required: Client Foreign Keys: None Join Type: None When typing in any non-numeric criteria, always surround the criteria with quotes.
8
The data table The resulting dynaset 8
9
Wild cards in Criteria An asterisk * replaces any number of characters Used with the keyword, Like – 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 w ill select BE & BA 9
10
Write a query to list the First Name and Last Name for all clients who live within the 614 area code. Field FirstNameLastNameHomePhone Table Client Sort Show xx Criteria Like “614*” OR 10 Query Name: Phone#614 Tables Required: Client Foreign Keys: None Join Type: None
11
The resulting dynaset 11 The data table
12
Write a query to list the First Name and Last Name of all clients who DO NOT live within the 614 area code. Field FirstNameLastNameHomePhone Table Client Sort Show xx Criteria Not Like “614*” OR 12 Query Name: NotPhone#614 Tables Required: Client Foreign Keys: None Join Type: None
13
The resulting dynaset 13 The data table
14
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. 14
15
Write a query to list the First Name, Last Name, amount and payment date for all clients who paid $200 or more. Field FirstNameLastNameAmountPaymentDate Table Client Payments Sort Show xx x Criteria > 200 OR 15 Query Name:GreaterThanExampleTables Required:Client/Payments Foreign Keys:ClientIDJoin Type:Inner
16
The resulting dynaset Dynaset only includes records that have matching keys on both tables. This is called an inner join which is the default join type in Access 16 The data tables
17
For multiple conditions in the same field use Boolean Operators - AND, OR, NOT “BE or “BA” >5 AND <10 NOT “BE” 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 a criteria is on a separate line it is automatically considered an OR 17
18
Write a query to list the First Name, Last Name, and amount for all clients who paid $250 or more or made payments of less than $75. 18 Query Name: Example1Tables Required: Client/Payments Foreign Keys: ClientIDJoin Type:Inner Field FirstNameLastNameAmount Table Client Payments Sort Show xx Criteria >= 250 OR < 75 OR Query Name: Example2Tables Required: Client/Payments Foreign Keys: ClientIDJoin Type:Inner Field FirstNameLastNameAmount Table Client Payments Sort Show xx Criteria >= 250 or < 75 OR
19
The resulting dynaset Dynaset only includes records that have matching keys on both tables. This is called an inner join which is the default join type in Access 19 The data tables
20
Write a query to list the First Name, Last Name, amount and payment date for all clients who paid more than $100 on or after 3/3/2008. Field FirstNameLastNameAmountPaymentDate Table Client Payments Sort Show xxxx Criteria > 100>= #3/3/2008# OR 20 Query Name:AndExampleTables Required:Client/Payments Foreign Keys:ClientIDJoin Type:Inner
21
The resulting dynaset 21 The data tables (partial view)
22
Write a query to list the First Name and Last Name of all clients who made payments between 1/1/2008 and 3/8/2008. Field FirstNameLastNamePaymentDate Table Client Payments Sort Show xxx Criteria Between #1/1/2008# And #3/8/2008# OR 22 Query Name:BetweenExampleTables Required:Client/Payments Foreign Keys:ClientIDJoin Type:Inner
23
The resulting dynaset 23 The data tables
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.