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 person into student and teacher.
  • 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.

  1. Entities: student(roll_no, name, phone), course(course_id, title, credits), department(dept_id, name).
  2. Relationships: student ENROLS_IN course (M:N, attribute grade), department HAS student (1:N), department OFFERS course (1:N).
  3. Mark keys by underlining roll_no, course_id and dept_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 hostel entity.
  • Forgetting the key attribute.
  • Drawing M:N without noting that it becomes its own table later.

Practice questions

  1. Define entity, entity set and attribute.
  2. What are the types of attributes? Give an example of each.
  3. Explain cardinality with examples.
  4. What is a weak entity set? How is it drawn?
  5. Draw an ER diagram for a hospital with doctors, patients and appointments.

Answers to the practice questions

  1. Entity: an identifiable real-world thing; entity set: a collection of similar entities; attribute: a property of an entity.
  2. Simple, composite, single-valued, multi-valued, derived and key attributes, as shown in the table.
  3. 1:1 (teacher heads department), 1:N (department has students), M:N (students enrol in courses).
  4. 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.
  5. Entities doctor, patient; relationship appointment (M:N) with attributes date and time; keys doctor_id and patient_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