9.Databases (4)
9.Databases (4)
1
Databases
▪ The data can include text, numbers, pictures; anything that can be
stored in a computer.
❖ each record is a row in the table and each field is a column in the table.
4
Databases Table
6
▲ Figure 9.2 Structure of the PATIENT table
Databases
Activity 9.1
State what fields would you expect to find in each record for the doctor’s
appointments and give each field a suitable name. Note: Field names should be a
single word, which should not contain any spaces, for example: BedNumber
7
Validation
For example, the DateOfAdmission field will automatically be checked by the software to
make sure that any data input is a valid date before it can be stored in the PATIENT table.
8
Validation
9
Data Type
» text/alphanumeric
» character
» Boolean
» integer
» real
» date/time
catbird
10
Data Type
▪ A data type classifies how the data is stored, displayed and the operations that can be
performed on the stored value.
▪ For example, a field with an integer data type is stored and displayed as a whole number
and the value stored can be used in calculations.
▪ These database data types are specified in the syllabus.
▪ They are available to use as Access data types, but the names Access uses may be different
from the terms in the syllabus.
11
Data
Data Type
Type
12
Data Type
13
Primary Keys
As each record within a table contains data about a single item, person, or event, it is
important to be able to uniquely identify this item.
In order to reliably identify an item from the data stored about it in a record there needs to
be a field that uniquely identifies the item.
This field is called the primary key.
14
Primary Key
▪ A field that is a primary key must contain data values that are never repeated in the table.
▪ The primary key can be a field that is already used, provided it is unique, for example the
ISBN in the book table.
▪ The PATIENT table would need an extra field for each record as all of the existing fields
could contain repeated data.
▪ To create a primary key, we could add a new field to each record, for example a unique
number could be added to each patient’s record.
▪ The extra field is:
▪ » Primary key field called HospitalNumber
15
Primary Key
16
Primary Key
17
SQL
➢ Structured Query Language (SQL) is the standard query language for writing scripts to obtain
useful information from a database.
➢ SQL is pronounced as se-queue-el.
For example,
somebody needing to visit a patient would only require the ward number and the
bed number of that patient in order to find where they are in the hospital. Whereas
a consultant could need a list of the names of all the patients that they care for.
18
SQL Scripts
▪ An SQL script is a list of SQL commands that perform a given task, often stored in a file so
the script can be reused.
▪ In order to be able to understand SQL and identify the output from an SQL script, you
should have practical experience of writing SQL scripts.
▪ You can write scripts using SQL commands in Access.
▪ There are many other applications that also allow you to do this – MySQL and SQLite are
freely available ones.
▪ When using any SQL application, it is important that you check the commands available
to use as these may differ slightly from those listed in the syllabus and shown below.
19
SQL Scripts
20
SQL Scripts
▪ Only the SELECT and FROM commands are mandatory in an SQL script.
▪ All other commands are optional.
21
SQL Scripts
A SELECT statement takes the form: SELECT Field1, Field2, Field3, etc. – this specifies the
individual fields (columns) to be shown. SELECT * – this specifies that all fields (columns) are to
be shown.
A FROM statement takes the form: FROM TableName – this specifies the table to use. A
WHERE statement takes the form: WHERE Condition – this specifies the condition to apply.
An ORDER BY statement takes the form: ORDER BY Field1, Field2, etc. – this specifies a sort in
ascending or alphabetical order starting with the first field. ORDER BY Field1, Field2 DESC – this
specifies a sort in descending or reverse alphabetical order starting with the first field.
A SUM statement takes the form: SELECT SUM (Field) – this specifies the field (column) for the
calculation. The field should be integer or real.
A COUNT statement takes the form: SELECT COUNT (Field) – this specifies the field (column) to
count if the given criterium is met.
22
SQL Scripts
Conditions often include values from fields, these values need to be stated in a form that matches the data type
for the field.
23
SQL Scripts
24
SQL Scripts
Example 1: Display consultant’s patients
For example, the following SQL command for the PATIENT single-table database would
provide a list of all Mr Smith’s patients showing the hospital number, first name and family
name for each of his patients.
would display:
▲ Figure 9.5 Output from the PATIENT table showing Mr Smith’s patients 25
SQL Scripts
Example 2: Display consultant’s patients in alphabetical order
This SQL command sorts the records in alphabetical order of family name:
would display:
▲ Figure 9.6 Output from the PATIENT table showing Mr Smith’s patients in
26
alphabetical order of family name
SQL Scripts
27
SQL Scripts
28
Practical use of a database
▪ As an IGCSE Computer Science student you need to be able to do the
following:
▪ » define a single-table database from given data storage requirements
▪ » choose a suitable primary key for a database table
▪ » read, complete and understand SQL scripts
b The cub scout leader wants to put each cub into a group called a ‘six’; each ‘six’
can have up to six cubs in it and is given a name for example red, yellow, blue and
green. Add a new text field called Six, and assign each cub to either a red, blue,
yellow or green six.
i Write an SQL query to pick out any cubs in the red six.
ii Write an SQL query to pick out any cubs in the red six or the blue six.
iii Write an SQL query to count the number of cubs in the red six.
c The cub scout leader wants to calculate the number of badges that all the cubs
have been awarded. Add a new integer field called Badges, enter the number of
badges awarded to each cub. Write an SQL query to count the number of badges
awarded to the whole cub group.
30
Activity 9.8
1 A database of students is to be set up with the following fields:
» Family name
» Other names
» Student ID
» Date of Birth
» Date of Entry to School
» Current Class » Current school year/grade
» Email address.
a Select a data type for each field.
b Which fields should be validated, and which fields should be verified?
c Decide the validation rules for those fields which should be validated.
d Which field would you choose for the primary key?
e Choose a suitable format for the student ID.
f Build a database with at least 10 records; include all your validation checks. Ensure there are at least 3
different classes and 2 different years/grades.
g Set up and test SQL scripts to:
i Display Other names, Family and Email address in alphabetical order of family name.
ii Select all the students from each class in alphabetical order.
iii Select all the students for each year/grade and print Other names, Family name and Date of Birth,
31
grouping the students by class