cscu9b1/pdmu9l7 databases 1 · pdf filecscu9b1 - essential skills databases 1 – pdmu9l7...

14
CSCU9B1 - ESSENTIAL SKILLS DATABASES 1 PDMU9L7 STREAM COMPUTING SCIENCE & MATHEMATICS PAGE 1 OF 14 CSCU9B1/PDMU9L7 DATABASES 1 INTRODUCTION TO MS ACCESS LEARNING OUTCOMES By the end of this practical students should be able to: Use MS Access to create a new database. Create a database table. Define a primary key for a table. Enter data in a table using different interfaces. Sort, search and filter data in a database table. Execute simple queries on a database table. Produce a formatted report from a database table. TRANSFERABLE SKILLS Understanding basic database concepts. The ability to analyse a problem in terms of related entities. IMPORTANT Remember to record your attendance via Groups on wide. Have you any checkpoints still to complete? Remember that checkpoints can only be checked for three weeks and then they are unavailable. Make sure you are up to date. Saving: Save any documents you create in the Office 2007 file format. This is the software in use across the University. We do not recommend that you work in earlier versions of the Office Suite (e.g. at home) since the interface is quite different. If you plan to work in an previous version of the Office Suite, then you need to save your work in a special format. Please ask if you are unsure. INTRODUCTION What do we mean by a database? In simple terms, it’s just a collection of related data. We are, of course primarily interested in data which is stored electronically on a computer. Most databases in use today are actually called relational databases for reasons, which will be explain later. The important point is that the information (data) that forms the database content is stored in a centralised and highly structured way. Let me give you an example. The University maintains a Student Records database containing information about: Students (Registration number, name, address, phone number, email, modules taken, final grades, etc.) Modules taught (module code, title, co-ordinator, level, etc.) …and much more besides

Upload: vomien

Post on 30-Mar-2018

217 views

Category:

Documents


3 download

TRANSCRIPT

CSCU9B1 - ESSENTIAL SKILLS DATABASES 1 – PDMU9L7 STREAM

COMPUTING SCIENCE & MATHEMATICS PAGE 1 OF 14

CSCU9B1/PDMU9L7 DATABASES 1

INTRODUCTION TO MS ACCESS

LEARNING OUTCOMES

By the end of this practical students should be able to:

Use MS Access to create a new database.

Create a database table.

Define a primary key for a table.

Enter data in a table using different interfaces.

Sort, search and filter data in a database table.

Execute simple queries on a database table.

Produce a formatted report from a database table.

TRANSFERABLE SKILLS

Understanding basic database concepts.

The ability to analyse a problem in terms of related entities.

IMPORTANT

Remember to record your attendance via Groups on wide.

Have you any checkpoints still to complete? Remember that checkpoints can only be checked for three weeks and then they are unavailable. Make sure you

are up to date.

Saving: Save any documents you create in the Office 2007 file format. This is

the software in use across the University. We do not recommend that you work in earlier versions of the Office Suite (e.g. at home) since the interface is quite different. If you plan to work in an previous version of the Office Suite, then you need to save your work in a special format. Please ask if you are unsure.

INTRODUCTION

What do we mean by a database? In simple terms, it’s just a collection of related data. We

are, of course primarily interested in data which is stored electronically on a computer. Most

databases in use today are actually called relational databases for reasons, which will be

explain later. The important point is that the information (data) that forms the database

content is stored in a centralised and highly structured way. Let me give you an example.

The University maintains a Student Records database containing information about:

Students (Registration number, name, address, phone number, email, modules taken,

final grades, etc.)

Modules taught (module code, title, co-ordinator, level, etc.)

…and much more besides

CSCU9B1 - ESSENTIAL SKILLS DATABASES 1 – PDMU9L7 STREAM

COMPUTING SCIENCE & MATHEMATICS PAGE 2 OF 14

Whichever way you look at it, this is going to be a lot of data! Not only that but it must be

recorded accurately in the first place, maintained and updated efficiently and be accessible to

