CSC265 ยท 2079 ยท 10 marks
What is entities and its types? How do we reduce different types of entities and relationships of ER diagram into Tables? Explain with assuming different types of entities and relations.
What is entities and its types? How do we reduce different types of entities and relationships of ER diagram into Tables? Explain with assuming different types of entities and relations.[10]
Quick answer
An entity is a real-world object or thing that has an independent existence and can be distinguished from other objects. For example: a Student, a Hotel, a Room, an Employee.
Entities, Their Types, and Reduction of ER Diagram to Tables
Part 1: Entity and Its Types
Definition of Entity
An entity type defines a collection of entities that have the same attributes. An entity set is the collection of all entities of a particular entity type at a given point in time.
Types of Entities
1. Strong Entity
A strong entity is an entity type that has a key attribute (primary key) which uniquely identifies each entity in the entity set. It does not depend on any other entity for its existence.
- Represented by a single rectangle in the ER diagram.
- Example:
Hotel(identified by Hotel_ID),Employee(identified by Emp_ID)
2. Weak Entity
Entity types that do not have key attributes of their own are called weak entity types: their entity sets do not have sufficient attributes to form a primary key on their own.
Key characteristics of weak entities:
- Cannot be identified on their own; they depend on an owner (strong) entity.
- Always have total participation in the identifying relationship.
- Represented by a double rectangle in the ER diagram.
- The identifying relationship is represented by a double diamond.
- Have a partial key (discriminator) instead of a full primary key.
Example: Room is a weak entity that depends on Hotel. A room cannot exist without a hotel.
[Hotel] ====<< has >>==== [[Room]]
3. Superclass and Subclass Entities (Specialization / Generalization)
- Specialization is a top-down approach where a higher-level entity is broken into lower-level entities (subclasses).
- Generalization is a bottom-up approach where lower-level entities are combined into a higher-level entity (superclass).
Example:
Employee (superclass)
ISA
/ \
Developer Tester (subclasses)
Part 2: Reduction of ER Diagram Components into Tables
As a general guideline, if a relation schema corresponds to one entity type or one relationship type, its meaning is straightforward to explain. If instead a relation corresponds to a mixture of multiple entities and relationships, semantic ambiguities result.
Therefore, each entity type and each relationship type should ideally map to one relation (table).
Rule 1: Mapping a Strong Entity to a Table
Each strong entity type becomes a separate table. The key attribute becomes the primary key.
Example:
ER Entity: Student(Student_ID, Name, Age, Email)
Table:
| Student_ID (PK) | Name | Age | |
|---|---|---|---|
| S01 | Ram | 20 | [email protected] |
| S02 | Sita | 21 | [email protected] |
Student(Student_ID, Name, Age, Email)
PK: Student_ID
Rule 2: Mapping a Weak Entity to a Table
A weak entity becomes a table where:
- The partial key of the weak entity is included.
- The primary key of the owner entity is included as a foreign key.
- The primary key of the weak entity table = (Owner's PK + Partial Key).
Example:
- Strong Entity:
Hotel(Hotel_ID, Hotel_Name, Location) - Weak Entity:
Roomdepends onHotel, with partial keyRoom_Noand attributeType
Tables:
Hotel(Hotel_ID, Hotel_Name, Location)
PK: Hotel_ID
Room(Hotel_ID, Room_No, Type)
PK: (Hotel_ID, Room_No)
FK: Hotel_ID references Hotel(Hotel_ID)
| Hotel_ID (PK, FK) | Room_No (PK) | Type |
|---|---|---|
| H01 | 101 | Deluxe |
| H01 | 102 | Standard |
| H02 | 101 | Suite |
Rule 3: Mapping a 1:1 (One-to-One) Relationship
In a 1:1 relationship, the primary key of one entity is added as a foreign key in the other entity's table. Preferably, the foreign key is placed in the entity with total participation.
Example:
Employee(Emp_ID, Name)managesDepartment(Dept_ID, Dept_Name)- Relationship:
Manages(1:1)
Tables:
Employee(Emp_ID, Name)
PK: Emp_ID
Department(Dept_ID, Dept_Name, Emp_ID)
PK: Dept_ID
FK: Emp_ID references Employee(Emp_ID)
| Dept_ID | Dept_Name | Emp_ID (FK) |
|---|---|---|
| D01 | IT | E01 |
| D02 | HR | E02 |
Rule 4: Mapping a 1:N (One-to-Many) Relationship
The primary key of the "one" side entity is added as a foreign key in the table of the "many" side entity.
Example:
Department(Dept_ID, Dept_Name)has manyEmployee(Emp_ID, Name)- Relationship:
Works_In(1:N)
Tables:
Department(Dept_ID, Dept_Name)
PK: Dept_ID
Employee(Emp_ID, Name, Dept_ID)
PK: Emp_ID
FK: Dept_ID references Department(Dept_ID)
| Emp_ID | Name | Dept_ID (FK) |
|---|---|---|
| E01 | Ram | D01 |
| E02 | Sita | D01 |
| E03 | Hari | D02 |
Rule 5: Mapping an M:N (Many-to-Many) Relationship
Unlike 1:1 and 1:N relationships, an M:N relationship cannot be represented by simply adding a foreign key to either entity's table, because that would force each row to reference only one match on the other side. Instead, a new separate table (junction/bridge table) is created that contains:
- The primary keys of both participating entities (as foreign keys).
- Any attributes of the relationship itself.
- A composite primary key made up of both foreign keys together.
Example:
Student(Student_ID, Name)enrolls in manyCourse(Course_ID, Course_Name), and a course has many students.- Relationship:
Enrolls(M:N), with attributeGrade
Tables:
Student(Student_ID, Name)
PK: Student_ID
Course(Course_ID, Course_Name)
PK: Course_ID
Enrolls(Student_ID, Course_ID, Grade)
PK: (Student_ID, Course_ID)
FK: Student_ID references Student(Student_ID)
FK: Course_ID references Course(Course_ID)
| Student_ID (FK) | Course_ID (FK) | Grade |
|---|---|---|
| S01 | C01 | A |
| S01 | C02 | B |
| S02 | C01 | A+ |
Summary
Every ER construct reduces to a relational table by the same underlying idea: strong entities become tables with their own primary key, weak entities inherit the owner's key as part of a composite key, 1:1 and 1:N relationships push a foreign key onto one existing table, and M:N relationships require a brand-new junction table carrying both sides' keys as a composite primary key. Applying these five rules to every entity and relationship in an ER diagram is what produces a complete, normalized relational schema.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.