Photo by Arnel Hasanovic on Unsplash
How to convert the ERD to a Relational Model Database while maintaining the principles of Set Theory “Mapping”
Mapping
Photo by Caspar Camille Rubin on Unsplash
explain the steps in a simplified way, here is how to convert the ERD to a Relational Model Database while maintaining the principles of Set Theory:
1. Define tables and Attributes:
- Create a table for each entity in the ERD model.
- Add the Attributes for each entity, noting that the Primary Key (PK) should be as short and vague as possible.
Entity: Student Attributes: StudentID, Name, DateOfBirth
CREATE TABLE Student (
StudentID INT PRIMARY KEY,
Name VARCHAR(100),
DateOfBirth DATE
);
2. Dealing with Multi-Valued Attributes:
- Create a new table for each Multi-Valued Attribute.
- Name the new table with a name derived from the original table name and the Attribute name. For example, if you have a table “Students” and a multi-valued Attribute “PhoneNumbers”, the name of the new table could be “StudentPhoneNumbers”.
- Add the Attributes for the Multi-Valued Attribute to the new table.
- Make the Primary Key in the new table consist of the Primary Key of the original table (to form the Foreign Key) and the multi-value Attribute (to ensure uniqueness).
Entity: Student Multi-Valued Attribute: PhoneNumbers
CREATE TABLE Student (
StudentID INT PRIMARY KEY,
Name VARCHAR(100),
DateOfBirth DATE
);
CREATE TABLE StudentPhoneNumbers (
StudentID INT,
PhoneNumber VARCHAR(15),
PRIMARY KEY (StudentID, PhoneNumber),
FOREIGN KEY (StudentID) REFERENCES Student(StudentID)
);
3. Dealing with Composite Attributes:
- Decompose the Composite Attributes into the basic Attributes that it consists of.
- Add these Attributes as columns in the table.
- Make sure to insert the appropriate Primary Key for the table.
Entity: Address Composite Attribute: Full Address consists of Street, City, Postal Code
CREATE TABLE Address (
AddressID INT PRIMARY KEY,
Street VARCHAR(100),
City VARCHAR(50),
PostalCode VARCHAR(10)
);
4. Not including Calculated Fields
Example:
If you have a calculated Attribute like Age in the Student entity, this is not included as an Attribute in the table, because age can be calculated based on the date of birth.
2. Binary Relationships
2.1. One-to-One Mandatory to Optional
Example:
Entity: Employee and EmployeeDetailsOne-to-One Mandatory: Each employee must have details.
CREATE TABLE Employee (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(100)
);
CREATE TABLE EmployeeDetails (
EmployeeID INT PRIMARY KEY,
Address VARCHAR(255),
PhoneNumber VARCHAR(15),
FOREIGN KEY (EmployeeID) REFERENCES Employee(EmployeeID)
);
Here, EmployeeDetails can be connected to Employee but Employee must contain details.
2.2. One-to-Many
Entity: Department and Employee
One-to-Many: A single department can have many employees.
CREATE TABLE Department (
DepartmentID INT PRIMARY KEY,
DepartmentName VARCHAR(100)
);
CREATE TABLE Employee (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(100),
DepartmentID INT,
FOREIGN KEY (DepartmentID) REFERENCES Department(DepartmentID)
);
Here, DepartmentID
in Employee
table is Foreign Key.
Weak Entity
Entity: Order and OrderItem
Weak Entity: OrderItem depends on Order .
CREATE TABLE Order (
OrderID INT PRIMARY KEY,
OrderDate DATE
);
CREATE TABLE OrderItem (
OrderID INT,
ItemID INT,
ItemName VARCHAR(100),
PRIMARY KEY (OrderID, ItemID),
FOREIGN KEY (OrderID) REFERENCES Order(OrderID)
);
Many-to-Many Relations
Example:
Entity: Student and Course
- Many-to-Many: Students can enroll in multiple courses, and courses can have multiple students.
CREATE TABLE Student (
StudentID INT PRIMARY KEY,
Name VARCHAR(100)
);
CREATE TABLE Course (
CourseID INT PRIMARY KEY,
CourseName VARCHAR(100)
);
CREATE TABLE Enrollment (
StudentID INT,
CourseID INT,
PRIMARY KEY (StudentID, CourseID),
FOREIGN KEY (StudentID) REFERENCES Student(StudentID),
FOREIGN KEY (CourseID) REFERENCES Course(CourseID)
);
Optional-to-Optional Bilateral Relationships
Example:
Entity: Customer and LoyaltyCard
- Optional-to-Optional: Not every customer has a loyalty card, and not every loyalty card is associated with a customer.
CREATE TABLE Customer (
CustomerID INT PRIMARY KEY,
Name VARCHAR(100)
);
CREATE TABLE LoyaltyCard (
CardID INT PRIMARY KEY,
CustomerID INT,
FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID) - can be NULL
);