Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science & Engineering 2111 Querying a Database 1CSE 2111 Lecture- Querying a Database.

Similar presentations


Presentation on theme: "Computer Science & Engineering 2111 Querying a Database 1CSE 2111 Lecture- Querying a Database."— Presentation transcript:

1 Computer Science & Engineering 2111 Querying a Database 1CSE 2111 Lecture- Querying a Database

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. CSE 2111 Lecture- Querying a Database

3 3 Query By Example (QBE) Grid CSE 2111 Lecture- Querying a Database Dynaset created when Query is run

4 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. 4 Structured Query Language (SQL) CSE 2111 Lecture- Querying a Database

5 5 SQL Created from QBE Grid CSE 2111 Lecture- Querying a Database

6 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 6 Query Name: Ohio Residents Tables Required: Client Foreign Keys: None Join Type: None When typing in non-numeric criteria, always surround the criteria with quotes. CSE 2111 Lecture-Basic Criteria in Queries

7 The data table The resulting dynaset CSE 2111 Lecture-Basic Criteria in Queries7

8 Write a query to list the ClientID for all payments of $100. Field ClientIDAmount Table Payments Sort Show x Criteria 100 OR 8 Query Name: PaymentOf100 Tables Required: Payments Foreign Keys: None Join Type: None When typing in numeric criteria, DO NOT surround the criteria with quotes. CSE 2111 Lecture-Basic Criteria in Queries

9 The data table The resulting dynaset CSE 2111 Lecture-Basic Criteria in Queries9

10 Write a query to list the Client ID, payment, and payment date for all payments made on March 8, 2008. Field ClientIDAmountPaymentDate Table Payments Sort Show xxx Criteria #3/8/2008# OR 10 Query Name: March8 Tables Required: Payments Foreign Keys: None Join Type: None When typing in date criteria, always surround the criteria with #. CSE 2111 Lecture-Basic Criteria in Queries

11 The data table The resulting dynaset CSE 2111 Lecture-Basic Criteria in Queries11

12 Write a query to list the ClientID and payment for all payments of $100 or more. Field ClientIDAmount Table Payments Sort Show xx Criteria >=100 OR 12 Query Name: Payment>=100 Tables Required: Payments Foreign Keys: None Join Type: None When typing in numeric criteria, DO NOT surround the criteria with quotes. CSE 2111 Lecture-Basic Criteria in Queries

13 The data table The resulting dynaset CSE 2111 Lecture-Basic Criteria in Queries13

14 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 will select all records that c onsists of two characters and start with the letter “B” CSE 2111 Lecture-Wild Cards and Key Words in Queries 14

15 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 15 Query Name: Phone#614 Tables Required: Client Foreign Keys: None Join Type: None CSE 2111 Lecture-Wild Cards and Key Words in Queries

16 The resulting dynaset CSE 2111 Lecture-Wild Cards and Key Words in Queries 16 The data table

17 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 17 Query Name: NotPhone#614 Tables Required: Client Foreign Keys: None Join Type: None CSE 2111 Lecture-Wild Cards and Key Words in Queries

18 The resulting dynaset CSE 2111 Lecture-Wild Cards and Key Words in Queries 18 The data table

19 Write a query to list the Client ID of all clients whose Client ID consists of 5 characters beginning with J75 and ending with 0. Field ClientID Table Clients Sort Show X Criteria Like “j75?0” OR 19 Query Name: J75?0 Tables Required: Client Foreign Keys: None Join Type: None CSE 2111 Lecture-Wild Cards and Key Words in Queries

20 The resulting dynaset CSE 2111 Lecture-Wild Cards and Key Words in Queries 20 The data table

21 Field ClientIDLastNameAmount Table Client Payments Sort Show xxx Criteria OR 21 Query Name:InnerJoinExampleTables Required:Client/Payments Foreign Keys:ClientIDJoin Type:Inner CSE 2111 Lecture-Inner Joins in Queries

22 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 CSE 2111 Lecture-Inner Joins in Queries22 The data tables

23 Multiple conditions in a Query Boolean Operators - AND, OR 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 CSE 2111 Lecture-Multiple and Compound Criteria in Queries 23

24 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. 24 Query Name: Example1Tables Required: Client/Payments Foreign Keys: ClientIDJoin Type:Inner Field FirstNameLastNameAmount Table Client Payments Sort Show xxx Criteria >= 250 OR < 75 OR Query Name: Example2Tables Required: Client/Payments Foreign Keys: ClientIDJoin Type:Inner Field FirstNameLastNameAmount Table Client Payments Sort Show xxx Criteria >= 250 or < 75 OR CSE 2111 Lecture-Multiple and Compound Criteria in Queries

25 The resulting dynaset 25 CSE 2111 Lecture-Multiple and Compound Criteria in Queries The data tables

26 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 26 Query Name:AndExampleTables Required:Client/Payments Foreign Keys:ClientIDJoin Type:Inner CSE 2111 Lecture-Multiple and Compound Criteria in Queries

27 The resulting dynaset CSE 2111 Lecture-Multiple and Compound Criteria in Queries 27 The data tables

28 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 28 Query Name:BetweenExampleTables Required:Client/Payments Foreign Keys:ClientIDJoin Type:Inner CSE 2111 Lecture-Multiple and Compound Criteria in Queries

29 The resulting dynaset CSE 2111 Lecture-Multiple and Compound Criteria in Queries 29 The data tables

30 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 or made a payment of $100. Field FirstNameLastNameAmountPaymentDate Table Client Payments Sort Show xxxx Criteria > 100>= #3/3/2008# OR 100 OR 30 Query Name:CompoundExampleTables Required:Client/Payments Foreign Keys:ClientIDJoin Type:Inner CSE 2111 Lecture-Multiple and Compound Criteria in Queries

31 The resulting dynaset CSE 2111 Lecture-Multiple and Compound Criteria in Queries 31 The data tables


Download ppt "Computer Science & Engineering 2111 Querying a Database 1CSE 2111 Lecture- Querying a Database."

Similar presentations


Ads by Google