computer science & engineering 2111

23
Computer Science & Engineering 2111 Lecture 11 Querying a Database 1

Upload: dillon-arnold

Post on 02-Jan-2016

26 views

Category:

Documents


1 download

DESCRIPTION

Computer Science & Engineering 2111. Lecture 11 Querying a Database. What is a Database Query?. 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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Computer Science & Engineering 2111

1

Computer Science & Engineering 2111

Computer Science & Engineering 2111

Lecture 11Querying a Database

Page 2: Computer Science & Engineering 2111

2

What is a Database Query?• 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.

Page 3: Computer Science & Engineering 2111

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.

Structured Query Language (SQL)

Page 4: Computer Science & Engineering 2111

4

Query By Example (QBE) Grid

SQL Created from QBE Grid

Dynaset c

reated

when Query

is Run

Page 5: Computer Science & Engineering 2111

Create a list of clients by first and and last name

FIELD FirstName LastNameTABLE Client ClientSORTSHOWCRITERIAOROR

5

Page 6: Computer Science & Engineering 2111

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

Page 7: Computer Science & Engineering 2111

7

Write a query to list the First Name and Last Name and state for all clients who live in Ohio.

Field FirstName LastName State

Table Client Client Client

Sort

Show x x x

Criteria “oh”

OR

OR

Query Name: Ohio Residents Tables Required: ClientForeign Keys: None Join Type: None

When typing in any non-numeric criteria, always surround the criteria with quotes.

Page 8: Computer Science & Engineering 2111

The data table

The resulting dynaset

8

Page 9: Computer Science & Engineering 2111

Wild cards in CriteriaAn 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 will select BE & BA

9

Page 10: Computer Science & Engineering 2111

10

Write a query to list the First Name and Last Name for all clients who live within the 614 area code.

Field FirstName LastName HomePhone

Table Client Client Client

Sort

Show x x

Criteria Like “614*”

OR

OR

Query Name: Phone#614 Tables Required: ClientForeign Keys: None Join Type: None

Page 11: Computer Science & Engineering 2111

The resulting dynaset

11

The data table

Page 12: Computer Science & Engineering 2111

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 FirstName LastName HomePhone

Table Client Client Client

Sort

Show x x

Criteria Not Like “614*”

OR

OR

Query Name: NotPhone#614 Tables Required: ClientForeign Keys: None Join Type: None

Page 13: Computer Science & Engineering 2111

The resulting dynaset

13

The data table

Page 14: Computer Science & Engineering 2111

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

Page 15: Computer Science & Engineering 2111

15

Write a query to list the First Name, Last Name, amount and payment date for all clients who paid $200 or more.

Field FirstName LastName Amount PaymentDate

Table Client Client Payments Payments

Sort

Show x x x

Criteria > 200

OR

OR

Query Name: GreaterThanExample Tables Required: Client/PaymentsForeign Keys: ClientID Join Type: Inner

Page 16: Computer Science & Engineering 2111

The resulting dynasetDynaset 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

Page 17: Computer Science & Engineering 2111

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

Page 18: Computer Science & Engineering 2111

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.

Query Name:

Example1 TablesRequired:

Client/Payments

Foreign Keys:

ClientID Join Type: Inner

Field FirstName LastName Amount

Table Client Client Payments

Sort

Show x x

Criteria >= 250

OR < 75

OR

Query Name:

Example2 TablesRequired:

Client/Payments

Foreign Keys:

ClientID Join Type: Inner

Field FirstName LastName Amount

Table Client Client Payments

Sort

Show x x

Criteria >= 250 or < 75

OR

OR

Page 19: Computer Science & Engineering 2111

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

Page 20: Computer Science & Engineering 2111

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 FirstName LastName Amount PaymentDate

Table Client Client Payments Payments

Sort

Show x x x x

Criteria > 100 >= #3/3/2008#

OR

OR

Query Name: AndExample Tables Required: Client/PaymentsForeign Keys: ClientID Join Type: Inner

Page 21: Computer Science & Engineering 2111

The resulting dynaset

21

The data tables(partial view)

Page 22: Computer Science & Engineering 2111

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 FirstName LastName PaymentDate

Table Client Client Payments

Sort

Show x x x

Criteria Between #1/1/2008# And #3/8/2008#

OR

OR

Query Name: BetweenExample Tables Required: Client/PaymentsForeign Keys: ClientID Join Type: Inner

Page 23: Computer Science & Engineering 2111

The resulting dynaset

23

The data tables