- Entity-Relationship Diagrams (ERDs) are visual tools used to model the relationships between entities in a database.
- They help in designing databases by illustrating how data is connected and organized.
- ERDs are essential for understanding the structure of a database before implementation.
Key Components of ERDs
Entities
- Entities are objects or concepts that have data stored about them.
- Each entity is represented as a rectangle in an ERD.
Attributes
- Attributes are properties or characteristics of an entity.
- They are represented as ovals connected to their entity.
- Student: StudentID, Name, Age
- Course: CourseID, Title, Credits
Primary Keys
- A primary key is a unique identifier for each record in an entity.
- It ensures that each record can be uniquely identified.
- StudentID can be a primary key for the Student entity.
- CourseID can be a primary key for the Course entity.
Foreign Keys
- A foreign key is an attribute that creates a link between two entities.
- It refers to the primary key of another entity.
- In a Student-Course relationship, CourseID in the Enrollment entity can be a foreign key referencing the Course entity.
Relationships
- Relationships describe how entities are connected.
- They are represented as diamonds in an ERD.
- Enrolls: Connects Student and Course
- Teaches: Connects Teacher and Course
Types of Relationships
- One-to-One (1:1)
- Each record in one entity is related to only one record in another entity.
- One-to-Many (1:M)
- A record in one entity can be related to multiple records in another entity.
- Many-to-Many (M:M)
- Records in one entity can be related to multiple records in another entity, and vice versa.
- One-to-one:
- Each Student has one Locker.
- Each Locker is assigned to one Student.
- One-to-many:
- One Teacher can teach multiple Courses.
- Each Course is taught by one Teacher.
- Many-to-many:
- Students can enroll in multiple Courses.
- Courses can have multiple Students.
- Many-to-many relationships are typically broken down into two one-to-many relationships using a linking entity.
Constructing an ERD
- Use software tools like Lucidchart or draw.io to create ERDs easily.
- Step 1: Identify Entities
- Determine the main objects or concepts in the scenario.
- Step 2: Define Attributes
- List the properties of each entity.
- Step 3: Establish Primary Keys
- Choose a unique identifier for each entity.
- Step 4: Identify Relationships
- Determine how entities are connected
- Step 5: Add Foreign Keys
- Include attributes that link entities.
- Step 6: Draw the ERD
- Use rectangles for entities, ovals for attributes, diamonds for relationships, and lines to connect them.
School Database
- Step 1: Identify Entities
- Student, Course, Teacher
- Step 2: Define Attributes
- Student: StudentID, Name, Age
- Course: CourseID, Title, Credits
- Teacher: TeacherID, Name, Department
- Step 3: Establish Primary Keys
- StudentID for Student
- CourseID for Course
- TeacherID for Teacher
- Step 4: Identify Relationships
- Students enroll in Courses.
- Teachers teach Courses.
- Step 5: Add Foreign Keys
- CourseID as a foreign key in Enrollment.
- TeacherID as a foreign key in Course.
- Step 6: Draw the ERD
- Missing Primary Keys: Every entity should have a unique identifier.
- Incorrect Relationships: Ensure the cardinality (1:1, 1:M, M:N) is accurately represented.
- Overcomplicating the Diagram: Focus on the essential entities and relationships to avoid clutter.
Example Scenario: Inventory and Admin Management System
Entities and Attributes
admin_login
- Userid (PK) – integer
- Username – varchar
- Password – varchar
- Email – varchar
attemptlogin
- locktime – varchar
- unlocktime – varchar
product table
- productID (PK) – integer
- productname – varchar
- quantity – integer
- mfgdate – varchar
- price – float
transactions table
- transactionsid – integer
- transactiondate – varchar
- productid (FK) – integer
Relationships
- admin_login to product table: One-to-many relationship (An admin can manage multiple products).
- admin_login to attemptlogin: One-to-one relationship (Each admin has a corresponding login attempt record).
- product table to transactions table: One-to-many relationship (Each product can have multiple transactions).
ERD Representation
- The admin_login entity is central to the system, linking to both attemptlogin and product table.
- The attemptlogin table is directly linked to admin_login, representing a one-to-one relationship, tracking lock and unlock times per admin.
- The product table stores information about products managed by admins and is linked to transactions table, representing product purchases or movements.
- The transactions table references product table via a foreign key, forming a one-to-many relationship where each transaction is tied to a specific product.