69
Querying and Sorting IT Key Ideas, Dynamic Database Systems, 2002 Chapter 3

Querying and Sorting IT Key Ideas, Dynamic Database Systems, 2002 Chapter 3

Embed Size (px)

Citation preview

Querying and Sorting

IT Key Ideas, Dynamic Database Systems, 2002

Chapter 3

Interrogating the DatabaseInterrogating the Database

IT Key Ideas, Dynamic Database Systems, 2002

The real power of database software is the ability to query the database in order to find answers to specific questions,

page 15

or in other words to turn data into information.

IT Key Ideas, Dynamic Database Systems, 2002

The result of a query is a set of records that match the conditions of the query.

This set of records is a table in appearance, although it is not a real table of the database system in use. Often it is referred to as a temporary or resultant table.

Queries provide the following advantages• specific fields can be chosen to display particular

columns of data

• specific records can be searched for that match a criteria

• records can be viewed in a specific order

• calculations can be performed on the data in a table

• data from multiple tables may be matched

• specific data can be obtained for use in forms, reports or graphs.

The interrogating of the database to find records that match a criteria is an essential component of any database system. The result of the interrogation is referred to as an outcome of the system. An outcome should present a sub-set of the data, rather than show all of the records of the database.

IT Key Ideas, Dynamic Database Systems, 2002

When one asks for help in a library, the librarian will ask a question like

IT Key Ideas, Dynamic Database Systems, 2002

what topic are you researching

(eg. database systems) ,

or what author are you looking for

(eg. Potter)?

When a person walks into a car yard to purchase a car the salesperson will ask a question like

what brand of car are you interested in (eg. Commodore), or

what size car (eg. 6 cylinder) or

what type of vehicle (eg. wagon)?

Queries

Practical 3.1page 16

IT Key Ideas, Dynamic Database Systems, 2002

2. Display a list of all surnames and given names, in that order.

3. Display a list of given names followed by surnames, where the list is alphabetically sorted by surname.

4. Display a list of the surnames of females and then males, where each group is alphabetically sorted.

5. Display a list of the people who have red hair, in alphabetical order?

6. How many males are there? Hint: look at the data in the table before executing the query.

7. Display a list of people who do not have blonde hair.

8. How many people are 15 years of age?

IT Key Ideas, Dynamic Database Systems, 2002

Practical 3.1

Database file for querying

IT Key Ideas, Dynamic Database Systems, 2002

Data File Class Details

Querying a Database Table

IT Key Ideas, Dynamic Database Systems, 2002

Show/Add a table to the query

IT Key Ideas, Dynamic Database Systems, 2002

IT Key Ideas, Dynamic Database Systems, 2002

click and drag fields to the Field line

or click the pop-down list arrow and choose

QBE Grid

page 17

To display all fields page 17

drag the asterisk to

the field line

Toolbars

IT Key Ideas, Dynamic Database Systems, 2002

If the line Table line is showing within QBE grid turn it off, View Table Names command; as the current database only has one table.

IT Key Ideas, Dynamic Database Systems, 2002

Advice

Displaying particular fields

drag Given Names and Surname to the field line

click the Datasheet View or Run button in order to view the recordset

Changing the order of the Fields

IT Key Ideas, Dynamic Database Systems, 2002

highlight a column(s)

drag the column(s) across by using the Field Selector (it’s a very thin grey line)

FieldSelector

Ordering Records

IT Key Ideas, Dynamic Database Systems, 2002

click in the required column on the sort line and choose ascending

or

descending

Sorting on more than one fieldand controlling the display

IT Key Ideas, Dynamic Database Systems, 2002

Given Name Surname Sex

Kate Abbott FZena Abram FCatherine Albany FKate Ashby FPatience Baldwin FGeorgia Barnes FKate Barnes FWinifred Barr FJacinta Barrachina FClark Abbey MBenson Aitchison MJoseph Badcock MAlan Badcow MMatt Bazocca MMatthew Clarke M

how is the table organised?

Hint: in order to achieve the required output, it is possible to insert the same field more than once.

IT Key Ideas, Dynamic Database Systems, 2002

