Understanding Database Fundamentals
Understanding Database Fundamentals
What is a database?
Data and information
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
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.
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) 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
FROM book
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
Write an SQL statement that selects the FirstName and Surname of all pupils whose
FormTutor is Mrs Krabapple
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”
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.
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