database exercise 1 database queries - teachnet uk projects/ict-access... · database exercise 1...

13
Database Exercise 1 Database Queries Learning outcomes You will understand basic database vocabulary and that the function of a database is to store data and produce information. You will be familiar with opening an Access database and working in MS Access You will be able to create a simple query selecting specified records and fields and sorting data from existing data stored in a database. You will go on to create a range of more complex queries in order to find specific information from existing data stored in a database using a combination of search criteria including: o Find records that match one criterion AND another o Find records that match one criterion OR another o Find records that match one criterion but NOT another o Find records that match BETWEEN one criterion and another o Find records that match similar to, or LIKE, a particular criterion o Use parameter value queries to perform a "fuzzy" search. You will be able to create their own queries using a combination of various criteria and appreciate that queries based on fields with different data types require criteria formed with different syntax

Upload: others

Post on 18-Jul-2020

27 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Database Exercise 1 Database Queries - Teachnet UK Projects/ICT-Access... · Database Exercise 1 – Database Queries Page 1 of 12 THE BASIC ELEMENTS OF A DATABASE A database is a

Database Exercise 1 Database Queries

Learning outcomes

• You will understand basic database vocabulary and that the function of a database is to store data and produce information.

• You will be familiar with opening an Access database and working in MS Access

• You will be able to create a simple query selecting specified records and fields and sorting data from existing data stored in a database.

• You will go on to create a range of more complex queries in order to find specific information from existing data stored in a database using a combination of search criteria including:

o Find records that match one criterion AND another o Find records that match one criterion OR another o Find records that match one criterion but NOT another o Find records that match BETWEEN one criterion and another o Find records that match similar to, or LIKE, a particular criterion o Use parameter value queries to perform a "fuzzy" search.

• You will be able to create their own queries using a combination of various criteria and appreciate that queries based on fields with different data types require criteria formed with different syntax

Page 2: Database Exercise 1 Database Queries - Teachnet UK Projects/ICT-Access... · Database Exercise 1 – Database Queries Page 1 of 12 THE BASIC ELEMENTS OF A DATABASE A database is a

Database Exercise 1 – Database Queries

Page 1 of 12

THE BASIC ELEMENTS OF A DATABASE A database is a tool for storing, organising and finding information and processing data (raw facts and figures) into useful information Data stored in a database is divided into fields and records. A record is all the information about one person, product, company or event (these are known as data subjects in the database.) The information about each data subject is divided into categories common to each record, for example, name, address, telephone number etc. These are known as fields. Each field has a field name and the information you enter into a field is known as a field entry.

WORKING IN MICROSOFT ACCESS Microsoft Access is different to other programs. To people who are used to working with Microsoft Word or Excel, Access might come as something of a shock. Whereas there are some aspects of this program that might seem familiar, others are completely different and this sometimes takes a little adjustment. The main differences between Access and other programs include:

• Setting the file name before entering data (this is a safety aspect to prevent data loss – data is saved as you work)

• Data being saved as you work • The ability to view data in many different ways, some of which are only

intended for viewing on the screen – not for printing.

Access objects are divided into main areas such as tables, queries or reports. Each of group of objects is opened by clicking the correct tab on the main dialogue box.

In this unit of work you will learn about the following: Tables – the main place where data is stored. Queries – allow the user to select chosen information from the table or tables. Forms – can be designed to allow easier data entry. Reports – allow for printing information based on tables or queries.

Page 3: Database Exercise 1 Database Queries - Teachnet UK Projects/ICT-Access... · Database Exercise 1 – Database Queries Page 1 of 12 THE BASIC ELEMENTS OF A DATABASE A database is a

Database Exercise 1 – Database Queries

Page 2 of 12

Simple queries The main purpose of a database is to store data (raw facts and figures) and process the data into meaningful information. In this exercise you will create a query in order to find specific information from existing data stored in a database. Locate the file called Video1.mdb Make a copy of this file in your own user area. Double click the file in order to open MS Access Note that there is already a table of data stored in the database called tblVideos.

Double click the object tblVideos and have a look at the data stored there

Close the table (but not the whole database).