IT Key Ideas, Dynamic Database Systems, 2002

Given Names Surname SexAscending

SurnameAscending

Conditional Retrieval

IT Key Ideas, Dynamic Database Systems, 2002

Setting a condition, by using criteria in the QBE, can retrieve particular records of a database table. Remember that a record is a row of data. The database software processes the data in the database by finding records that match the criteria and produces a subset of data, from the whole system.

Alphanumeric Criteria

IT Key Ideas, Dynamic Database Systems, 2002

To find people with red hair, enter the value red as the criterion under the Hair Colour field.

Hair Colour

red

SQL equivalent

IT Key Ideas, Dynamic Database Systems, 2002

SELECT [Given Names], Surname

FROM Details

WHERE [Hair Colour] =”red” ORDER BY Surname;

SQL

IT Key Ideas, Dynamic Database Systems, 2002

SELECT specifies the fields in the query

FROM specifies the table the data is stored in

WHERE specifies the criteria that records have to match, and

ORDER BY specifies the order in which the records will be sorted.

IT Key Ideas, Dynamic Database Systems, 2002

How many males?

Sex

m

IT Key Ideas, Dynamic Database Systems, 2002

Not blondes

not is referred to as a logical operator

Hair Colour

not blonde

Calculation Fields

IT Key Ideas, Dynamic Database Systems, 2002

In order to query the database for ages, eg. 15 year olds, it is necessary to calculate people’s ages, as each person has their Date of Birth stored.

Date of Birth is stored so that when queries are executed at later times correct outputs apply irregardless of when the list is produced.

IT Key Ideas, Dynamic Database Systems, 2002

The fact that different outputs will be produced depending on when the query is executed is referred to as dynamic.

It is this concept of dynamic outputs that distinguishes IT apart from other fields of study.

IT Key Ideas, Dynamic Database Systems, 2002

In order to calculate people’s ages it is necessary to develop an algorithm to complete the calculation.

An algorithm is a series of steps to solve a problem.

IT Key Ideas, Dynamic Database Systems, 2002

Consider your age.

What does your age depend on?

Calculating Age

IT Key Ideas, Dynamic Database Systems, 2002

Your date of birth and today’s date

Calculating Age

IT Key Ideas, Dynamic Database Systems, 2002

Today’s date can be displayed by using the function Date().

Type this on the Field line.NB: The Now() function could be used alternatively.

Today’s date

IT Key Ideas, Dynamic Database Systems, 2002

The Expr1: can be replaced by your own field name, eg. Current – followed by a colon :

Today’s date

IT Key Ideas, Dynamic Database Systems, 2002

A person’s age is calculated byToday’s Date minus their Date of Birth

Age

IT Key Ideas, Dynamic Database Systems, 2002

Functions and fields can be selected using a point and click method by using the Expression Builder.

Age

IT Key Ideas, Dynamic Database Systems, 2002

Empty the field line and click on the Build button.

Select Functions

Built-in Functions

Date/Time

and double-clickDate

IT Key Ideas, Dynamic Database Systems, 2002

Type or click on a

minus sign

then select

Tables

Details

and double click

Date of Birth

Select OK

IT Key Ideas, Dynamic Database Systems, 2002

Also place the Date of Birth field in this query as a separate column.

Run the query.

IT Key Ideas, Dynamic Database Systems, 2002

This calculation produces a person’s age in days, as computers store dates in days - from a starting point, such as 1/1/1900.

IT Key Ideas, Dynamic Database Systems, 2002

To determine a person’s age in years, divide the result of the subtraction by 365.2425

NB: The decimal component is necessary to allow for leap years every 400 and every 4 years, but not every 100 years.

IT Key Ideas, Dynamic Database Systems, 2002

Re-enter the Expression Builder and enter the division. Hint: don’t forget the extra brackets. Change the name of the calculated field to Age.

Age

Run the query.

IT Key Ideas, Dynamic Database Systems, 2002

Ages now appear, except they include a decimal component.

To remove the decimal component, as one is 15 at 15 years and 1 day through to 364 days, another function, namely Int, is utilised.

Age, as a whole number

IT Key Ideas, Dynamic Database Systems, 2002

