CIE AS & A Level Computer Science, section 8.1 (Database concepts)
Terminology
| Term | Meaning |
|---|---|
| Entity | a thing about which data is stored (a student, a book) |
| Table | a set of data about one entity, in rows and columns |
| Record (tuple) | one row: all the data about one item |
| Field (attribute) | one column: one piece of data, such as a name |
| Primary key | a field (or fields) that uniquely identifies each record |
| Candidate key | a field that could be the primary key because it is unique |
| Secondary key | a field used for searching, but not unique (and indexed for speed) |
| Foreign key | a field in one table that is the primary key of another table, linking them |
| Relationship | a link between two tables |
| Referential integrity | every foreign key value must match an existing primary key, so there are no "orphan" records |
| Index | a structure that makes searching a field faster |
Types of relationship
- One-to-one (1:1): each record in A matches at most one in B (a person and a passport).
- One-to-many (1:M): one record in A matches many in B (one class has many students). The foreign key goes on the "many" side.
- Many-to-many (M:M): many records in A match many in B (students and courses). It is split into two one-to-many relationships using a link table.
Entity-relationship (E-R) diagram
An E-R diagram shows entities (boxes) and relationships (lines), with the type of relationship marked (a "crow's foot" for the many end).
A member can have many loans, and a book can appear in many loans. The LOAN table is the link table, holding MemberID and BookID as foreign keys.
Example tables
- Member(MemberID, Name, Year)
- Book(BookID, Title, Price)
- Loan(LoanID, MemberID, BookID, DateOut)
Bold marks the primary key, and italic marks a foreign key.
Common mistake: putting the foreign key on the wrong side. In a one-to-many relationship the foreign key goes in the table on the "many" side. Also, do not confuse a candidate key (could be the key) with the primary key (the one chosen).
Exam tip: when drawing an E-R diagram, label each entity and show the relationship type. Resolve many-to-many relationships with a link table. Underline primary keys.
Check yourself
What is a primary key?
A field that uniquely identifies each record in a table.
What is a foreign key?
A field in one table that matches the primary key of another table, linking them.
How is a many-to-many relationship implemented?
By a link table with two one-to-many relationships.