many different users simultaneously throughout the University.

Another example is the University Library database. This contains information about Books

(e.g. Title, Author, ISBN, etc.) and Borrowers (e.g. Name, Address, Phone, etc.). In addition,

the database is updated every time someone borrows (or returns) a book. Let’s consider a

simplified version of this Library database. Suppose that the database contains information

about its staff borrowers as follows:

Borrower Table:

Payroll Number Name Department Room Phone

123456 Johnstone, WS CS 4B74 7451

234567 Smith, DA CS 4B51 7468

etc.

Now suppose that the database contains the following data about books:

Book Table:

ISBN Title Author Publisher Release Date

078972507 The Complete Idiot’s Guide to Office XP Kraynak, J Alpha books 2001

0130399175 ECDL The Complete Coursebook Munelly & Holden

Pearson 2004

etc

Obviously, the library will have many borrowers and many books (and other sorts of

publications). For each borrower, and each book there will be a record in the database

containing the sorts of information above (and possibly much more besides). The list of

records for all library borrowers would form a table of data, each row of which would be an

individual borrower’s record. Similarly, the books table would contain rows of data and each

row would refer to a different book. In database terminology, the fundamental things in our

library database (borrowers and books) are called entities, the rows are called records and

the columns are called fields or attributes.

So far, we have described how data can be stored about the fundamental entities in our

database, (i.e. books and borrowers) but what happens if someone wants to borrow a book?

How do we record that? To achieve this process of keeping track of who has borrowed what,

we need to record the details of the borrower and the book together. In addition, there will be

other information associated with the act of borrowing the book (e.g. the date the book was

borrowed and the date is due back). Suppose we decided to record the book’s data and the

borrower’s data along with the dates in another table called Loan. So, if a borrower borrowed

a book we would have an entry in our new table like:

Payroll

Number

Name Department Room Phone ISBN Title Author Publisher Release

Date

Issue

Date

Return

Date

123456 Johnstone,

WS

CS 4B74 7451 078972507 The

Complete

Idiot’s Guide to

Office XP

Kraynak,

J

Alpha

books

2001 09 Jul

04

16 Jul

04

CSCU9B1 - ESSENTIAL SKILLS DATABASES 1 – PDMU9L7 STREAM

COMPUTING SCIENCE & MATHEMATICS PAGE 3 OF 14

As you can see, from this example, simply borrowing a book would require a large amount of

data to be stored, which would take up a lot of file space. In addition, every time a book is

borrowed, the book’s details and the borrower’s details are being duplicated over and over

again. This is surely unnecessary! Let’s go back to our basic tables (Book and Borrower).

Notice that the Book table contains an ISBN column to hold the International Standard Book

Numbering code, which identifies the book uniquely. Obviously, in practice, the library may

have several copies of the same book but these could easily be identified by adding a copy

number at the end of the ISBN. We don’t need to concern ourselves about this level of detail.

Notice also that Borrowers have a Payroll number (could be a student registration number, or

even just a code issued by the library for each user), which is unique to the borrower.

In fact, any table in a well-designed database must always have a way of uniquely identifying

each row (i.e. borrower or book in this case). This is called the Primary Key of the table. In

the simplest case, the uniqueness is defined in one column designed for that purpose (e.g.

Payroll Number and ISBN would be the primary keys for the Borrower and Book tables). If

we were to use, for example, the borrower’s name or the book’s title, there is no guarantee

that two borrowers might not have the same name or that, indeed, two books might not have

the same title. As we shall see in due course, it is not always possible to uniquely identify

each row in a table from one column alone.

To record the fact that a borrower has borrowed a book, we just need to record the borrower’s

primary key field and the book’s primary key field along with any data specific to the loan

(e.g. the dates). So our Loan table would look like the following:

Payroll Number ISBN Issue Date Return Date

123456 078972507 09 Jul 04 16 Jul 04

I’m sure you will agree that this is a lot neater. As long as the Payroll Number and the ISBN

in the Loan table actually exist in the Borrower and Book tables, then there should be no

problem. In fact we can arrange that it is impossible to record a loan if either the book or the

borrower does not exist. This is taken care of by the Database Management System (DBMS),

which in our case is MS Access, part of the Microsoft Office suite.

Before we go ahead and create our first database, consider one other aspect of the example

above. What we have described is a set of three related tables (a relational database). The

relationship between the tables lies in the facts that, firstly, the Payroll Number field in the

Loan table refers to the same data as the Payroll Number field in the Borrower table and,

secondly, the ISBN field in the Loan table refers to the same data as the ISBN field in the

Book table. To put it another way, the value of the Payroll Number in the Loan table can only

be one of the values, which already exists in the Payroll Number field in the Borrower table.

Similarly, the value of the ISBN field in the Loan table can only be one of the values, which

already exists in the ISBN field in the Book table. When we make our own databases, we

must define these relationships as part of the database construction process.

CSCU9B1 - ESSENTIAL SKILLS DATABASES 1 – PDMU9L7 STREAM

COMPUTING SCIENCE & MATHEMATICS PAGE 4 OF 14

The Borrower and Book tables, in our example, are sometimes called primary tables while

the Loan table is a related table, which indicates that it only exists because of the prior

existence of its primary tables.

Your First Database

If the discussion above doesn’t make too much sense yet, don’t worry. We’ll spend a lot of

time illustrating the points described. It would be a good idea to come back and read the

introduction again when you have tried to build your first database.

The simplest type of database is one with a single table. You might want to set up a personal

phone book, catalogue, your Video, CD, DVD or MP3 collections, or build up a list of

customers’ details if you are running a small business. You can still perform lots of useful

tasks such as finding, sorting and selecting records within a single table.

Let’s create a small database about movies.

» In your home file store on the H: drive create a folder to hold all your work for this module.

Call it PDMU9L7.

» Inside your PDMU9L7 folder create a folder called Databases.

» Start up Access. There should be an icon on the desktop or you can find it via the start menu

When Access starts up there should be a welcome screen offering various templates.

» Choose Blank Database.

» The following Panel appears on the right hand side. Click the wee open folder icon, navigate

to your PDMU9L7/Databases folder and create the database as movies.accdb in this folder.

Click OK.

» Click Create.

» You should be presented with a new empty table.

Setting up a table

» There are two ways to enter into Design View. Use

whichever you prefer:

CSCU9B1 - ESSENTIAL SKILLS DATABASES 1 – PDMU9L7 STREAM

COMPUTING SCIENCE & MATHEMATICS PAGE 5 OF 14

» On the top left of the ‘Ribbon’ bar, the left most icon ‘View’ will let you toggle between

Datasheet View and Design View.

» Right click on the table listed in the left panel and select Design View.

» This brings up a ‘Save As’ window. Enter the name ‘Movies’. Then we get to Design View.

The Table Design window allows us to specify what the table will look like. We need to

decide what fields or attributes of movies we are going to store in our database. For our

simple movie database, we can start by storing the following:

Movie title

Director

Writer

List of actors

Year of first release

Running Time

Let’s just go ahead and create the table.

» Click in the first cell under the Field Name heading to position the cursor (The one that says

ID in it).

» Replace ‘ID’ in the column heading with ‘Title’.

» Now move to the next cell on the right under the Data Type heading (the one with

AutoNumber in it).

You can use the arrow keys, the Tab key, or even the mouse to get you there. When you move

to the cell, what should happen is that it becomes a selectable choice list. You can see the

choices by clicking the small, downward-pointing arrow on the right. This is our opportunity

to decide what type of data should be contained in the Title field of our database. Choose

Text.

Why should we choose a data type? One good reason is that, when we are entering data into

our database, Access will prevent us from putting inappropriate content into a field (e.g. text

into a number field) thus helping to avoid errors.

Having set the field and data type, you can enter a description for the field, which Access will

use if available. Usually this is not necessary.

Take a look at the lower half of the Table Design window. There are a number of items which

allow us to be even more precise about what the field may contain. Notice that the Field Size

property is set at 255, which means that the movie title can contain up to 255 characters. This

should be plenty, but we can always change it later (check out the list at

http://www.listology.com/list/longest-movie-titles-ever if you want to see the longest movie

titles ever).

We’ll use some of the field properties. It wouldn’t make any sense to have a movie without a

title (that would be a bit too avant-garde) so let’s make the title field required.

» Click in the box beside the Required label and a list of possible choices should appear. In this

case there are only two: Yes or No. Choose Yes.

» Next, set the property called Allow Zero Length to No.

» Back in the top half of the window, move to the next row and create a field called Director of

Text data type.

» Create a Writer field of Text data type too.

The ‘List of actors’ field presents us with a problem insofar as it will contain several names.

When we come to consider a more evolved version of our database, we will see that recording

multiple values in a single field is a very poor way of building a database. In fact, it is the first

thing you would notice and avoid. However, it will be educational to do it this way and later,

refine our database. We will use this field to record only the main actors in each movie.

CSCU9B1 - ESSENTIAL SKILLS DATABASES 1 – PDMU9L7 STREAM

COMPUTING SCIENCE & MATHEMATICS PAGE 6 OF 14

» Anyway, call the field Cast and make it a text data type again.

» Create a Year field of type Number and a Runtime field, also of type Number. (Using Year

will cause Access to warn you about it being a reserved word, just hit ok).

Notice I have not said anything about making the fields, other that Title, required. We could

if we want make them all required but in reality, we may not always be in possession of all

the data about every movie at the time when we are setting up the database. Not making

fields required allows us to leave blanks if necessary.

We are almost ready to save our table and start entering data into it but first we need to think

about the primary key for it.

» By default, Access will set the first row to be the primary key. You can see this from the

small key symbol in the left most column. You can remove this first clicking on the small

key next to your title cell, then by selecting the Design tab on the ribbon and hitting the large

primary key button.

Our first attempt at creating a database will only

contain one table and therefore does not strictly

need a primary key but, as we’ll be developing

our database to be more sophisticated, we might

as well have one. As we said above, a primary

key can be any field (or set of fields) which can

uniquely define the record (row) in the table.

The Title field would seem to be a good candidate for this purpose and indeed for our simple

database would do fine. (It is, of course quite possible for two films to have the same name -

it is not unknown for films to be remade).

Entering Data

We’ve created the layout of the table (i.e. the list of headers for the columns and the kind of

data each column should contain). This is sometimes called the schema of the table. To enter

data into the table we need to switch to the Datasheet View of our table.

» First save your table. (hit the buton)

» Click on the View button at the top left of the Design tab. It has an icon

that looks a bit like a tiny data table.

» Now we can see the Datasheet view of the table. Notice that the View

button has changed its appearance.

Clicking on it again will return us to Design View. This is an example of a

toggle button, which just switches between two different modes.

» Enter the following data into your table:

Title Director Writer Cast Year Runtime

Sunset Blvd. Billy Wilder Charles Brackett William Holden,

Gloria Swanson

1950 110

Double

Indemnity

Billy Wilder James Cain Fred MacMurray,

Barbera Stanwyck

1944 107

The Third Man Carol Reed Graham Greene Joseph Cotten,

Alida Valli, Orson

Welles

1949 104

The Maltese

Falcon

John Huston Dashiel

Hammett

Humphrey Bogart,

Mary Astor

1941 101

Touch of Evil Orson

Welles

Whit Masterson Charleton Heston,

Janet Leigh, Orson

Welles

1958 95

CSCU9B1 - ESSENTIAL SKILLS DATABASES 1 – PDMU9L7 STREAM

COMPUTING SCIENCE & MATHEMATICS PAGE 7 OF 14

» Just type the data in. Use the Tab or arrow keys to move between cells.

If you make a mistake, you can move back to a field at any time and edit the contents. You

