91热爆

Linked database

Designing relational databases that use two or more linked tables to store data is the way to avoid insert, delete and update anomalies.

It is possible to split the data from the previous example into two different tables. These can be linked them together using a field that is relevant to both tables:

  • a Teacher table could store details on each member of staff - ID, forename, surname, department ID
  • a Department table could store details on each department - department ID, department, phone number
  • both tables are linked because they share a field - department ID

Teacher table

The ID is the primary key and is a unique value - two teachers cannot share the same ID.

IDForenameSurnameDepartment ID
1ColinArthur001
2LauraBrown001
3StephenMacLeod001
4ScottSinclair002
5MichelleWie002
6RossDyett003
7IanAnderson003
8BettyFlood004
ID1
ForenameColin
SurnameArthur
Department ID001
ID2
ForenameLaura
SurnameBrown
Department ID001
ID3
ForenameStephen
SurnameMacLeod
Department ID001
ID4
ForenameScott
SurnameSinclair
Department ID002
ID5
ForenameMichelle
SurnameWie
Department ID002
ID6
ForenameRoss
SurnameDyett
Department ID003
ID7
ForenameIan
SurnameAnderson
Department ID003
ID8
ForenameBetty
SurnameFlood
Department ID004

Department table

Department ID is the primary key and is unique for each department.

Department IDDepartmentPhone number
001ICT300
002English301
003PE302
004Geography303
Department ID001
DepartmentICT
Phone number300
Department ID002
DepartmentEnglish
Phone number301
Department ID003
DepartmentPE
Phone number302
Department ID004
DepartmentGeography
Phone number303

The tables are linked because the primary key (Department ID) in the Department table is also needed in the Teacher table 鈥 so that you know which department each member of staff is part of.

In the Teacher table, Department ID is known as a foreign key.

When you use linked tables in a relational database you reduce the likelihood of insert, update and delete anomalies. In this example, moving to linked tables means:

  • a new department can be added without having to add staff information alongside it.
  • if a member of staff were deleted, such as Betty Flood, you would no longer lose the information held about her department.
  • if the ICT phone number had to change, it would only have to change once in the Department table.