0% found this document useful (0 votes)
27 views47 pages

Understanding Database Fundamentals

The document discusses databases and their components. A database is a collection of data organized into tables with rows and columns. Each table contains information about an entity or object. Relational databases link tables together through primary and foreign keys to reduce data redundancy and inconsistency. This allows complex databases to efficiently store and retrieve related information across multiple tables.

Uploaded by

neil.king2010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views47 pages

Understanding Database Fundamentals

The document discusses databases and their components. A database is a collection of data organized into tables with rows and columns. Each table contains information about an entity or object. Relational databases link tables together through primary and foreign keys to reduce data redundancy and inconsistency. This allows complex databases to efficiently store and retrieve related information across multiple tables.

Uploaded by

neil.king2010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Databases

What is a database?
Data and information

Data are facts, values and observations


Information is data about objects that have been processed to make
them useful in a particular context
What is a database?
A database is a collection of data stored in an organised and logical way
Data are stored in tables with rows and columns
Each table contains information about an entity
An example of a table is given below.
StudentID FirstName Surname DateOfBirth FormTutor
712 Bart Simpson 1/4/2010 Principal Skinner
423 Lisa Simpson 20/5/2012 Mrs Krabapple
917 Ralph Wiggum 16/6/2010 Mrs Krabapple
124 Nelson Muntz 14/9/2009 Principal Skinner
291 Milhouse van Hauten 19/12/2009 Principal Skinner
What is a database entity?
A database entity is an object, person, item or thing about which you
want the data stored.
Person entity Object entity Item entity

Customer Book Sale transaction


Employee Car Appointment
Student House
Teacher
Data Types
Data can be stored in any form, here are some examples:
Text – characters, letters, numbers, symbols
Integer
Float
Date/time – dates and times
Boolean – Yes/no or true/false
Task: Suggest appropriate data types for each
of these fields in the following patient table
Patient table
FirstName Height DateOfBirth RestingHeartRate HadFluVaccination?
Bart 1.35 1/4/2010 70 True
Lisa 1.16 20/5/2012 65 True
Ralph 1.05 16/6/2010 75 False
Nelson 2.00 14/9/2009 60 False
Milhouse 1.50 19/12/2009 65 True

 First name: Text


 Height: Float/real
 Date of Birth: Date
 Resting heart rate: Integer
 Had Flu Vaccination?: Boolean
Database index
A database index allows for quick speed of retrieval of data from
searches of tables
The index is a separate file that has a sorted column of values that
link to records in a table
What is a Record?
A records is a single row in a table that can have data stored as 1 or more fields
(columns)
A record needs to be uniquely identifiable and needs an entity identifier which in this
example is Student ID
A table contains multiple records
The example below contains 5 records
StudentID FirstName Surname DateOfBirth FormTutor
712 Bart Simpson 1/4/2010 Principal Skinner
423 Lisa Simpson 20/5/2012 Mrs Krabapple
Records

917 Ralph Wiggum 16/6/2010 Mrs Krabapple


124 Nelson Muntz 14/9/2009 Principal Skinner
291 Milhouse van Hauten 19/12/2009 Principal Skinner
What do we mean by unique?
StudentID FirstName Surname DateOfBirth FormTutor
712 Bart Simpson 1/4/2010 Principal Skinner
423 Lisa Simpson 20/5/2012 Mrs Krabapple
917 Ralph Wiggum 16/6/2010 Mrs Krabapple
124 Nelson Muntz 14/9/2009 Principal Skinner
291 Milhouse van Hauten 19/12/2009 Principal Skinner

 The StudentID field contains unique values for each record; each value
is different
 The Surname field does not contain unique values. For instance,
Simpson appears twice.
A field / attribute is the columns of the database table and refer to the
What is a characteristics of a record. For instance the fields of the table below
include:
field?  Student ID
 First name
 Surname
 Date of Birth
 Form tutor

field field field field field

StudentID First name Surname Date of Birth Form Tutor


712 Bart Simpson 1/4/2010 Principal Skinner
423 Lisa Simpson 20/5/2012 Mrs Krabapple
917 Ralph Wiggum 16/6/2010 Mrs Krabapple
124 Nelson Muntz 14/9/2009 Principal Skinner
291 Milhose van Hauten 19/12/2009 Principal Skinner
Task
Student ID FirstName Surname Age FormTutor
712 Bart Simpson 10 Principal Skinner
423 Lisa Simpson 8 Mrs Krabapple
917 Ralph Wiggum 10 Mrs Krabapple
124 Nelson Muntz 11 Principal Skinner

