9 Databases Reduced

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

A.

BETCHOO
O-LEVEL
9 DATABASES

9.1 Databases
9.1.1 Single-table databases
A database is a structured collection of data that allows people to extract information in a way that
meets their needs. Data can include text, numbers and pictures.

For example, a simple database of items for sale could include a description, the price and a picture
of the item.

A single-table database contains only one table. A table consists of many records. The number of
records in a table will vary, as new records can be added and deleted from a table as required. Each
record consists of several fields. The number of fields in table is fixed so each record contains the
same number of fields. An easy way to remember this is:

• each record is a row in the table.


• each field is a column in the table.

Data is often validated when input into a field.

9.1.2 Basic data types


A data type classifies how the data is stored, displayed and the operations that can be performed on
the stored value. Each field requires a data type to be selected.

These are the database data types you need to be able to use.

Data type Description Access data type


Text/alphanumeric A number of characters Short text/long text
Character A single character Short text with a field size of one
One of two values: either True or
Boolean Yes/No
False, 1 or 0, Yes or No
Number formatted as fixed with
Integer Whole number
zero decimal places
Real A decimal number Number formatted as decimal
Date/time Date and/or time Date/Time

1
A. BETCHOO
O-LEVEL
9 DATABASES

9.1.3 Primary keys


Each record within a table contains data about a single item, person or event. It is important to be
able to uniquely identify each record. The primary key is a field that uniquely identifies the record.

Each primary key contains a unique value; it must contain data values that are never repeated in the
table.

9.1.4 Structured Query Language


Structured Query Language (SQL) is the standard query language for writing scripts to obtain
information from a database. An SQL script is a list of SQL commands that perform a given task.

Here are some examples of the SQL statements you need to be able to use.

SQL query statement Description of statement


Fetches specified fields (columns) from a table; queries always begin
SELECT
with SELECT
FROM Identifies the table to use.
WHERE Includes only records (rows) in a query that match a given condition.
Sorts the results from a query by a given column either alphabetically or
ORDER BY
numerically.
SUM Returns the sum of all the values in a field (column). Used with SELECT
Counts the number of records (rows) where the field (column) matches a
COUNT
specified condition. Used with SELECT

SQL Comparison Operators

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

SQL Logical Operators

Operator
AND
OR

2
A. BETCHOO
O-LEVEL
9 DATABASES

Here are some examples of commands.

SELECT ItemDescription, Price


FROM ItemsForSale Displays the description and price of all items
WHERE Price > 10.00 for sale with a price of more than 10.00
ORDER BY Price;
SELECT SUM(Price)
Displays the total value all items for sale with a
FROM ItemsForSale
price of more than 10.00
WHERE Price > 10.00
SELECT COUNT(Price)
Displays the number of items for sale with a
FROM ItemsForSale
price of more than 10.00
WHERE Price > 10.00

Examples of using SQL on the following table:


MovieID Name Genre Certificate Rating
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
Movie

Example using SELECT:


SELECT Name, Rating

FROM Movie

WHERE Rating > 8.4;

The results of this query would be:

Name Rating
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.

3
A. BETCHOO
O-LEVEL
9 DATABASES

Example using Logical Operators


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
• Another logical operator which can be used in the WHERE statement is OR
o For example, WHERE Genre=”Comedy” OR Genre=”Family”

ORDER BY
You can enter a fourth line to the statement using the ORDER BY command, followed by ASC or DESC

• If you enter ASC the results of the query will be sorted in ascending order
• 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

4
A. BETCHOO
O-LEVEL
9 DATABASES

The query has returned four fields and all records because there were no WHERE criteria. The
records are sorted by Name alphabetically.

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

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.

The result of this query would be 25.

Example
SELECT COUNT(*)

FROM ProductTable

WHERE Price > 2;

This query will count all the records with a price greater than 2.

The result of this query would be 3.

This is because there are three products with a price greater than £2 (Chips, Beans, Bananas)

5
A. BETCHOO
O-LEVEL
9 DATABASES

Worked Example 2:
A database table, Dogs2023, is used to keep a record of all dogs registered at a veterinary practice.

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

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

SELECT Name, Breed

FROM Dogs2023

WHERE Gender = “F”;

Write an SQL query to find out the name and breed of all dogs aged 10 years old or older.

SELECT Name, Breed

FROM Dogs2023

WHERE Age >= 10;

Write an SQL query to return the name and breed of all male dogs in ascending order by their
name.

SELECT Name, Breed

FROM Dogs2023

WHERE Gender = “M”

ORDER BY Name ASC;

Write an SQL query to count the number of female dogs aged less than 5 years old.

SELECT COUNT(*)

FROM Dogs2023

WHERE Gender = “F” AND Age < 5;

You might also like