Edexcel International Advanced Level Computer Science, point 4.1.1
Definition and function
An entity-relationship model (ERM) is a plan of the data a database will hold: what things (entities) need to be stored, what facts (attributes) are held about each, and how the things are related. It is drawn before the database is built.
Why needed: it makes you understand the data first, helps you avoid repeating data and spotting errors early, and gives developers and clients a shared picture.
Flat file or relational database?
A flat file keeps all the data in one table, so the same facts are repeated (a customer's address on every one of their orders). That wastes space and causes update errors: change one copy and the others are now wrong. A relational database stores each entity in its own table, linked by keys, so each fact is stored once.
Names of entities and attributes are written in PascalCase: each word starts with a capital letter and there are no spaces, as in OrderLine and CustomerID.
a. Entity
An entity is a thing about which data is stored: a person, object, place or event. It becomes a table. Examples: Customer, Order, Product, Booking.
b. Attribute
An attribute is a fact (property) about an entity. It becomes a column. For Customer: CustomerID, Name, Email, Phone.
c. Keys
| Key | Meaning |
|---|---|
| Primary key | An attribute (or set of attributes) that uniquely identifies each record in a table. It can't be empty or repeated. Example: CustomerID |
| Foreign key | An attribute in one table that holds the primary key of another table, creating the link between them. Example: CustomerID in the Order table |
| Composite key | A primary key made of two or more attributes together, where no single one is unique on its own. Example: (OrderID, ProductID) in an order line table |
Choose a primary key that never changes and is always unique (an ID number), not a name, which can repeat or change.
d. Relationship cardinality
Cardinality says how many records in one entity can relate to records in another.
| Type | Meaning | Example |
|---|---|---|
| One-to-one (1:1) | One record in A relates to at most one in B, and the reverse | Person and Passport |
| One-to-many (1:M) | One record in A relates to many in B, but each B relates to one A | One Customer places many Orders |
| Many-to-many (M:M) | Many in A relate to many in B | Orders and products (an order lists many products, and a product appears on many orders) |
Resolving many-to-many
A relational database can't store an M:M relationship directly. It is split into two one-to-many relationships using a link (junction) table whose primary key is the composite of the two foreign keys:
OrderLine(OrderID, ProductID, Quantity) has the composite key (OrderID, ProductID): one order can't list the same product twice.
e. Ways of representing an ERM
Entity-relationship diagram (ERD)
A diagram: each entity is a box (rectangle), and lines join related entities. The specification's symbols (Appendix 2) show the cardinality at the ends of each line: a plain end means one, and a crow's foot (a three-pronged fork) means many. So a plain line is 1:1, a crow's foot at one end is 1:M, and crow's feet at both ends is M:M.
When you interpret, complete, find or correct errors in an ERD, check:
- Every M:M has been resolved with a link table.
- The many end carries the foreign key.
- Cardinality is right for the scenario.
- Every entity has a primary key.
- There is no repeated data and no missing relationship.
Parenthetical notation
A compact text form: the entity name, then its attributes in brackets, with the primary key underlined and foreign keys marked.
Customer (CustomerID, Name, Email)
Product (ProductID, Description, Price)
Order (OrderID, OrderDate, CustomerID → Customer)
OrderLine (OrderID → Order, ProductID → Product, Quantity)
Here the primary key is underlined, and each foreign key is followed by an arrow to the table it links to. OrderLine's primary key is the composite of its two foreign keys. Exam papers may mark foreign keys differently (for example with an asterisk, CustomerID*), so follow the convention the question gives.
Worked example
Scenario: A library lends books to members. A member can borrow many books over time, and a book can be borrowed by many members over time. Each loan has a date.
- Entities:
Member,Book,Loan(the link). - Relationship:
MemberandBookare many-to-many, so resolve it withLoan. - Keys:
MemberhasMemberID;BookhasBookID;LoanhasLoanIDas primary key, with foreign keysMemberIDandBookID. A composite key(MemberID, BookID)wouldn't work here: the same member can borrow the same book again on another date, so that pair isn't unique. (CompareOrderLine, where one order never lists the same product twice, so its composite key is unique.) - Parenthetical notation:
Member (MemberID, Name, Email)
Book (BookID, Title, Author)
Loan (LoanID, MemberID → Member, BookID → Book, DateOut)
- ERD:
Common error to spot: putting BookTitle in Loan as well as BookID repeats data (the title is already held in Book), and it can go out of step if a title is corrected.
Exam tip: when you complete a diagram, check every many-to-many has been replaced by a link table, and that the foreign key is on the "many" side. In parenthetical notation, mark the primary key and each foreign key as the paper shows. For "why use an ERM", say it avoids repeated data and plans the structure before building.
Check yourself
What is a primary key?
An attribute (or set of attributes) that uniquely identifies each record in a table, and which can't be empty or repeated.
What does a foreign key do?
It holds the primary key of another table, creating a link between the two tables.
How is a many-to-many relationship dealt with in a relational database?
It is split into two one-to-many relationships using a link table, which holds the foreign keys of both tables (often as a composite primary key).
A customer places many orders, but each order is placed by one customer. What is the cardinality, and where does the foreign key go?
One-to-many (customer to order). The foreign key (CustomerID) goes in the Order table.
What is a composite key?
A primary key made of two or more attributes together, where no single attribute is unique on its own.