a) What is the most suitable data type for the Age field in the table?
b) How many records are there in the table?
c) How many fields are there in the table?
d) Why is it better to replace the Age field with date of birth in the table?
Data redundancy occurs when the same data are stored in
Data redundancy multiple places and so we have repeating data.

Notice how the Author Name fields (and other fields) are
repeated. This means that more space is needed to store
an author name several times which is not efficient.

BookID Title FirstName Surname YearPublished Publisher Genre


1 Fantastic Beasts and Where to Find Them Joanne Rowling 2001 Bloomsbury Fantasy
2 Harry Potter and the Chamber of Secrets Joanne Rowling 1998 Bloomsbury Fantasy
3 Harry Potter and Order of the Phoenix Joanne Rowling 203 Bloomsbury Fantasy
4 The BFG Roald Dahl 1982 Penguin Fantasy
5 Going Solo Roald Dahl 1986 Jonathan Cape Autobiography
6 Danny Champion of the World Roald Dahl 1975 Jonathan Cape Children
7 War Horse Michael Morpurgo 1982 Kaye & Ward Historical fiction
8 Private Peaceful Michael Morpurgo 2003 HarperCollins Historical fiction
Data inconsistency occurs when data pertaining to the
Data Inconsistency same object are in fact stored in a different format.

For instance JK. Rowling and Joanne Rowling refer to the


same person, but the database may record these as two
separate authors.

BookID Title FirstName Surname YearPublished Publisher Genre


1 Fantastic Beasts and Where to Find Them JK Rowling 2001 Bloomsbury Fantasy
2 Harry Potter and the Chamber of Secrets Joanne Rowling 1998 Bloomsbury Fantasy
3 Harry Potter and Order of the Phoenix Joanne Rowling 203 Bloomsbury Fantasy
4 The BFG Roald Dahl 1982 Penguin Fantasy
5 Going Solo Roald Dahl 1986 Jonathan Cape Autobiography
6 Danny Champion of the World Roald Dahl 1975 Jonathan Cape Children
7 War Horse Michael Morpurgo 1982 Kaye & Ward Historical fiction
8 Private Peaceful Michael Morpurgo 2003 HarperCollins Historical fiction
Relational Databases
Complex databases can be made up of multiple tables linked together
by shared values called a key.
These relational databases makes it easier to search and find
information that you want.
Reduces the amount of duplication (redundancy) of data
Reduces inconsistencies in the data
Keys

Primary key – All tables have a field that is the primary key and
uniquely identifies each record. This is also known as entity identifier
Foreign key – These are primary keys that are held as fields in other
tables to cross reference data between tables. They allow tables to be
linked together
Linking tables

Book Entity
BookID
Author Entity Title
Author ID Author ID
Surname Publisher
Firstname Year
Literary Agent Genre

AuthorID is primary key in Author table and is used to cross reference with the
AuthorID in the book table which is the foreign key so the two tables can be linked
Multiple Tables Primary key
AuthorID FirstName
Author Table
Surname LiteraryAgent
Use more than 1 table to
1 Joanne Rowling Neil Blair
reduce data redundancy and
inconsistencies 2 Roald Dahl David Higham Associates
3 Michael Morpurgo David Higham Associates

Foreign
key Book Table
BookID AuthorID Title YearPublished Publisher Genre
1 1 Fantastic … 2001 Bloomsbury Fantasy
2 1 … Chamber of Secrets 1998 Bloomsbury Fantasy
3 1 … Order of the Phoenix 203 Bloomsbury Fantasy
4 2 The BFG 1982 Penguin Fantasy
5 2 Going Solo 1986 Jonathan Cape Autobiography
6 2 Danny Champion … 1975 Jonathan Cape Children
7 3 War Horse 1982 Kaye & Ward Historical fiction
8 3 Private Peaceful 2003 HarperCollins Historical fiction
Task

Teacher Table
FormTurorID Title Surname Room
a) Which field is the foreign 10 Principal Skinner 101
key in the Student table? 12 Mrs Krabapple 102

b) Which field is the primary


key in Teacher table? Student Table
StudentID FirstName Surname Age FormTutorID
712 Bart Simpson 10 10
423 Lisa Simpson 8 12
917 Ralph Wiggum 10 12
124 Nelson Muntz 11 10
291 Milhouse van Hauten 11 10
Match the keyword to the definition

