BTEC NCF IT Unit 02 Creating Systems to Manage Information

BTEC NCF IT Unit 02 Creating Systems to Manage Information
Lesson 04 – Database Keys
Mr C Johnston
Learning Outcome
• Understand the different key types used within a
relational database
• Understand how keys are used within databases to
create working relationships
• Used keys to design working data models and
implemented them in Microsoft Access
STARTER
• Simon has been running trials for a local football team.
He has player details, a list of skills and needs to record
a mark for a player when they complete a skill from the
list. Each player only get ONE trial. Draw an entity
relationship diagram to describe this situation.
• How do we join entities together so that a database
works?
Answer
PLAYER
PLAYER
demonstrates
SKILL
PLAYER SKILL
SKILL
• We use additional attributes called keys to link entities
within a data model so that it will work.
Normally though a player gets more than one trial which makes the model more
complicated but more realistic
PLAYER
PLAYER
TRIAL
TRIAL
SKILL
TRIAL SKILL
SKILL
Database Keys
• Each entity needs a primary key,
• This is an attribute which can be used as a unique identifier
for a single entity occurrence,
▫
studentID could be a primary key for a STUDENT entity
• Entities can also have secondary keys,
• A secondary key is used to make an index of attributes which
are commonly searched or sorted on,
▫
studentName could be a secondary key for the STUDENT set of entities
• keys also help define relationships between entities,
• When a relationship is formed between entities the primary
key of one will appear as a foreign key in another to create a
link.
Database Keys
Example relationship using a primary key and a foreign key
QUESTION
• In Microsoft Access autonumber is typically used as the
data type for an attribute which is going to be a primary
key. Why?
• Is using DOB ideal for a primary key? How about if you
add initials?
• It generates a unique ascending value each time you enter a record
into the database. No two values will ever be the same. Primary
keys must be unique so its perfect.
• Lots of people have the same DOB so NO… Adding initials make it
less likely of a clash but it still happens so NO
RECAP
• Each entity needs a primary key so each record can be
uniquely identified.
• The convention needs to be set so it is truly unique
• Autonumber provides a good solution to stop two (or
more) records having the same ID.
• When there is a one to many relationship the PRIMARY
KEY from the ONE side of the relationship – goes and
lives in the MANY side of the relationship as a foreigner
in the form of a FOREIGN KEY
Example
PLAYER
PLAYER
PK
PlayerID
Autonumber
FirstName
PLAYERSKILL
SKILL
SKILL
PK
SkillID
Autonumber
Short Text
SkillName
Short Text
SurName
Short Text
SkillDescription
Long Text
DOB
Date/Time
Importance
Number
Address #1
Short Text
Address #2
Short Text
TrialDate
Date/Time
PLAYER-SKILL
PK
PlayerSkillID
Autonumber
FK
PlayerID
Number
FK
SkillID
Number
Mark
Number
ACTIVITY
• For each of the ERDs drawn last session
▫
▫
▫
▫
Ferries and their routes
Customers and videos in a rental shop
Students and courses in a school
Patients and medicine in a hospital
produce tables to describe the entities. Include keys,
attribute names and datatypes.
EXTENSION ACTIVITY
• MATHS PROBLEM!!!
▫ Each character takes up 1 byte of storage
▫ Short Text allows for 255 characters therefore it reserves by
default 255 bytes of space
▫ A phone number has 11 digits.
▫ If we store 1306 phone numbers in a database using the default
small text setting - how much space is reserved unnecessarily
and is wasted?
• Add an additional column to your entity models and
suggest a size for each data type… choose wisely so not
to waste space