0% found this document useful (0 votes)
24 views

9.Databases (4)

A database is a structured collection of data that allows for efficient information retrieval, consisting of tables made up of records and fields. Databases prevent data duplication and ensure consistency, and they can be used to store information about people, things, and events. SQL is the standard language for querying databases, allowing users to perform operations such as selecting, sorting, and counting data.

Uploaded by

Yoon Nadi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

9.Databases (4)

A database is a structured collection of data that allows for efficient information retrieval, consisting of tables made up of records and fields. Databases prevent data duplication and ensure consistency, and they can be used to store information about people, things, and events. SQL is the standard language for querying databases, allowing users to perform operations such as selecting, sorting, and counting data.

Uploaded by

Yoon Nadi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Databases

1
Databases

▪ A database is a structured collection of data that allows people to


extract information in a way that meets their needs.

▪ The data can include text, numbers, pictures; anything that can be
stored in a computer.

▪ Two Types of Databases: Relational database and Single-table


Database

▪ A single-table database contains only one table


2
Databases

Why are databases useful?


Databases prevent problems occurring because:
» if any changes or additions are made it only has to be done once – data is consistent
» the same data is used by everyone
» data is only stored once in relational databases which means no data duplication.
What are databases used for?
▪ To store information about people, for instance:
➢ patients in a hospital
➢ pupils at a school.
▪ To store information about things, for instance:
➢ cars to be sold
➢ books in a library.
▪ To store information about events, for instance:
➢ hotel bookings
➢ results of races. 3
Databases

Fields and records – the building blocks for any database

• Inside a database, data is stored in tables, which consists of many records.


• Each record consists of several fields.
• The number of records in a table will vary as new records can be added and deleted
from a table as required.
• The number of fields in a table is fixed so each record contains the same number of field

❖ each record is a row in the table and each field is a column in the table.

4
Databases Table

A table contains data about one type of


item or person or event, and will be Each record within a table contains data about
given a meaningful name, for example: a single item, person or event, for example:
» a table of patients called PATIENT » Winnie Sing (a hospital patient)
» a table of books called BOOK » IGCSE Computer Science (a book)
» a table of doctor’s appointments » 15:45 on January 2020 (an appointment).
called APPOINTMENT 5
Databases
As every record contains the same number of fields, each field in a record contains a specific piece of information about the
single item, person or event stored in that record. Each field will have a meaningful name to identify the data stored in it, for
example:
» For a hospital patient the fields could include:
– The patient’s first name field called FirstName
– The patient’s family name field called FamilyName
– The patient’s date of admission field called DateOfAdmission
– The name of the patient’s consultant field called Consultant
– The patient’s ward number field called WardNumber
– The patient’s bed number field called BedNumber, etc.
PATIENT Table

6
▲ Figure 9.2 Structure of the PATIENT table
Databases

» For the table called BOOK the fields could include:


– Title of the book called Title
– Author of the book called Author
– ISBN, etc

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

Basic data types


There are six basic data types :

» text/alphanumeric
» character
» Boolean
» integer
» real
» date/time

catbird

10
Data Type

What is a 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

Conditions also require operators to compare values from fields.

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.

SELECT HospitalNumber, FirstName, FamilyName


FROM PATIENT
WHERE Consultant = 'Mr Smith';

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:

SELECT HospitalNumber, FirstName, FamilyName


FROM PATIENT
WHERE Consultant = 'Mr Smith’
ORDER BY FamilyName;

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

• In order to do this, you will need to use a database


management system.

Boys and girls between the ages of seven and


eleven can join a cub scout group.
(http://en.wikipedia.org/wiki/Cub_Scout).
Each cub scout group needs to keep records
about its members.
Most groups will keep the following
information about each cub in their group
▪ Define a single-table database from given data storage requirements and choose a suitable primary key 29
Activity 9.7
1 a Set up a cub scout database including appropriate validation checks for each
field. Enter data for at least 10 records.

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

You might also like