A. Index A single row in a table

B. Record An object person or thing about which data are collected

C. Primary Key Allows for quick speed of retrieval of data


D. Foreign Key Columns of a table and refer to the characteristics of a record
E. Field
A field that uniquely identifies each record
F. Entity
Used to cross reference data between tables
Task

a) What is the most suitable data type for Owner Table


the Email field in the Owner table? Owner ID First name Surname Email Phone

b) What is the most suitable data type for 28 Fred Flintstone [email protected] 07763 826374
83 Brian Griffin [email protected] 07239 459672
the Make field in the Car table?
41 Popeye Sailor [email protected] 07193 948392
c) Which field is the foreign key in the Car
table?
Car Table
d) Which field is the primary key in Owner
Registration Make Model Colour Owner ID
table?
e) How many records are there in the GH21 BBN Ferrari F8 Red 28
Owner table? CN13 JJK McLaren 720S Green 83

f) How many records are there in the Car SD16 ASD Honda Prius Black 83
table? RE17 CVB Ford GT Blue 41
g) Why is car registration a good primary UY28 FDK Aston Martin Vanquish Blue 41
key?
Structured Query Language
Learning objectives:
 Use SQL to retrieve data from a database
Database case study: book table
Book Title Author Year Publisher Genre
ID Published
1 Fantastic Beasts and Where to Find Them JK Rowling 2001 Bloomsbury Fantasy
2 Harry Potter and the Chamber of Secrets JK Rowling 1998 Bloomsbury Fantasy
3 Harry Potter and Order of the Phoenix JK Rowling 2003 Bloomsbury Fantasy
4 The BFG Roald Dahl 1982 Penguin Fantasy
5 Going Solo Roald Dahl 1986 Jonathan Cape Autobiography
6 Danny Champion of the World Roald Dahl 1975 Jonathan Cape Children
7 War Horse Michael Morpurgo 1982 Kaye & Ward Historical fiction
8 Private Peaceful Michael Morpurgo 2003 HarperCollins Historical fiction

We will use this database table in many of the examples that follow.
SELECT
To retrieve data from a database
SELECT – To retrieve data from a table

To retrieve all records data from the


table we can use the SELECT 1 Fantastic Beasts and JK Rowling 2001 Bloomsbury Fantasy
Where to Find Them
statement with the wild card operator
2 Harry Potter and the JK Rowling 1998 Bloomsbury Fantasy
*. Chamber of Secrets
3 Harry Potter and JK Rowling 2003 Bloomsbury Fantasy
Order of the Phoenix
4 The BFG Roald Dahl 1982 Penguin Fantasy
SELECT *
FROM tableName 5 Going Solo Roald Dahl 1986 Jonathan Cape Autobiography

6 Danny Champion of Roald Dahl 1975 Jonathan Cape Children


the World

EXAMPLE 7 War Horse Michael


Morpurgo
1982 Kaye & Ward Historical fiction

SELECT * 8 Private Peaceful Michael 2003 HarperCollins Historical fiction


Morpurgo
FROM book
SELECT statement

We can also choose the fields that we wish to retrieve

SELECT field1, field2, …


FROM tableName Fantastic Beasts and Where to Find Them JK Rowling
Harry Potter and the Chamber of Secrets JK Rowling
Harry Potter and Order of the Phoenix JK Rowling
EXAMPLE
The BFG Roald Dahl
SELECT Author, Title
Going Solo Roald Dahl
FROM book
Danny Champion of the World Roald Dahl
War Horse Michael Morpurgo
Private Peaceful Michael Morpurgo
Sort with ORDER BY clause
We can sort the output of our SELECT statement by using the ORDER BY clause. ASC and DESC refer
to sorting ascending and descending alphabetically and numerically of a specified field.

ORDER BY fieldname ASC|DESC


Danny Champion of the World Roald Dahl
Fantastic Beasts and Where to Find Them JK Rowling
EXAMPLE – Sort ascending Going Solo Roald Dahl
Harry Potter and the Chamber of Secrets JK Rowling
SELECT Author, Title
Harry Potter and Order of the Phoenix JK Rowling
FROM book Private Peaceful Michael Morpurgo
The BFG Roald Dahl
ORDER BY Title ASC
War Horse Michael Morpurgo
Sort – Descending example

SELECT Author, Title War Horse Michael Morpurgo


The BFG Roald Dahl
FROM book
Private Peaceful Michael Morpurgo
ORDER BY Title DESC Harry Potter and Order of the Phoenix JK Rowling
Harry Potter and the Chamber of Secrets JK Rowling
Going Solo Roald Dahl
Fantastic Beasts and Where to Find Them JK Rowling
Danny Champion of the World Roald Dahl
WHERE clause

We can filter our selection using the WHERE clause


WHERE fieldname operator value
Operator Description
= Value equal to
!= Value not equal to
< Value less than
> Value greater than
<= Value less than or equal to
>= Value greater than or equal to
SELECT using WHERE clause
EXAMPLE 1 – Select books written since 2000

SELECT Title, Author, yearPublished


Fantastic Beasts and Where to Find Them JK Rowling 2001
FROM book Harry Potter and Order of the Phoenix JK Rowling 2003
WHERE YearPublished > 2000 Private Peaceful Michael Morpurgo 2003

EXAMPLE 2 – Select books written by Michael Morpurgo. Notice how the


author name is in speech marks because it is a string datatype.

SELECT Title, Author

FROM book

WHERE Author = “Michael Morpurgo” War Horse Michael Morpurgo


Private Peaceful Michael Morpurgo
NB for data type date you need to use #. Eg
WHERE Date < #1/1/2010#
Boolean operators with WHERE clause
We can use Boolean and relational operators with the WHERE clause if we
have multiple conditions that need to be met.
Operator Description
OR Allows us to combine multiple conditions. Any of the conditions can be true
AND Allows us to combine multiple conditions. All conditions need to be true
NOT Reverses the value of a condition. If it is true it will be false and vice versa

EXAMPLE – Select all books written by Michael Morpurgo since 2016

Private Peaceful Michael Morpurgo


SELECT Title, Author FROM book
WHERE Author=“Michael Morpurgo” AND YearPublished > 2000
Task: School DB

1) Write an SQL statement that selects the FirstName and Surname of all pupils whose form tutor is Principal Skinner

2) Write an SQL statement that selects the FirstName and Surname of all pupils whose date of birth is later than 1/1/10

3) Write an SQL statement that selects the FirstName and Surname of all pupils whose date of birth is later than 1/1/10
and whose form tutor is Principal Skinner

4) Write an SQL statement that selects the FirstName and Surname of all pupils whose date of birth is later than 1/1/10
and whose form tutor is Principal Skinner and displays them descending order by surname
Answers: School DB
1) Write an SQL statement that selects the FirstName and Surname of all pupils whose form tutor is Principal Skinner
SELECT FirstName, Surname FROM student WHERE FormTutorID=10
2) Write an SQL statement that selects the FirstName and Surname of all pupils whose date of birth is later than
1/1/10
SELECT FirstName, Surname FROM student WHERE DateOfBirth > #1/1/10#
3) Write an SQL statement that selects the FirstName and Surname of all pupils whose date of birth is later than
1/1/10 and whose form tutor is Principal Skinner
SELECT FirstName, Surname FROM student
WHERE FormTutorID = 10 AND DateOfBirth > #1/1/10#
4) Write an SQL statement that selects the FirstName and Surname of all pupils whose date of birth is later than
1/1/10 and whose form tutor is Principal Skinner and displays them descending order by surname
SELECT FirstName, Surname FROM student
WHERE FormTutorID=10 AND DateOfBirth > #1/1/10#
Starter

StudentID FirstName Surname DateOfBirth FormTutor


712 Bart Simpson 1/4/2010 Principal Skinner
423 Lisa Simpson 20/5/2012 Mrs Krabapple
917 Ralph Wiggum 16/6/2010 Mrs Krabapple
124 Nelson Muntz 14/9/2009 Principal Skinner
291 Milhouse van Hauten 19/12/2009 Principal Skinner

Write an SQL statement that selects the FirstName and Surname of all pupils whose
FormTutor is Mrs Krabapple

SELECT FirstName, Surname FROM students WHERE


FormTutor=“Prinicipal Skinner”
Structured Query Language
Learning objectives:
 Use SQL to insert data into a database
 Use SQL to update database records
 Use SQL to delete database records
