Databases
Databases
Databases
Databases
Single Table Databases
Worked example
How many fields are there in the Dogs table shown above? [1]
Answer: 5
Answer: 6
Exam Tip
It is very likely you will be presented with an example database table and identify either
how many fields there are or how many records there are so make sure you remember a
record is row and a field is a column
Computer Science 2210 By Sajid Ali Imam
Verification checks check whether the data that has been entered is the correct data and
is accurate
o This is often completed by getting data entered by one person is then checked by
another person
When a table is created, validation rules can be assigned to the different fields
o A validation rule controls what data can be entered into that field
o There are different types of validation checks used to limit what data can be entered
into each field
Type Description
This type of validation checks the number of characters that have been entered into
Length
a field. For example, you might make phone numbers entered have to be eleven
Check
characters long
Format This type of validation checks data entered meets an exact format. For example, a
Check product code might have to be two letters followed by five numbers
Range A range check will check the number entered is within a specific range. For example,
Check the age of a dog would be between 0 - 40. Any other number would be disallowed
Presence
A presence check can be added to fields which cannot be left blank
Check
A type check will allow data with a specific data type to be entered into a field. For
Type
example, if text was entered into a field which is supposed to contain the date of
Check
birth of a person it would not be allowed
Check digit validation is a process used to verify the accuracy of numbers such as
credit card numbers. A check digit is a single digit added to the end of the number,
Check
which is calculated based on a specific algorithm applied to the other digits in the
Digits
number. When the data is re-entered, the same algorithm can be applied and if it
produces a different result the code is incorrect
Computer Science 2210 By Sajid Ali Imam
Worked example
A Database Table Containing Student Grades
Describe two validation checks that could be used to check data being inputted into the table
above. [4]
Answer:
StudentID could have a length check to ensure 7 characters are entered [2]
FirstName and LastName could have a presence check to make a record cannot be entered
without entering the name of the student [2]
A type check of boolean could be applied to the Mark Submitted field so that only Y or N
are entered [2]
A range check could be assigned to the Mark column to ensure only numbers between 0
and 100 are entered [2]
Computer Science 2210 By Sajid Ali Imam
Data Types
Worked example
A Database Table Containing Dog Details
Answer: Integer
Answer: Text/Alphanumeric
Computer Science 2210 By Sajid Ali Imam
Primary Keys
Each table has a primary key field which acts as a unique identifier
o Each item of data in this field is unique
o Duplicate data items would be blocked if they were entered into the primary key field
Because the items of data are unique within the primary key field they can be used
to identify individual records
In the example customer table, the primary key would be the CustomerID because each
customer’s ID is unique
If there was a customer with the same name, they could be identified correctly using the
CustomerID
Worked example
Which field would be suitable for the primary key? Explain why [2]
Answer:
DogID
It is a unique identifier
It does not contain any duplicate data items
SQL
Records in a database can be searched and data can be manipulated using Structured
Query Language (SQL)
SQL statements can be written to query the data in the database and extract useful
information
SQL statements follow this structure:
o SELECT the fields you want to display
o FROM the table/tables containing the data you wish to search
o WHERE the search criteria
Example
FROM Movie
WHERE Rating>8.4;
Name Rating
Computer Science 2210 By Sajid Ali Imam
Shaun of the Dead 8.7
Big 8.5
The two fields - Name and Rating have been extracted from the Movie table and then the
records have been filtered by Rating
This example uses the > operator to search for records where the rating is greater than 8.4
There are several other comparison operators which can be used to create the filter
criteria in the WHERE line of a SQL query
Operator Description
Example
FROM Movie
Name Rating
Moana 8.1
The two fields Name and Rating have been extracted from the Movie table and the records
have been filtered by both Genre and Certificate
This query uses the AND logical operator to include multiple criteria in the WHERE line of
the SQL query
Computer Science 2210 By Sajid Ali Imam
Another logical operator which can be used in the WHERE statement is OR
o For example, WHERE Genre=”Comedy” OR Genre=”Family”
Worked example
A database table, Dogs2023, is used to keep a record of all dogs registered at a veterinary
practice
Gende
DogID Name Breed Age
r
Complete the structured query language (SQL) to return the name and breed of all Female
dogs.
________ Dogs2023
Computer Science 2210 By Sajid Ali Imam
WHERE _______; [2]
Answer:
FROM
Gender=”F”
Write an SQL query to find out the name and breed of all dogs aged 10 years old or older. [4]
Answer:
ORDER BY
You can enter a fourth line to the statement using the ORDER BY command, followed by
ASC or DESC
o If you enter ASC the results of the query will be sorted in ascending order
o If you enter DESC the results of the query are sorted in descending order
Example
FROM Movie
The query has returned four fields and all records because there were no WHERE criteria.
The records are sorted by Name alphabetically
Computer Science 2210 By Sajid Ali Imam
o If numbers are sorted in ascending order they go from the lowest number at the top of
the table to the highest number at the bottom
o Descending order is the highest number to the lowest
1 Sausages 1.99 3
2 Chips 2.99 2
3 Beans 2.50 5
4 Bananas 2.10 12
5 Avocado 1.00 3
Example
SELECT SUM(QuantityInStock)
FROM ProductTable;
This query will add up all of the numbers in the QuantityInStock field
o The result of this query would be 25
Computer Science 2210 By Sajid Ali Imam
Example
SELECT COUNT(*)
FROM ProductTable
WHERE Price>2;
This query will count all the records with a price greater than 2
o The result of this query would be 3
o This is because there are three products with a price greater than £2 (Chips, Beans,
Bananas)