Data Modeling Concepts
Learning Objectives
- Understand what data modeling is and why it's crucial
- Identify entities, attributes, and relationships
- Learn the three levels of data modeling
- Master the process of turning real-world scenarios into data models
- Apply data modeling best practices
What is Data Modeling?
Data modeling is the process of creating a visual representation of data and its relationships. Think of it as creating a blueprint for your database! 🏗️
Real-World Analogy
Core Components of Data Models
graph TB
DM[Data Model]
DM --> E[Entities]
DM --> A[Attributes]
DM --> R[Relationships]
E --> E1[Things/Objects
Customer, Product] A --> A1[Properties
Name, Price] R --> R1[Connections
Customer BUYS Product] style DM fill:#667eea,color:white style E fill:#10b981,color:white style A fill:#f59e0b,color:white style R fill:#ef4444,color:white
Customer, Product] A --> A1[Properties
Name, Price] R --> R1[Connections
Customer BUYS Product] style DM fill:#667eea,color:white style E fill:#10b981,color:white style A fill:#f59e0b,color:white style R fill:#ef4444,color:white
🏪 Real World Example: Online Store
What We See Here:
- 📦Entities:Customer, Product, Order (the main "things")
- 📝Attributes:Properties like Name, Price, Email
- 🔗Relationships:Customer PLACES Order, Order CONTAINS Products
- 🔢Cardinality:One customer can place many orders (1:N)
Three Levels of Data Modeling
1️⃣ Conceptual Data Model
The Big Picture- What are we storing?
- High-level overview
- Business-friendly language
- No technical details
- Focus on entities and relationships
graph LR
Student[Student] -->|enrolls in| Course[Course]
Course -->|taught by| Instructor[Instructor]
Student -->|submits| Assignment[Assignment]
style Student fill:#e0f2fe
style Course fill:#dcfce7
style Instructor fill:#fef3c7
style Assignment fill:#fee2e2
2️⃣ Logical Data Model
The Detailed Design- How is it structured?
- All entities and attributes
- Primary and foreign keys defined
- Relationships with cardinality
- Still database-independent
Student
--------
StudentID (PK)
FirstName
LastName
Email
EnrollmentDate
Course
------
CourseID (PK)
CourseName
Credits
InstructorID (FK)
Enrollment
----------
StudentID (FK)
CourseID (FK)
EnrollmentDate
Grade
3️⃣ Physical Data Model
The Implementation- Actual database code!
- Specific to database system (MySQL)
- Data types specified
- Indexes defined
- Constraints implemented
CREATE TABLE students (
student_id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
enrollment_date DATE NOT NULL,
INDEX idx_email (email)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Identifying Entities
Pro Tip: Entity Identification
Defining Attributes
Attribute Types and Examples
| Attribute Type | Description | Examples |
|---|---|---|
| Simple | Cannot be divided further | Age, Price, Quantity |
| Composite | Can be divided into sub-parts | Full Name → First + Last |
| Single-Valued | Only one value per entity | DateOfBirth, SSN |
| Multi-Valued | Can have multiple values | Phone Numbers, Skills |
| Derived | Calculated from other attributes | Age (from DateOfBirth) |
Attribute Best Practices
- ✅ Usemeaningful names(customer_email not ce)
- ✅ Beconsistentwith naming conventions
- ✅ Avoidcalculated valuesif possible
- ✅ Don't store what you canderive
- ❌ Avoid multi-valued attributes in relational databases
Understanding Relationships
graph TB
subgraph "One-to-One"
P1[Person] ---|1:1| Pass1[Passport]
end
subgraph "One-to-Many"
Dept[Department] ---|1:N| Emp[Employees]
end
subgraph "Many-to-Many"
Stu[Students] ---|M:N| Crs[Courses]
end
Practice: Model a Social Media Platform
Exercise: Create a Data Model
💡 Click for Solution
graph TB
User[User]
Post[Post]
Comment[Comment]
Hashtag[Hashtag]
User -->|creates| Post
User -->|follows| User
User -->|likes| Post
User -->|writes| Comment
Post -->|has| Comment
Post -->|tagged with| Hashtag
style User fill:#3b82f6,color:white
style Post fill:#10b981,color:white
style Comment fill:#f59e0b,color:white
style Hashtag fill:#8b5cf6,color:white
Entities:User, Post, Comment, Hashtag
Relationships:
- User creates Post (1:N)
- User follows User (M:N - self-referencing)
- User likes Post (M:N)
- User writes Comment (1:N)
- Post has Comment (1:N)
- Post tagged with Hashtag (M:N)