can change the width of the columns by moving the dividers between the column headers.

Along the bottom left of the Datasheet window for the table is a set of widgets that allow you

to navigate through the table more easily when the table becomes reasonably large.

You can type a record number into the box to move immediately to that record, or use the

buttons to move backwards and forwards one at a time or all the way to the end or the

beginning. The button with the arrow and the asterisk lets you insert a new record.

Using data entry forms

You may already find that there is insufficient room on the screen to see the entire width of

the rows without scrolling left and right. While Datasheet view is actually very useful for

entering data initially (and it allows you to keep many other records visible at the same time),

scrolling back and forward all the time can become a bit of a chore, so Access offers you

another way of entering data into your tables, namely, Forms.

» Select the Create tab from the ribbon, and then chose Form.

This should give you a form window showing just one of the records in the table. The form

window is just a different view of the table and you can enter data just as easily as in

Datasheet View. The advantage of the form is that you can see all the details of the current

record even if the table has a large number of fields. Forms can also be customized to be

much more user friendly than Datasheet View.

» At the moment the form is in design view, hit the view button to jump from Layout View to

Form View.

» Press the New Record button at the bottom right of the form.

» Add the following four records to the database:

Title Director Writer Cast Year Runtime

Strangers on a

Train

Alfred

Hitchcock

Raymond

Chandler

Farley Granger,

Ruth Roman,

Robert Walker

1951 101

Notorious Alfred

Hitchcock

Ben Hecht Cary Grant,

Ingrid Bergman,

Claude Rains

1946 101

The Big Sleep Howard

Hawks

Raymond

Chandler

Humphrey

Bogart, Lauren

Bacall

1946 114

The Killers Robert

Siodmak

Ernest

Hemingway

Burt Lancaster,

Ava Gardner

1946 105

» When you enter the last field of the last row, make sure to press Return and this will save the

record and clear the data in the form fields.

» Save your Form, just use the default name ‘Movies’.

Using the database

Now that we’ve set up our database, we can start to make use of it. We usually talk about

querying the database. Essentially, we are asking questions and the database management

CSCU9B1 - ESSENTIAL SKILLS DATABASES 1 – PDMU9L7 STREAM

COMPUTING SCIENCE & MATHEMATICS PAGE 8 OF 14

system, having allowed us to put data into the database, allows us to get data back out to

answer our queries.

In our simple movie database, we might want to ask questions like:

Who directed Strangers on a Train?

Which movies were directed by Billy Wilder?

Which movies have a running time of 101 minutes exactly?

Which movies were first released before 1950?

In which movies did the director also appear as actor?

Which directors directed movies containing Orson Welles?

Because the database is so small, we can answer these questions quite

easily by inspecting the table directly but, of course, the real movie

database (even our little ‘film-noir’ subsection of it) would be much

more substantial containing at least a few hundred films. In case you

are wondering, I’ve ‘acquired’ the data above from the Internet Movie

Database web site at www.imdb.com/chart/filmnoir.

All modern relational database systems (Access, Oracle, MySQL,

PosrtgreSQL to name but a few) can be interrogated or queried using a

standard Data Manipulation Language known as SQL (Structured

Query Language). Learning SQL is not the business of this course

although we will have a brief look at some simple examples later.

Access provides us with an interface, which allows us to construct queries by selecting fields

from tables and applying conditions and restrictions to obtain answers to questions like those

above.

Sorting, searching and filtering

Before we construct specific queries, let’s look at what Access can do in the way of data

manipulation ‘off-the-shelf’.

Sorting

» Sorting records in a table is easy. Jump back to the Movies Datasheet View. Clicking on any

column header will bring up a menu allowing you to sort that column in either ascending or

descending order.

Multiple Criteria Sort

Suppose that you want to sort the data by more than one field at a time, for example, you

might want to sort the films into year order first and then by ascending order of running time.

» Choose the Home tab from the ribbon, and then the Advanced button. Then choose

Advanced Filter/Sort.

» In the filter/sort grid, which appears at the bottom of the window, leave the Field box in the

first column as Year.

» Choose Ascending from the drop down list, which appears when you click in the Sort box in

the first column. If your table was already sorted, these values would have been already

inserted.

» Now move to the second column and this time select Runtime and Ascending for the second

sort. (If you have more than two columns simply delete the text from the field cell)

» Choose Toggle Filter from the Home tab and examine the results.

Check that films with the same year are sorted in order of running

time.

CSCU9B1 - ESSENTIAL SKILLS DATABASES 1 – PDMU9L7 STREAM

COMPUTING SCIENCE & MATHEMATICS PAGE 9 OF 14

Searching

Suppose you can’t remember the name of a film but you

can remember that it starred Humphrey Bogart:

» Open the Movies table again and click on the Find button

(or hit CTRL+F on the keyboard).

Type in what you want to find in the Find What box.

Better still, just type in a few distinctive characters like

“bog” in case you don’t know how to spell his name. This

will cause the search to be made for the string of letters b

and o and g occurring consecutively.

» Choose Movies: Table in the Look In box and Any Part

of Field in the Match box. Remember, our Cast field is,

at the moment, just a list of names separated by commas.

» Hit the Find Next button. It should cause the string to be found in the row containing the film

“The Maltese Falcon”. Hitting the Find Next button again should find “The Big Sleep”.

Hitting it a third time should result in a message indicating (somewhat obscurely) that there

are no more occurrences of the search string.

» Open your Movies data entry form again and use the Find feature to search for the string

“bog” again. Each time you press the Find Next button, the record on the screen should

become the next one containing the search string. So the Find procedure also works in Form

view.

Filtering

» Filtering data is also possible without much effort. To find a group of records which have the

same value in one of the fields (e.g. movies written by Raymond Chandler), with the table

open in Datasheet View, click in the Writer column heading.

» The easiest way is to first deselect ‘(Select All)’ then select ‘Raymond Chandler’.

» Now press the Filter By Selection button and only the films written by Mr

Chandler will be left on screen.

» To switch off the filter just select

the column heading again and hit

‘Clear Filter from Writer’.

» Now Apply a Filter to the

Director column to make it

show all the films directed by

Alfred Hitchcock. There should

be two films listed: Strangers on

a Train and Notorious.

» Suppose we want to find films directed by Alfred Hitchcock

or Billy Wilder. We need an OR filter! No problem!

» Do as above, this time selecting both Alfred Hitchcock and

Billy Wilder.

Sorting, searching and filtering can deal with many of your

requirements but are best used for simple one-off actions.

Although you can elect to save your table after it has been

CSCU9B1 - ESSENTIAL SKILLS DATABASES 1 – PDMU9L7 STREAM

COMPUTING SCIENCE & MATHEMATICS PAGE 10 OF 14

sorted into a different order, the results of searches and filters are not preserved in any way. In

general, your database queries are likely to be more complex than can be easily dealt with by

the above techniques. In particular, as we shall see later, you may be querying data contained

in several tables and you may only want to view certain chosen columns in the results.

Queries

Queries allow you to make almost any imaginable selections from your database. In addition,

queries can be saved for future use. Each time you run a query it will operate on the most up-

to-date data in the database. Running a query produces a set of results (sometimes called a

result set) as we shall see, but the results do not get saved so that the next time you run the

same query you may get different results if you or someone else has updated the database in

the meantime. If I ask the student records database who is registered for PDMU9L7, I don’t

want to know who had registered three days ago (or even ten minutes ago) but who is

registered right now!

In our Movies database, suppose you want to list just the titles and the directors of all movies

made between 1946 and 1951 inclusive. We’ll create a query!

» (Clear any filters)

» From the Create tab select Query Design.

» In the Show Table dialog box choose the

table you want to use in your query (mmm… I think

we’ll choose Movies).

» Press Add and then Close to remove the dialog box.

» If necessary, now resize the different bits of the Query

