Databases

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Computer Science 2210 By Sajid Ali Imam

Databases
Single Table Databases

 A database is a structured collection of data so it can be searched, sorted,


filtered and analysed quickly
o Data in a database can be any type of data including text, images, videos, sound
 Databases use tables to store data
 Tables have records of data represented by one row
o In the example below, each row represents the data stored about a single customer (the
customer’s record)
o In the customer table there are 3 records
o Each record is divided into fields (CustomerID, FirstName, LastName, DOB and Phone
Number)

A Database Table Containing Customer Details

CustomerID FirstName LastName DOB PhoneNumber

1 Andrea Bycroft 05031976 0746762883


2 Melissa Langler 22012001 0756372892
3 Amy George 22111988 0746372821

 Fields are represented by the columns in a table


o There are 5 fields in the customer table
o The first row in a table contains the field names which is the heading for the data stored
in that field
o Each field in a table has a data type which defines what data can be entered into that
field
Computer Science 2210 By Sajid Ali Imam

Worked example

A Database Table Containing Pet Details

DogID Name Breed Age Gender

DG12 Smokey Poodle 12 M


DG34 Harvey Springer Spaniel 10 M
DG48 Maisie Labradoodle 3 F
DG49 Maggie Labradoodle 3 F
Winsto
DG88 Bulldog 7 M
n
DG95 Shona Golden Retriever 6 F

How many fields are there in the Dogs table shown above? [1]

Answer: 5

How many records are there in the Dogs table? [1]

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

Validation and Verification

 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

The different types of validation check

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

StudentID FirstName LastName MarkSubmitted Percentage

1483792 Shanay Giles Y 55


1498378 Poppy Petit N 20
1500121 Diya Dinesh Y 74
1382972 Joe Swaile Y 68
1598264 Anton Smith Y 34
1548282 Felicity Hall N 47

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

 Table fields are represented by columns in a table


 There are 5 fields in the customer table below
o These include CustomerID, FirstName, LastName, DOB and PhoneNumber

A Database Table Containing Customer Details

CustomerID FirstName LastName DOB PhoneNumber

1 Andrea Bycroft 05031976 0746762883


2 Melissa Langler 22012001 0756372892
3 Amy George 22111988 0746372821

 Each field in a table, has a data type


o Common data types include text/alphanumeric, character, boolean,
integer, real and date/time
o Phone numbers have to be assigned the text/alphanumeric data type because they
begin with a 0
 If you assigned the data type Integer to a phone number it would remove the initial 0

Database Data Types

Data Type Explanation Example

This data type allows letters, special characters like


Text/Alphanumeric spaces and punctuation and numbers to be entered NG321AE
into a field
This allows single characters to be entered into a field.
Character Characters can be any alphanumeric value and can be A
lowercase or uppercase
This data type can be used in fields where there are
only two possible options. Data is stored as a 1 or 0 in
Boolean True/False
the database but can be used to represent True/False
or Yes/No
Integer Only whole numbers can be entered 15
Real Numbers including decimal numbers can be stored 30.99
Computer Science 2210 By Sajid Ali Imam
Only dates or times can be entered into a field with
Date/Time this type. A format for the date/time can also be 180855
assigned to the field

Worked example
A Database Table Containing Dog Details

DogID Name Breed Age Gender

DG12 Smokey Poodle 12 M


Springer
DG34 Harvey 10 M
Spaniel
DG48 Maisie Labradoodle 3 F
DG49 Maggie Labradoodle 3 F
DG88 Winston Bulldog 7 M
Golden
DG95 Shona 6 F
Retriever

What data type should Age be? [1]

Answer: Integer

What data type should DogID be? [1]

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

A Database Table Containing Customer Details

CustomerID FirstName LastName DOB PhoneNumber

1 Andrea Bycroft 05031976 0746762883


2 Melissa Langler 22012001 0756372892
3 Amy George 22111988 0746372821

 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

A database table containing the details of dogs

DogID Name Breed Age Gender

DG12 Smokey Poodle 12 M


DG34 Harvey Springer Spaniel 10 M
DG48 Maisie Labradoodle 3 F
DG49 Maggie Labradoodle 3 F
DG88 Winston Bulldog 7 M
DG95 Shona Golden Retriever 6 F
Computer Science 2210 By Sajid Ali Imam

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

A Database Table Containing Movie Details

MovieI Name Genre Certificate Rating


D
M23 Moana Family U 8.1
M8 Shaun of the Dead Comedy 18 8.7
M56 Die Hard Action 18 8.4
M34 Big Family PG 8.5

Example

SELECT Name, Rating

FROM Movie

WHERE Rating>8.4;

The results of this query would be:

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

SQL Comparison Operators

Operator Description

> Greater than


>= Greater than or equal to
< Less than
<= Less than or equal to
= Equal to
<> Not equal to

Example

SELECT Name, Rating

FROM Movie

WHERE Genre=”Family” AND Certificate=”U”;

The results of this query would be:

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

DG12 Smokey Poodle 12 M


Springer
DG34 Harvey 10 M
Spaniel
DG48 Maisie Labradoodle 3 F
DG49 Maggie Labradoodle 3 F
DG88 Winston Bulldog 7 M
Golden
DG95 Shona 6 F
Retriever

Complete the structured query language (SQL) to return the name and breed of all Female
dogs.

SELECT Name, Breed

________ 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:

 SELECT Name, Breed


 FROM Dogs2023
 WHERE Age>=10;

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

SELECT Name, Genre, Certificate, Rating

FROM Movie

ORDER BY Name ASC

 The results of this query would be:

Name Genre Certificate Rating

Big Family PG 8.5


Die Hard Action 18 8.4
Moana Family U 8.1
Shaun of the Dead Comedy 18 8.7

 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

SUM and COUNT commands

 The SUM command can be used to add numerical data


 The COUNT command can be used to count items of data

ProductID ProductName Price QuantityInStock

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)

You might also like