Page 4: Database Exercise 1 Database Queries - Teachnet UK Projects/ICT-Access... · Database Exercise 1 – Database Queries Page 1 of 12 THE BASIC ELEMENTS OF A DATABASE A database is a

Database Exercise 1 – Database Queries

Page 3 of 12

Query 1 – displaying certain from fields and sorting the data Click on Queries tab Click Create query in Design view.

Click on tblVideos in table list and then click on Add.

Click Close on this dialogue box

Page 5: Database Exercise 1 Database Queries - Teachnet UK Projects/ICT-Access... · Database Exercise 1 – Database Queries Page 1 of 12 THE BASIC ELEMENTS OF A DATABASE A database is a

Database Exercise 1 – Database Queries

Page 4 of 12

Drag the VideoID, title and certificate fields to the query grid.

The query that you have created will only show data from the three fields that you have selected. In order to view the data, click the view button (this is the first button from the left of the toolbar). This will take you from design view to datasheet view Datasheet view

Note that you have only shown the fields that you selected and not the all of the data in the data set. (See how this view is different from the view of the whole table on page 1 of this leaflet) Switch back to design view of query by clicking on view button again. Click on the Save button and save the query with the name qryFilmList

Page 6: Database Exercise 1 Database Queries - Teachnet UK Projects/ICT-Access... · Database Exercise 1 – Database Queries Page 1 of 12 THE BASIC ELEMENTS OF A DATABASE A database is a

Database Exercise 1 – Database Queries

Page 5 of 12

The list of films might be more useful if it were in alphabetical order. Working is design view, click the drop down list in the Sort cell in the Title column.

Choose Ascending Switch back to Datasheet view.

Note how the films are now listed alphabetically Save the query

Page 7: Database Exercise 1 Database Queries - Teachnet UK Projects/ICT-Access... · Database Exercise 1 – Database Queries Page 1 of 12 THE BASIC ELEMENTS OF A DATABASE A database is a

Database Exercise 1 – Database Queries

Page 6 of 12

Query 2 - Refining your search and only showing certain records It is possible to refine the way Access processes the data still further Working in design view type 15 in the criteria row in the certificate column

Switch back to data sheet view

Note how the data set now only displays films with a 15 certificate. Our query now shows only fields that we specified and only records meeting specified criteria. Save the query

Page 8: Database Exercise 1 Database Queries - Teachnet UK Projects/ICT-Access... · Database Exercise 1 – Database Queries Page 1 of 12 THE BASIC ELEMENTS OF A DATABASE A database is a

Database Exercise 1 – Database Queries

Page 7 of 12

Complex Queries In a previous exercise you made a query which found information according to one criterion. A complex query is one where you use a combination of criteria. You can look for criteria in various combinations. For example – Find records that match one criterion AND another Find records that match one criterion OR another Find records that match one criterion but NOT another Find records that match BETWEEN one criterion and another Find records that match similar to, or LIKE, a particular criterion In this exercise you will create some more queries but these will use a combination of criteria to produce more detailed information

Return to the file that you have already created called Video1.mdb If it’s not already open, double click the file in order to open MS Access

Query 3 – using AND to find records which match two search criteria You will create a query to find films that are Musicals AND had an award Click on Queries tab Click Create query in Design view. Click on tblVideos in table list and then click on Add. Click Close Add the fields Title, Category, Year, Award and Rating fields to the query grid. In the criteria cell of the Award field, type True In the criteria cell of the Category field type: Musical For AND queries place criteria in same row of the grid

Switch to datasheet view to see the result of your query. How many records match these criteria? Save the query as qryMusicalAward.

Page 9: Database Exercise 1 Database Queries - Teachnet UK Projects/ICT-Access... · Database Exercise 1 – Database Queries Page 1 of 12 THE BASIC ELEMENTS OF A DATABASE A database is a

Database Exercise 1 – Database Queries

Page 8 of 12

Query 4 – using OR to find record which match one criteria OR another This query will use the OR operator to find either 15 Or 18 certificate films. Create a new query based on tblVideos Add the fields Title, Category, Certificate and Rating to the query grid. In the criteria cell of Certificate field type: 15, in the OR cell type 18