window so that you can get a good view of all the areas

without having to use scrollbars.

We are interested in the Title, Director and Year fields from our table although we don’t

actually want to display the Year field in the results. We could if we wanted, but we don’t!

Notice that all the fields of the Movies table are displayed in a little window in the top half of

the screen. To insert any of these fields into the grid in the bottom half of the window, called

the QBE (Query By Example) grid, just double-click on its name in the little window.

» Do this now; double-click on Title, Director and Year in that order. Alternatively, you can

click in the first Field box and choose from the drop-down list that appears. Either way, your

queries should now contain the three fields required.

» To view the results of a query (even one that is not complete), press the Run

button from the Design tab.

At the moment, the query should just contain the three columns from all of the rows in the

table.

» Press the View button to switch back to Design View.

CSCU9B1 - ESSENTIAL SKILLS DATABASES 1 – PDMU9L7 STREAM

COMPUTING SCIENCE & MATHEMATICS PAGE 11 OF 14

» We only want to see films made between 1946 and 1951. To achieve this, click in the

Criteria box under the Year column in the grid. Type in:

>= 1946 And <= 1951

This expression tells Access only to display rows in which the Year column value is both

greater-than-or-equal-to 1946 and less-than-or-equal-to 1951. You must enter the

expression exactly as above otherwise it will not work. View the query results again. Has

Access ignored the years out of the range of our query?

» Notice that we can arrange for the results of the query to be sorted if we like. Click in the Sort

box under the Year heading and choose Ascending. Check that the sort has been performed

by viewing the results again.

» Finally, for this query, we said that we only wanted to see the Title and Director fields

displayed, so uncheck the Show box under the Year heading. View the results. Success?

» Save the query. Press the Save button. You’ll be given the chance to choose a name for the

query. Access suggests the name Query1. Call it Title+Director+Year46to51.

At any time in the future, you can run this query again simply by double clicking on the

Title+Director+Year46to51 Query in the left hand column. You can open a query again in

Datasheet View, to see the results, or in Design View, to edit or modify it. Hey, it’s your

query, you decide!

There are a number of useful criteria operators that you can use. For example:

And Both condition must be true

Or Only one of the conditions need be true

Not e.g Not > 5 would be the same as <=5

Like Containing the specified item (see below)

Between… And Alternative to the example above (>= And <=)

=,<>,>,<,>=,<= Equals, not equals, greater than, less than, greater than or equal, less than

or equal.

*,/,+,- Multiply, divide, add, subtract (see below)

Let’s briefly look at a couple of these.

We’ll use the Like operator to repeat the search we did earlier for movies in which Humphrey

Bogart appears:

» Start a new query. Include the Title, Year and Cast fields.

» In the Criteria box under Cast type

Like *bog*

The asterisks mean that we are looking for the string somewhere within the field so there may

be characters before or after the search string. For example, if the search string was *bog then

a match would be found only if the field ended with the string bog. Similarly bog* would

produce a match only if the field started with bog.

» View the query results. This time we will elect to view the Cast field just in case there are any

other actors who might match the query string.

CSCU9B1 - ESSENTIAL SKILLS DATABASES 1 – PDMU9L7 STREAM

COMPUTING SCIENCE & MATHEMATICS PAGE 12 OF 14

» Save your query as Like *bog*.

» Start a new query. This time select the Title field only.

When we create a query, it is possible to construct new ‘columns’ that do not already exist in

the table. These columns are constructed temporarily and do not persist after the query has

been executed. Suppose we wanted to show the year as just the last two digits since we know

that all of our classic Hollywood movies were made in 19-something. Here’s how we could

do it.

» Click on the Field box adjacent to the Title column, which should be blank. Instead of

choosing one of the fields from the drop-down list, type in

Short Date: [Year]-1900

Short Date will be the heading for the column. Anything after the colon will form the

expression used to calculate the contents of the column. Any existing fields used in the

expression must be enclosed in square brackets, so the expression [Year]-1900 means ‘the