Database case study: book table
Book Title Author Year Publisher Genre
ID Published
1 Fantastic Beasts and Where to Find Them JK Rowling 2001 Bloomsbury Fantasy
2 Harry Potter and the Chamber of Secrets JK Rowling 1998 Bloomsbury Fantasy
3 Harry Potter and Order of the Phoenix JK Rowling 2003 Bloomsbury Fantasy
4 The BFG Roald Dahl 1982 Penguin Fantasy
5 Going Solo Roald Dahl 1986 Jonathan Cape Autobiography
6 Danny Champion of the World Roald Dahl 1975 Jonathan Cape Children
7 War Horse Michael Morpurgo 1982 Kaye & Ward Historical fiction
8 Private Peaceful Michael Morpurgo 2003 HarperCollins Historical fiction

We will use this database table in many of the examples that follow.
UPDATE
To update records in a database

Open up book.accdb and copy the teacher examples as we go through them and complete
the tasks.
Updating a record
To make changes to a record that is already in a table we can use the UPDATE statement.

EXAMPLE 1: Update the book table to change the genre of all fields to Children

UPDATE book
SET Genre=“Children”

EXAMPLE 2: Update the book table to change the author name from JK Rowling to Joanne Rowling.

UPDATE book
SET Author=“Joanne Rowling”
WHERE Author=“JK Rowling”
Updating a record
UPDATE book SET Genre=“Children”
UPDATE book SET Author=“Joanne Rowling” WHERE Author=“JK Rowling”

Book Title Author Year Publisher Genre


ID Published
1 Fantastic Beasts and Where to Find Them Joanne Rowling 2001 Bloomsbury Children
2 Harry Potter and the Chamber of Secrets Joanne Rowling 1998 Bloomsbury Children
3 Harry Potter and Order of the Phoenix Joanne Rowling 2003 Bloomsbury Children
4 The BFG Roald Dahl 1982 Penguin Children
5 Going Solo Roald Dahl 1986 Jonathan Cape Children
6 Danny Champion of the World Roald Dahl 1975 Jonathan Cape Children
7 War Horse Michael Morpurgo 1982 Kaye & Ward Children
8 Private Peaceful Michael Morpurgo 2003 HarperCollins Children
Task: Set all the genres back to how they
were originally
EXAMPLE
UPDATE book
SET Genre=“Fantasy”
WHERE Author=“JK Rowling”

You will need to make quite a few UPDATE statements to change the table to how it was
INSERT
To add records to a database
Adding new records
INSERT INTO is a commonly used command in SQL for adding new records to database tables. To insert all attributes for a table we can
use:
INSERT INTO table (field1,field2,…) VALUES(value1,value2,…)
The values correspond to the fields in the table i/e.:
 Field 1: Book ID
 Field 2: Title
 Field 3: Author
 Field 4: YearPublished
 Field 5: Publisher
 Field 6: Genre

EXAMPLE
INSERT INTO book (Title,Author,YearPublished,Publisher,Genre)
Task: Adding new records
EXAMPLE
INSERT INTO book (Title,Author,YearPublished,Publisher,Genre)
VALUES (‘Boy’, ‘Roald Dahl’, 1984, ‘Penguin’, ‘Autobiography’)

Write the command to insert the following record into the books table:
 Title: The Boy in the Striped Pyjamas
 Author: John Boyne
 Year Published: 2006
 Publisher: David Fickling Books
 Genre: Historical Fiction
Task: Adding new records

EXAMPLE
INSERT INTO book (Title, author, genre) VALUES (‘The
BFG’, ‘Roald Dahl’, ‘Fantasy’)

Write the command to add the following record into the book table:
Title: Harry Potter and the Prisoner of Azkaban
Author: JK Rowling
DELETE
To remove records from a database
Deleting a record
To delete a record we specify which record(s) from which table we
wish to remove.

DELETE FROM table WHERE condition


WHERE clause
The WHERE clause is used to filter records so that we do not apply a statement to a whole table
EXAMPLE 1: Remove all books
DELETE FROM book
DELETE * FROM book

EXAMPLE 2: Remove all books written by JK Rowling:


DELETE FROM book WHERE Author=‘JK Rowling’

EXAMPLE 3: Remove all books written by Michael Morpurgo and written before 2000
DELETE FROM book
Task: Deleting records

EXAMPLES
DELETE FROM book WHERE Author=“JK Rowling”
DELETE FROM book WHERE Author=“Michael Morpurgo”
AND YearPublished < 2000

a) Write a statement to remove all records with books written by Roald Dahl
b) Write a statement to remove all records with books written before 1990
c) Write a statement to remove all records with books published by Penguin
d) Write a statement to remove all records with books written by Roald Dahl and published by Penguin
e) Write a statement to remove all records with books written by JK Rowling and published before 2003

You might also like