0
0
DBMS Theoryknowledge~6 mins

Entity-Relationship model in DBMS Theory - Full Explanation

Choose your learning style9 modes available
Introduction
When designing a database, it can be hard to organize all the information clearly. The Entity-Relationship model helps by showing how different pieces of data connect and relate to each other in a simple way.
Explanation
Entities
Entities are the main objects or things in the system that we want to store information about. Each entity represents a real-world object like a person, place, or event. Entities are usually shown as rectangles in diagrams.
Entities represent real-world objects or concepts that hold data.
Attributes
Attributes describe the properties or details of an entity. For example, a person entity might have attributes like name, age, and address. Attributes help to store specific information about each entity instance.
Attributes provide details that describe an entity.
Relationships
Relationships show how entities are connected to each other. For example, a student entity might be related to a course entity through an enrollment relationship. Relationships help explain how data items interact or depend on each other.
Relationships link entities to show how they interact.
Cardinality
Cardinality defines the number of instances of one entity that can be associated with instances of another entity. It tells us if the relationship is one-to-one, one-to-many, or many-to-many. This helps in understanding the rules of data connections.
Cardinality specifies how many entity instances participate in a relationship.
Primary Key
A primary key is an attribute or a set of attributes that uniquely identifies each instance of an entity. It ensures that each record can be distinguished from others. For example, a student ID can be a primary key for a student entity.
Primary keys uniquely identify each entity instance.
Real World Analogy

Imagine a school where students enroll in classes. Each student has a unique ID and personal details. Classes have names and schedules. The enrollment shows which students attend which classes, and how many students can be in each class.

Entities → Students and classes as main objects in the school
Attributes → Student names, IDs, and class schedules as details about students and classes
Relationships → Enrollment showing which students attend which classes
Cardinality → Rules like one student can enroll in many classes, and each class can have many students
Primary Key → Student ID uniquely identifying each student
Diagram
Diagram
┌─────────┐       Enrolls       ┌─────────┐
│ Student │─────────────────────│  Class  │
└─────────┘                     └─────────┘
    │                              │
    │                              │
    ▼                              ▼
[StudentID, Name, Age]       [ClassID, Name, Time]
This diagram shows two entities, Student and Class, connected by the Enrolls relationship, with their attributes listed below.
Key Facts
EntityA real-world object or concept represented in a database.
AttributeA property or detail that describes an entity.
RelationshipA connection between two or more entities.
CardinalityThe number of instances of one entity related to instances of another.
Primary KeyAn attribute that uniquely identifies each entity instance.
Code Example
DBMS Theory
import sqlite3

conn = sqlite3.connect(':memory:')
cur = conn.cursor()

# Create tables for entities
cur.execute('CREATE TABLE Student (StudentID INTEGER PRIMARY KEY, Name TEXT, Age INTEGER)')
cur.execute('CREATE TABLE Class (ClassID INTEGER PRIMARY KEY, Name TEXT, Time TEXT)')

# Create table for relationship
cur.execute('CREATE TABLE Enrollment (StudentID INTEGER, ClassID INTEGER, '
            'FOREIGN KEY(StudentID) REFERENCES Student(StudentID), '
            'FOREIGN KEY(ClassID) REFERENCES Class(ClassID))')

# Insert sample data
cur.execute("INSERT INTO Student VALUES (1, 'Alice', 20)")
cur.execute("INSERT INTO Class VALUES (101, 'Math', '9AM')")
cur.execute("INSERT INTO Enrollment VALUES (1, 101)")

# Query to show relationship
cur.execute('''SELECT Student.Name, Class.Name FROM Student
               JOIN Enrollment ON Student.StudentID = Enrollment.StudentID
               JOIN Class ON Class.ClassID = Enrollment.ClassID''')
for row in cur.fetchall():
    print(row)
OutputSuccess
Common Confusions
Thinking that attributes can connect entities directly.
Thinking that attributes can connect entities directly. Attributes describe entities but do not show connections; relationships do.
Believing all relationships are one-to-one.
Believing all relationships are one-to-one. Relationships can be one-to-one, one-to-many, or many-to-many depending on cardinality.
Using non-unique attributes as primary keys.
Using non-unique attributes as primary keys. Primary keys must be unique to correctly identify each entity instance.
Summary
The Entity-Relationship model helps organize data by showing objects (entities), their details (attributes), and how they connect (relationships).
Cardinality defines the rules for how many instances of entities can be linked together.
Primary keys uniquely identify each entity instance to keep data clear and distinct.