value of the Year column minus 1900’.

» View the results.

Alternatively, Access offers a tool called Expression Builder to allow you to manufacture

your expressions. Right-clicking on the Field box in Design View and choosing Build from

the menu that appears will lead you to the Expression Builder. Personally I’d leave it well

alone for now though!

» Save your query as Short Date.

Remember the queries suggested earlier?

1. Who directed Strangers on a Train?

2. Which movies were directed by Billy Wilder?

3. Which movies have a running time of 101 minutes exactly?

4. Which movies were first released before 1950?

5. Which directors directed movies containing Orson Welles?

» Construct a query for each one. Use the question itself as the name for the query.

Checkpoint

Call over a tutor and demonstrate your list of queries. Then go on with the rest of the worksheet.

Did you notice that I missed one of the queries out? You might want to know:

In which movies did the director also appear as actor? We can’t do this with the present

database! But you will learn about how to do this next week.

CSCU9B1 - ESSENTIAL SKILLS DATABASES 1 – PDMU9L7 STREAM

COMPUTING SCIENCE & MATHEMATICS PAGE 13 OF 14

Reports

Now that we know how to get data in and out of our database, we need to consider how to

produce output that people might actually find useful and readable. Access lets us format the

results of queries in a number of ways.

» Click the Report button in the Create tab with the desired table open (could be

the whole table (Movies : Table) or maybe one of the queries (like Short Date).

» You should get an automatically generated report based on the table contents.

Nice!

» Go back to the main table then select Report Wizard from the Create tab.

» Choose Table: Movies in the Table/Queries

box.

Firstly we are asked which fields to include

in the report. To select a field, just double-

click its name in the Available Fields list

(you can single click and then move the

field across using the arrow button, if you

prefer).

» Select the fields Title, Director, Cast and

Year. Now press Next.

At this point, we can elect to group the

results by any of the chosen fields.

» Just ignore this step and press Next.

» Now we can sort the records by any of the chosen fields. Choose to sort first by Year

(Ascending) and then by Title (Ascending).

» Press Next.

» Choose the default layout (i.e. Tabular and Portrait). Press Next.

» Pick a style from the predefined list. We can customize things later.

» Press Next.

» Call your report Movies by Year by Title.

» Press Finish. The report should appear.

CSCU9B1 - ESSENTIAL SKILLS DATABASES 1 – PDMU9L7 STREAM

COMPUTING SCIENCE & MATHEMATICS PAGE 14 OF 14

Note that the report title appears at the top, followed by the chosen column headings

followed, by the records sorted in year order. Records with the same year are sorted by title.

Some standard footer data has been entered at the bottom of the page.

You should be seeing the Print Preview layout of the report. It is possible to customize the

layout if you close print preview. In fact, it is possible to create a report from scratch in

Design layout if you really want to. (We don’t).

» You will see a view of the report, which breaks it into several sections whose names appear in

the horizontal blue bars across the screen. To edit an item just click on it once to select it and

click on it again to edit it just as you would in Word.

Editing the various Header and Footer sections makes sense since the field names have been

copied straight from the database and may be too concise for the report. However, editing the

text of the Detail section does not make sense because these names refer to the underlying

database fields and to change them would break the link when the report is generated. If you

make a mistake, you can always use the undo button to reverse the previous steps.

Try the following changes; It may help to jump between Design View and Print Preview to

see what effect your changes are having as you work.

» Change the Report Header to Movies Ordered by Year and then by Title.

» Alter the Report Header font to size 26. Resize the text box if you have to.

» In the Page Header Section change Year to Release Date and Cast to Starring.

» View the report by toggling the view button. Save the report.

Onwards and upwards…

We have just about exhausted all the possibilities regarding things to do with a one-table

database. The power of databases rests in the design and structure of multi-table databases

where the tables are related to each other and where the core data is stored once without

duplication.

Next time we will reconstruct our database in a more useful way, splitting the data into

several tables joined by relationships.