Switch to datasheet view to see the result of your query. How many films match these criteria? Save the query as qry15or18. Query 5 – using BETWEEN to find data that falls in a certain range This query will use AND to find films released between 1980 and 1989 Create a new query based on tblVideos Add the fields Title and Year. In the criteria cell of the Year field type Between 1980 And 1989 Also click in the sort cell and sort Year into ascending order.

Switch to datasheet view to see the result of your query. How many films were released during this decade? Save the query as qryEightiesFilms.

Page 10: Database Exercise 1 Database Queries - Teachnet UK Projects/ICT-Access... · Database Exercise 1 – Database Queries Page 1 of 12 THE BASIC ELEMENTS OF A DATABASE A database is a

Database Exercise 1 – Database Queries

Page 9 of 12

Query 6 – using LIKE to do a fuzzy search Sometimes we want to use a query to find a specific record in a field with other information. The LIKE function allows us to find films featuring certain actors. Create a new query based on tblVideos Add the fields Title, Category, Director, Rating and Cast to the query grid. In criteria cell of the Cast field type: like *Schwarz* (the * stands for anything either side of the word Schwarz – this saves typing Schwarzenegger!)

Switch to datasheet view to see the result of your query. Save query as qrySchwarzeneggerFilms Query 7 - using NOT to find something that doesn’t match certain criteria Create a new query based on tblVideos Add the fields Title, Category In criteria cell of the Category field type Not Musical And Not Comedy

Switch to datasheet view to see the result of your query. Save query as qryNotMusicalOrComedy

Page 11: Database Exercise 1 Database Queries - Teachnet UK Projects/ICT-Access... · Database Exercise 1 – Database Queries Page 1 of 12 THE BASIC ELEMENTS OF A DATABASE A database is a

Database Exercise 1 – Database Queries

Page 10 of 12

Query 8 – using parameter values Rather than write a new query for every eventuality it is possible to create a query that prompts the database user to type in a query criterion. This is known as using parameter values and is done using square brackets [ ] in the criteria cell. Create a new query based on tblVideos Add the fields Title, Category, Director, Rating and Cast to the query grid. In the criteria cell in the Cast field type Like [Enter Name]

Switch to datasheet view to see the result of your query. When prompted type *cruise* (remember to include the *’s)

Click OK

Save the query as qryLike.

Page 12: Database Exercise 1 Database Queries - Teachnet UK Projects/ICT-Access... · Database Exercise 1 – Database Queries Page 1 of 12 THE BASIC ELEMENTS OF A DATABASE A database is a

Database Exercise 1 – Database Queries

Page 11 of 12

Query 9 – using greater that, less than or equals When you want to search for values in numerical fields you have to use slightly different syntax (wording). For example – in text fields the text search criterion is always surrounded by “speech marks” (Access even puts them in automatically). In number fields you never use speech marks. Create a new query based on tblVideos Add the fields Title, Category, and Length to the query grid. In the criteria cell in the Length field type <100

Switch to datasheet view to see the result of your query.

Save the query as qryLessThan100Minutes Try changing the value in criteria cell in the Length field >100 or =100 Switch to datasheet view to see the result of your queries.

Page 13: Database Exercise 1 Database Queries - Teachnet UK Projects/ICT-Access... · Database Exercise 1 – Database Queries Page 1 of 12 THE BASIC ELEMENTS OF A DATABASE A database is a

Database Exercise 1 – Database Queries

Page 12 of 12

Extension activities – Additional queries Create complex queries to find the following information:

1. Classification 15 films that are over 100 minutes in length 2. SciFi films with a rating of 5 3. PG films with a rating of 4. 4. Films that are either Musicals or Comedies. 5. Films that have been directed by Gurinder Chandha or George Lucas 6. Films featuring the following actors:

a. Arnold Schwarzenegger b. Tom Cruise c. Helen Hunt

7. Comedies made: a. Before 1990 b. Between 1990 and 2000 c. After 2000

8. Science Fiction films made: a. Before 1990 b. Between 1990 and 2000 c. After 2000

9. Films in the Star Wars series listed in the order they were released 10. A list of films that are not specifically suitable for family viewing.