Entity relationship diagrams
What an Entity Relationship Diagram Is
An entity relationship diagram is a visual tool for planning databases
An ER diagram represents real world things as entities
Attributes describe the details that belong to each entity
Write one sentence that explains what an ER diagram is without using the words “table” or “SQL”. List three possible entities you might find in a school database. For one of your entities, write two attributes it would need.
ER diagrams are often called ERDs, and they are used across schools and industry. They help you plan your database before you write any SQL or create any tables.
+------------------+ +----------------+
| student | ------- | class |
+------------------+ +----------------+
Why ER Diagrams Are Useful
ER diagrams help you organise information before you build a database
A diagram is easier to discuss than a set of table definitions
ER diagrams reduce redesign work later because errors are found early
In one sentence, explain how an ER diagram can prevent mistakes later. Name one person who could give feedback on your diagram, and state what they should check. Write one example of a mistake you might catch early by drawing an ER diagram.
In NCEA work, an ER diagram is evidence that you planned your data structure logically. It also helps you explain your design choices during evaluation.
The Main Parts of an ER Diagram
Every ER diagram uses entities, attributes, and relationships to model a system
Entities represent the main categories of data you store, like student or book
Relationships show how one entity links to another in the real world
Write down one entity, one attribute, and one relationship for a “school club” system. Underline which item is the entity, circle which item is the attribute, and box which item is the relationship. Explain in one sentence what cardinality tells you.
Some diagram styles also show primary keys and foreign keys directly in the diagram. This can make the later table-building step faster, but it is not always required at the planning stage.
entity + attributes + relationship
+--------------------+ borrows +-------------------+
| member | -------------------- | book |
|--------------------| |-------------------|
| PK member_id | | PK book_id |
| name | | title |
+--------------------+ +-------------------+
Understanding Entities
An entity is a thing you store data about in your database
Entity names are usually singular nouns like student, book, or order
An entity contains many records, so you need a way to tell them apart
Choose a system such as “library”, “sports team”, or “online shop”. List three entities for that system. For one entity, write a suitable primary key attribute name.
Some systems need joining entities to handle many-to-many links, such as enrolment between student and class. These extra entities are added when relationships need more structure.
+--------------------+
| student |
|--------------------|
| student_id |
| first_name |
| last_name |
+--------------------+
Understanding Attributes
Attributes are the pieces of information stored for each entity
A good attribute stores one clear value, not a mixed bundle of information
One attribute is chosen as the primary key, which uniquely identifies each record in the entity
Write three attributes for an entity called “book”, and make sure each attribute stores only one value. Identify which attribute should be the primary key, and explain why. Rewrite one bad attribute into two better attributes, such as turning “full_name” into “first_name” and “last_name”.
Sometimes an attribute should be moved into its own entity, such as turning address into an address entity when you need multiple addresses per person. This is a design choice that improves flexibility and reduces duplication.
primary key highlighted
+--------------------+
| student |
|--------------------|
| PK student_id | <-- unique identifier
| first_name |
| date_of_birth |
+--------------------+
Understanding Relationships
Relationships show how two entities are connected in your system
Relationship names often use verbs like borrows, teaches, or belongs_to
A relationship line is drawn between the two entities it connects
Write two entities and a relationship name that connects them, such as “customer places order”. Explain what the relationship means in the real world in one sentence. Predict one question a database designer would ask to confirm the relationship rules.
Some relationships also have their own attributes, such as a borrowed_date on a borrowing relationship. When that happens, the relationship is often modelled as a new entity instead.
+-------------------+ borrows +-------------------+
| member | ------------- | book |
+-------------------+ +-------------------+
Cardinality and Optionality
Cardinality tells you how many records can be linked in a relationship
Optionality tells you whether the link is required or whether it can be missing
Crow’s foot notation shows these rules using symbols at the ends of relationship lines
Decide the cardinality for “teacher teaches class” and justify your choice. Decide whether “class has students” is optional or mandatory on each side, and explain why. Write one example of a relationship that is many-to-many.
If you get cardinality wrong, your database tables will allow the wrong data, such as multiple teachers for a class when only one should be allowed. Checking cardinality is one of the most important quality steps in an ER diagram.
crow's foot style (ascii approximation)
one-to-many:
+---------+ 1 many +---------+
| class | |--------------< | student |
+---------+ +---------+
optional-to-many:
+---------+ 0 many +---------+
| class | O--------------< | student |
+---------+ +---------+
Steps for Drawing an ER Diagram
Start by listing the entities that the system needs to store
Add the attributes each entity needs, and choose a primary key for each one
Draw relationships between entities and name them clearly
For a “cafeteria ordering” system, list three entities and one primary key for each. Add one relationship line between two entities, and write a verb name for it. Choose a cardinality for that relationship and explain your reasoning.
After your first draft, check for duplication, missing entities, and unclear names. A clean second draft is usually much easier to read than the first sketch.
A Worked Example
In a library system, you might store information about members and books
You would create a member entity with attributes like member_id, name, and email
You would then connect member and book with a borrows relationship
Identify the two entities and list two attributes for each. Name the relationship and describe what it represents. State the cardinality on both sides and explain what it allows.
To record each borrowing event, you would usually add a loan entity that stores dates and return status. This turns the relationship into a trackable history, which many real databases need.
member borrows book (with loan entity)
+---------+ 1 many +---------+ many 1 +---------+
| member | |--------------< | loan | >--------------| | book |
+---------+ +---------+ +---------+
Tips for Clear, High-Quality ER Diagrams
Use simple, consistent entity names and keep them singular
Make relationships readable by spacing entities out and avoiding crossed lines
Check your diagram by asking whether it would allow incorrect data, such as duplicate IDs or impossible links
List two design mistakes that would make an ER diagram hard to read. Write one rule you will follow every time you name entities and relationships. Explain how you would check whether your ER diagram matches the real world.
Final Challenge (from scratch): Create an ER diagram for this system using the entities and attributes below. Add relationships, relationship names, cardinality, and optionality. Make sure each entity has a primary key and that your diagram is neat and readable.
System to model: School course enrolments
student: student_id, first_name, last_name, year_level
course: course_id, course_name, credits
teacher: teacher_id, last_name, room
enrolment: enrolment_id, enrol_date
Suggested real-world rules to model (you can adjust if you justify it): students can enrol in many courses, each course has many students, each course is taught by one teacher, and enrolment links a student to a course with a date.
For NCEA, keep your final diagram tidy and easy to explain, because it supports your evidence of planning. A diagram that you can describe clearly is usually a diagram that is designed well.