The ER model is a way to draw a database before you build it. You list the things you want to store (entities), their details (attributes) and how they connect (relationships).
Entities and entity sets
An entity is a real-world thing that can be told apart from others, such as one student or one course. An entity set is a collection of similar entities, such as all students. In a diagram, an entity set is a rectangle.
Attributes
An attribute is a property of an entity. In a diagram it is an oval joined to its entity.
| Type | Meaning | Example |
|---|---|---|
| Simple | Cannot be split | roll_no |
| Composite | Made of parts | name = first + last |
| Single-valued | One value | date_of_birth |
| Multi-valued | Many values | phone numbers |
| Derived | Computed from others | age from date_of_birth |
| Key | Identifies the entity | roll_no (underlined) |
Relationships
A relationship associates entities. In a diagram it is a diamond. A student enrols in a course; a department has teachers.
Cardinality says how many entities can take part:
- One to one (1:1): one teacher heads one department.
- One to many (1:N): one department has many students.
- Many to many (M:N): students and courses.
Participation can be total (every entity must take part, drawn with a double line) or partial.
Key constraints and weak entity sets
A key constraint says an attribute (or set) identifies each entity in its set. A weak entity has no key of its own and depends on an owner entity. For example, a dependent of an employee is identified only by the employee plus the dependent's name. A weak entity is drawn with a double rectangle, and its identifying relationship with a double diamond.
Extended ER features
- Specialisation: splitting an entity set into sub-groups, for example
personintostudentandteacher. - Generalisation: combining similar entity sets into one higher-level set.
- Inheritance: sub-entities take the attributes of the parent.
- Aggregation: treating a relationship as an entity so it can take part in another relationship.
Drawing an ER diagram: a worked example
Registrar office. Students enrol in courses. Each department has many students and offers many courses.
- Entities:
student(roll_no, name, phone),course(course_id, title, credits),department(dept_id, name). - Relationships:
student ENROLS_IN course(M:N, attributegrade),department HAS student(1:N),department OFFERS course(1:N). - Mark keys by underlining
roll_no,course_idanddept_id.
[department] --1---HAS---N-- [student] --M--ENROLS_IN--N-- [course]
\_________1---OFFERS---N_________________________/
Common mistakes
- Making an attribute out of something that deserves its own entity, such as storing a whole address of a hostel instead of a
hostelentity. - Forgetting the key attribute.
- Drawing M:N without noting that it becomes its own table later.
Practice questions
- Define entity, entity set and attribute.
- What are the types of attributes? Give an example of each.
- Explain cardinality with examples.
- What is a weak entity set? How is it drawn?
- Draw an ER diagram for a hospital with doctors, patients and appointments.
Answers to the practice questions
- Entity: an identifiable real-world thing; entity set: a collection of similar entities; attribute: a property of an entity.
- Simple, composite, single-valued, multi-valued, derived and key attributes, as shown in the table.
- 1:1 (teacher heads department), 1:N (department has students), M:N (students enrol in courses).
- A weak entity has no key of its own and depends on an owner entity; drawn as a double rectangle with a double-diamond relationship.
- Entities
doctor,patient; relationshipappointment(M:N) with attributes date and time; keysdoctor_idandpatient_id.
Sources
- Official BCA syllabus Database Management Systems, course code CC202, Unit I
- Raghu Ramakrishnan and Johannes Gehrke, Database Management Systems, 3rd edition, McGraw-Hill
