Relational Databases
Download
Report
Transcript Relational Databases
28 Database Theory
28 Database Theory
•
•
•
•
•
Our world depends on databases
Google basically IS a database (index)
Facebook is build around databases
So is e-Bay & Netflix and X-Box Live
Travel agents, schools, doctors and
hospitals all rely on them
• You get the idea!
28 Database Theory
• Databases are so useful because they’re
so good at storing info
• But they do it in a very structured way
• That’s so the data can also be sorted
• And so the data can also be searched
• So overall, what makes databases so
special is how they store, sort & search
• Let’s look at some examples…
28 Database Theory
Store
Search
Sort
28 Database Theory
We’re actually going to look at databases
into two stages.
28 Database Theory
First, we’ll learn the basics of how
databases work:
A Database Tables
B Types of Data
C Relational Databases
28 Database Theory
A Database Tables
• So we’ve said what makes databases so
special is how they store, sort & search
• But how can they do that?
• The secret is that they work in a very
structured or organised way
• This starts with the way that all data is
stored in what are called tables…
28 Database Theory
A Database Tables
Here’s a database table for members of a club...
Table
Field name
Record
Field
28 Database Theory
A Database Tables
Here’s a database table for members of a club...
Table
Field name
Record
Field
28 Database Theory
A Database Tables
Here’s a database table for members of a club...
Table
Field name
Record
Primary Key
Field
28 Database Theory
A Database Tables
Recap
•
•
•
•
•
Databases store info in a structured way
Data in a database is stored in a table
Each row is called a record
Each column is called a field
A special field called the primary key
holds a unique identifier for each record
28
Database
Theory are everywhere!
‘Unique
identifiers’
28 Database Theory
A Database Tables
Recap
Table
All this data is stored in a ??
This is a
??
Field
This is a
??
Recordl
This is the
??
Primary
Key
This is a
??
Field
name
28 Database Theory
A Database Tables
Recap
This table has ____
4
records
There are ____
4 fields
The primary key is called LessonID
_______
28 Database Theory
A Database Tables
B Types of Data
C Relational Databases
28 Database Theory
B Types of Data
• A second secret to how databases
work is the way they handle different
types of data
• Let’s look at an example…
28 Database Theory
B Types of Data
Can you spot the different types of data?
We call these ‘data types’
Text
Numbers
Dates
28 Database Theory
B Types of Data
• Database tables can store different
data types, such as...
– Text
– Numbers
– Dates
• When building a database, you say
what type of data can go in each field
• This can help prevent errors, such as
checking that dates are DD/MM/YY
28 Database Theory
A Database Tables
B Types of Data
C Relational Databases
28 Database Theory
C Relational Databases
• Simple databases with just one data table
are called flat file databases.
• But using only one table creates BIG
problems for larger systems because lots of
data can be duplicated and the database
becomes very inefficient.
• So advanced databases use multiple tables
• They are called relational databases
• Let’s look at why they are needed…
28 Database Theory
C Relational Databases
• Notice that several patients see Dr Brown
• So her details are duplicated.
• This is called redundant data because copies are
inefficient & cause inconsistencies
• Can you spot one?
28 Database Theory
C Relational Databases
PatientID
Primary
Key
DocID
Primary Key
DocID
Foreign Key
28 Database Theory
C Relational Databases
We show this relationship in a diagram
Doctors
Patients
DocID
Surname
Department
Mobile
Patient ID
Forename
Surname
DocID
It’s a ‘One To Many’ relationship
One doctor can treat many patients
28 Database Theory
C Relational Databases
Car Club
• Again, data is duplicated or redundant
• Redundant data causes inconsistencies
• Can you spot one?
28 Database Theory
C Relational Databases
Car Club
SupplierID
1
2
3
1
1
2
SupplierID
1
2
3
28 Database Theory
C Relational Databases
We show this relationship in a diagram
Supplier
Member
SupplierID
CarSupplier
Address1
Town
Postcode
MemberID
Forename
Surname
Car Reg
Car Supplier
It’s a ‘One To Many’ relationship
One Car Supplier can supply many members
28 Database Theory
C Relational Databases
Dog Kennel Business
• Again, data is duplicated or redundant
• Redundant data causes inconsistencies
• Can you spot any?
28 Database Theory
C Relational Databases
Dog Kennel Business
VetID
3
1
2
2
3
2
VetID
1
2
3
Betta Vetz
Top Vets
West Vets
28 Database Theory
C Relational Databases
We show this relationship in a diagram
Vet
Pets
VetID
VetName
Address1
Town
Postcode
PetID
Name
Owner Name
Owner Mobile
Vet ID
It’s a ‘One To Many’ relationship
One Car Supplier can supply many members
28 Database Theory