Int is a function in both database and spreadsheet software.

Example Int(Cost)

Int(number)

Item Cost Int(Cost)Deodorant 6.3 6Magazine 8.85 8Washing Powder 5.95 5Detergent 4.85 4Nails 7.74 7

IT Key Ideas, Dynamic Database Systems, 2002

Place the previous expression inside the brackets of Int function.

Age, as a whole number

Save this query for re-use many times.

IT Key Ideas, Dynamic Database Systems, 2002

15 year olds

Age:

15

Age:Int((Date()-[Date of Birth])/365.2425)

IT Key Ideas, Dynamic Database Systems, 2002

At least 15 years of age

Age:Int((Date()-[D

>=15

IT Key Ideas, Dynamic Database Systems, 2002

Full Names

People’s full names consist of their given name(s) and their surname. To join strings of text together use the & operator. This can be constructed by pointing and clicking on the field names in the Expression Builder.

IT Key Ideas, Dynamic Database Systems, 2002

IT Key Ideas, Dynamic Database Systems, 2002

Full names of females

Save this query!Both with the Sex field and without – in order to re-use the Full Name calculation in future questions!

Full Name:[Given Names]&” “&[Surname] Sex

F

IT Key Ideas, Dynamic Database Systems, 2002

Between 14 and 17 years of ages

Age:Int((Date()-[Date of Birth]

between 14 and 17

IT Key Ideas, Dynamic Database Systems, 2002

Born in the second of 1986

Age:Int((Date()-[Date of Birth]Ascending

between 1/7/86 and 31/12/86

IT Key Ideas, Dynamic Database Systems, 2002

12, 14 or 17 years of age

Age:Int((Date()-[Da

in (12, 14, 17)

IT Key Ideas, Dynamic Database Systems, 2002

Brownish hair

Hair Colour

*brown

IT Key Ideas, Dynamic Database Systems, 2002

Complex Criteria

A query with a criterion (one single criteria) is referred to as a simple query.

A query with two or more criteria is referred to as a complex query.

IT Key Ideas, Dynamic Database Systems, 2002

Complex Criteria

As a complex query involves two or more criteria the conditions are joined through the use of the and or or logical operators.

eg. blue eyes and blonde hair

16 years of age or male

IT Key Ideas, Dynamic Database Systems, 2002

Blue eyes and blonde hair

SELECT Surname, Eye Colour, Hair ColourFROM DetailsWHERE [Eye Colour] = blueAND [Hair Colour] = blonde;

SQL

Hair ColourEye Colour

blue blonde

IT Key Ideas, Dynamic Database Systems, 2002

Blue or hazel eyes

Eye Colour

blue

hazel

IT Key Ideas, Dynamic Database Systems, 2002

Blue eyes or red hair

Hair ColourEye Colour

bluered

IT Key Ideas, Dynamic Database Systems, 2002

Blue eyes and blonde hair and 15

Hair ColourEye Colour

blue not blonde

Age:Int((Dat

15

IT Key Ideas, Dynamic Database Systems, 2002

Blue eyes and not blonde hair

Hair ColourEye Colour

blue not blonde

IT Key Ideas, Dynamic Database Systems, 2002

blue eyes and blonde hair

brown eyes and brownish hair

Matching hair and eyes

Hair ColourEye Colour

blue blondebrown *brown

IT Key Ideas, Dynamic Database Systems, 2002

15 who have either brown or blonde hair

Hair Colour

brown or blonde15

Age:

IT Key Ideas, Dynamic Database Systems, 2002

Brown hair or blonde haired 15 year olds

Hair Colour

brownblonde 15

Age:

IT Key Ideas, Dynamic Database Systems, 2002

Same hair colour and eye colour

SELECT Surname, Hair ColourFROM DetailsWHERE [Hair Colour] = [Eye Colour];

SQL

Hair Colour

[Eye Colour]

Exercise 3.2 - Extensionpage 33

IT Key Ideas, Dynamic Database Systems, 2002

Data File People

Homework/Study

Exercise 3.3page 35-36

IT Key Ideas, Dynamic Database Systems, 2002

table of data page 34

Chapter 3

********************************