9 - Databases New Syllabus 2210 (MT-L)
9 - Databases New Syllabus 2210 (MT-L)
at www.majidtahir.com
Syllabus Content
9- Databases
Candidates should be able to:
Define a single-table database from given data storage requirements
Notes and guidance
fields
records
validation
Suggest suitable basic data types
Notes and guidance
Including:
text/alphanumeric
character
Boolean
Integer
Real
Date/time
Understand the purpose of a primary key and identify a suitable primary key for a
given database table
Read, understand and complete structured query language (SQL) scripts to
query data stored in a single database table
Notes and guidance
Limited to:
SELECT
FROM
WHERE
ORDER BY
SUM
COUNT
Identifying the output given by an SQL statement that will query the given
contents of a database table
What is a Database?
A database, also called electronic database is organized collection of data or
information typically stored electronically in a computer system for storage, rapid search
and retrieval of data.
Field: A database field is a set of data values, of the same data type, in a table. It is also
referred to as a column or an attribute.
Primary Key:
The primary key is a field which uniquely identifies each record in the table. It cannot be
duplicated or contain a null value e.g. Candidate number for a school student in Table. Every
student must have a Candidate number. It cannot a blank value or duplicated.
Relational Databases
Relational Databases use two or more tables linked together
(to form a relationship).
Relational Databases do not store all the data in the same table.
Repeated data is moved into it's own table as shown in the image below:
What is a relationship?
A relationship is formed when our two tables are joined together.
Relationships make use of key fields and primary keys to allow the two tables
to communicate with each other and share their data.
Key fields are identified using a primary key as shown in the image below:
Once the tables are linked together each one can read data from the other.
This means that we only need to enter the details of each doctor once instead of
many separate entries.
How do you form the relationship? (Link the tables)
In order to link the tables we need to use a common field.
The foreign key (Doctor Id in the patient table) can then be used to match to the
primary key (Doctor Id in the doctor table) and share the correct data.
For example:-
A patient with a Doctor Id 01 will be automatically assigned to Doctor Hyde & Room 03.
They are available to use as Access data types, but the names Access uses may be different.
Validation:
The role of validation was discussed in earlier chapters. Some validation checks will be
automatically provided by the database management software that is used to construct
and maintain the database.
Other validation checks need to be set up by the database developer during the
construction of the database.
There are many different types of validation checks including:
range checks
length checks
type checks
presence checks
format checks
Database Queries:
Queries allow users to search and sort data held in a database. There are two different
ways to create queries.
The first is to use the built in query generator that comes with most database software.
The example below shows a complex query searching two tables to find any student
named Smith who is taught Spanish.
The query below has been created using an in built query generator.
More advanced users may want to create some code to perform queries.
Explain why the query-by-example is incorrect, and write a correct query-by-example. Explanation
...............................................................................................................................
Answers:
Queries in SQL:
Databases allow us to store and filter data to find specific information. A database can
be queried using a variety of methods, although this depends on the software you are
using.
Databases can use query languages or graphical methods to interrogate the data.
Query Language:
Query language is a written language used only to write specific queries. This is
a powerful tool as the user can define precisely what is required in a database. SQL is a
popular query language used with many databases.
Boolean Operators:
Boolean operators are used to filter databases using AND, OR or NOT. They can
search multiple fields at the same time to help us retrieve the data that we need.
AND
AND is used to search records that contain one piece of information AND another.
A query for the words brown AND shoes would return results that contain the words
brown and shoes.
In general, search engines treat the query brown shoes as brown AND shoes,
which means that all results will contain both words, eg brown trousers and red
shoes for sale.
OR
OR is used to search for records that contain EITHER one piece of information OR
another.
for example black shoes OR white shoes. This would present results for any shoes
that were black or white.
Most search engines use the OR function best if the search statements are defined by
speech marks, eg "brown shoes" OR "black jeans" would show pages which
either contain brown shoes or black jeans.
NOT
NOT is used to exclude results.
The query shoes NOT brown will return results that contain the word shoes but
NOT the word brown.
Arithmetic operators
A query can also be performed using arithmetic operators. These help to make
specific searches related to numerical data.
Operator Meaning
= Equals
Queries are useful for searching for specific conditions. You might want to find
entertainment programmes on BBC3. A query for these conditions would look like this:
You may want a programme that is less than 20 minutes long or is a nature programme.
A query for these conditions would look like this:
The SQL code needed would look like the example below:
the SELECT statement states which fields to look at - the Title and Duration fields
the FROM statement states which table to look at - Programmes
the ORDER BY statement sorts the Duration field in ascending order by default
This table shows the results from this query:
Duration Title
Duration Title
25 Blue Peter
30 EastEnders
50 Newsnight
60 Wild Brazil
75 The Voice
90 Sherlock
01 EastEnders Drama 30
06 Sherlock Drama 90
The following code searches the Title field of the table to find the words 'The Voice'.
Alternatively, you could find all of the programmes which are less than 30 minutes long
using this code:
Wildcards
The wildcard uses the * symbol, and is used in place of any number of unknown
characters. For example, the following code searches for all programmes with the
letter i in the title:
SELECT * FROM Programmes
WHERE Title LIKE “*i*” ;
COUNT() Syntax
The COUNT() function returns the number of rows that matches a
specified criterion.
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
DEMO DATABASE
Table Products
SUM() Syntax
The SUM() function returns the total sum of a numeric column.
SELECT SUM(column_name)
FROM table_name
WHERE condition;
Demo Database
Below is a selection from the "Order" table in the sample database:
References:
https://www.bbc.com/education/guides/z37tb9q/revision/8
https://www.w3schools.com/sql/sql_count_avg_sum.asp
Cambridge (Hodder) O level Book by David Watson & Helen Williams