Entities: Entity-Relationship Diagram (ERD)
Entities: Entity-Relationship Diagram (ERD)
Entities: Entity-Relationship Diagram (ERD)
depicts the structure of a database. It shows how entities (data objects) relate to one another and
is commonly used in database design to illustrate the relationships between tables.
1. Entities
2. Attributes
3. Relationships
1. Entities
An entity represents a real-world object or concept that can have data stored about it in a
database. Each entity becomes a table in the database.
Example:
2. Attributes
Attributes describe the properties or characteristics of an entity. Each attribute becomes a column
in the entity's corresponding table.
Example:
3. Relationships
A relationship shows how two entities are related to each other. The relationship between
entities often translates into foreign keys in a relational database.
Relationships are depicted using diamonds or by directly connecting entities with lines.
Relationships can have different cardinalities, which define the number of instances of
one entity that can or must be associated with each instance of another entity.
o One-to-One (1:1): An entity in one set is related to one entity in another set.
o One-to-Many (1): One entity in a set is related to many entities in another set.
o Many-to-Many (M): Many entities in one set are related to many entities in
another set.
Example:
A primary key is a unique identifier for each record in a table. In an ERD, the primary
key is usually underlined or indicated in the diagram.
A foreign key is a field (or collection of fields) in one table that refers to the primary key
in another table. Foreign keys establish the relationship between the tables.
c. Cardinality
Cardinality indicates the number of instances of one entity that can be related to
instances of another entity. It is typically shown using symbols such as:
o 1 for one
o N for many
One-to-One (1:1): A customer can have only one loyalty card, and a loyalty card can
belong to only one customer.
One-to-Many (1): A customer can place many orders, but each order belongs to only one
customer.
Many-to-Many (M): A product can be part of many orders, and each order can contain
multiple products.
Example 1: Simple ERD for a Library System
Entities:
Book:
BookID (PK)
Title
Author
PublicationYear
ISBN
Member:
MemberID (PK)
Name
Email
Phone
Loan:
LoanID (PK)
LoanDate
ReturnDate
BookID (FK)
MemberID (FK)
Relationships:
One-to-Many (1): A Member can borrow many Books, but each Loan transaction
involves only one Book.
One-to-Many (1): A Book can be borrowed by multiple Members over time, but each
Loan is associated with one Member.
ERD Diagram:
Explanation:
Entities: Member, Loan, and Book are represented as tables in the database.
Relationships: A member can borrow many books, so there is a one-to-many
relationship between Member and Loan, and Book and Loan.
Attributes: Each entity has attributes that describe its properties.
Entities:
Customer:
CustomerID (PK)
CustomerName
Email
Phone
Product:
ProductID (PK)
ProductName
Price
StockQuantity
Order:
OrderID (PK)
OrderDate
TotalAmount
CustomerID (FK)
Relationships:
One-to-Many (1): A Customer can place many Orders, but each Order is placed by
only one Customer.
Many-to-Many (M): Each Order can contain many Products, and each Product can be
part of many Orders. This requires a junction table (like OrderDetails) to break down
the many-to-many relationship into two one-to-many relationships.
OrderID (FK)
ProductID (FK)
Quantity
ERD Diagram:
Explanation:
Entities:
Student:
o StudentID (PK)
o StudentName
o Email
Course:
o CourseID (PK)
o CourseName
o Credits
Enrollment:
o EnrollmentID (PK)
o Grade
o StudentID (FK)
o CourseID (FK)
Relationships:
Many-to-Many (M
): Students can enroll in many courses, and each course can have many students. The
Enrollment table serves as a junction table that links Student and Course.
ERD Diagram:
Explanation: