CONVERTING ER DIAGRAM TO TABLES
Converting an Entity-Relationship (ER) diagram into tables (relational schema) involves several steps to ensure that entities, relationships, and attributes are correctly transformed into a database schema.
Each entity in the ER diagram is converted into a table. The attributes of the entity become the columns of the table. The primary key of the entity becomes the primary key of the table.
Entity: Student
Table: Student
StudentID |
Name |
Age |
Major |
PK |
Entity: Employee
Table: Employee
EmployeeID |
FirstName |
LastName |
DOB |
PK |
Multivalued attributes require a separate table. This table includes a foreign key referencing the primary key of the original entity and a column for each value of the multivalued attribute.
Entity: Student
Tables:
StudentID |
Name |
PK |
Multivalued Attribute Table: StudentPhoneNumbers
StudentID (FK) |
PhoneNumber |
FK |
Weak entities are entities that do not have a primary key of their own and depend on a "strong" entity. The table for a weak entity includes a foreign key referencing the primary key of the strong entity and its own attributes.
Entities: Order (strong), OrderItem (weak)
Tables:
OrderID |
OrderDate |
PK |
OrderItemID |
OrderID (FK) |
ProductName |
Quantity |
PK |
FK |
Relationships between entities are represented in tables. Depending on the type of relationship, this mapping can vary:
A one-to-one relationship can be implemented by adding a foreign key to one of the tables. This foreign key references the primary key of the other table.
Here, two tables will be required. Either combine ‘R’ with ‘P’ or ‘Q’
OR
Entities: Student, Dorm
Tables:
StudentID |
Name |
Age |
Major |
DormID (FK) |
PK |
FK |
DormID |
DormName |
PK |
One to Many:
A one-to-many relationship is implemented by adding a foreign key to the table representing the "many" side of the relationship, referencing the primary key of the "one" side.
Here, two tables will be required-
NOTE- The relationship table may or maynot be required . If required a combined table will be drawn for the entity set Q and relationship set R.
Many to One :
Entities: Department, Professor
Tables:
DepartmentID |
DeptName |
PK |
ProfessorID |
Name |
Age |
DepartmentID (FK) |
PK |
FK |
A many-to-many relationship is represented by creating a new table (junction table) to capture the relationship. This table includes foreign keys referencing the primary keys of the related entities.
Three tables will be required
Entities: Student, Course
Tables:
StudentID |
Name |
Age |
Major |
PK |
CourseID |
CourseName |
PK |
Junction Table: Enrollment
StudentID (FK) |
CourseID (FK) |
EnrollmentDate |
FK |
FK |
There are several options for mapping a number of subclasses that together form a specialization (or alternatively, that are generalized into a superclass), such as the {SECRETARY, TECHNICIAN, ENGINEER} subclasses of EMPLOYEE.
maps to
PRACTICE QUESTION:
E-R DIAGRAM TO REALATION TABLE
1. Manager and Employee has (is-a ) relation , so primary key of employee(eid) table should be foreign key of manager table.
2. M employee works in 1 department , hence it has m:1 relationship, so primary key of depatrmet(id) table is the foreign key of employee table.
3. Dependent is a weak entity , hence primary key of employee(eid) table is the foreign key of Dependent table.
4 and 5.Employee works on a project has m:n relationship , hence three tables are formed and both primary key of employee(eid) and project(pno) are foreign key in works on table .