Data Modeling 101

02/27/24·4 min read

Understanding Data Modeling

Data modeling is the process of creating an abstract model that documents and organizes data elements and their relationships. It serves as a blueprint for database design and helps ensure data consistency, reliability, and usability.

Types of Data Models

1. Conceptual Data Model

The highest-level view that represents business concepts and their relationships.

Key characteristics:

  • Business-focused
  • Technology-independent
  • Shows major entities and relationships
  • Used for communicating with stakeholders

Example of a conceptual model for an e-commerce system:

Customer --- Orders --- Products
     |          |          |
     |          |          |
  Address    Payment    Category

2. Logical Data Model

A detailed technical model that defines data structures without considering specific database technology.

Key characteristics:

  • Defines entities, attributes, and relationships
  • Includes data types and constraints
  • Technology-independent
  • Shows all entities and relationships

Example of a logical model for a Customer entity:

Customer
- CustomerID (Primary Key)
- FirstName
- LastName
- Email
- Phone
- CreatedDate
- LastModifiedDate

3. Physical Data Model

The most detailed model that specifies how data will be implemented in a specific database system.

Key characteristics:

  • Database-specific implementation
  • Includes indexes, constraints, and triggers
  • Defines physical storage requirements
  • Optimized for performance

Example of a physical model in SQL:

CREATE TABLE customers (
    customer_id SERIAL PRIMARY KEY,
    first_name VARCHAR(50) NOT NULL,
    last_name VARCHAR(50) NOT NULL,
    email VARCHAR(100) UNIQUE NOT NULL,
    phone VARCHAR(20),
    created_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    last_modified_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX idx_customer_email ON customers(email);

Data Modeling Concepts

1. Entities

Represent distinct objects or concepts in the system (e.g., Customer, Order, Product).

2. Attributes

Properties that describe entities:

  • Simple attributes (e.g., name, price)
  • Composite attributes (e.g., address)
  • Derived attributes (e.g., age calculated from birth date)
  • Multi-valued attributes (e.g., phone numbers)

3. Relationships

Define how entities are connected:

Types of Relationships:
- One-to-One (1:1)
- One-to-Many (1:N)
- Many-to-Many (M:N)

Example:

Customer (1) ----< Orders (N)  // One customer can have many orders
Order (N) >----< Product (M)   // Many orders can contain many products

4. Cardinality

Defines the numerical relationship between entities:

  • Mandatory: Must have at least one
  • Optional: May have zero or more
  • Exact: Specific number of relationships

Data Modeling Best Practices

1. Normalization

Process of organizing data to reduce redundancy and improve data integrity.

First Normal Form (1NF)

  • Each table has a primary key
  • Each column contains atomic values
  • No repeating groups

Example:

-- Bad Design
CREATE TABLE orders (
    order_id INT,
    products TEXT  -- Contains comma-separated products
);

-- Good Design (1NF)
CREATE TABLE orders (
    order_id INT,
    product_id INT,
    quantity INT
);

Second Normal Form (2NF)

  • Meets 1NF requirements
  • No partial dependencies

Third Normal Form (3NF)

  • Meets 2NF requirements
  • No transitive dependencies

2. Naming Conventions

Consistent naming helps maintain clarity:

Tables:
- Use plural nouns (customers, orders)
- Lowercase with underscores
- Descriptive names

Columns:
- Singular form
- Use prefixes for clarity
- Consistent naming across tables

3. Primary and Foreign Keys

  • Use surrogate keys (e.g., auto-incrementing IDs) for primary keys
  • Define appropriate foreign key constraints
  • Create indexes on frequently queried columns

Example:

CREATE TABLE orders (
    order_id SERIAL PRIMARY KEY,
    customer_id INTEGER NOT NULL,
    order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);

Common Data Modeling Patterns

1. Star Schema

Used in data warehousing:

  • Central fact table
  • Surrounding dimension tables
  • Optimized for querying

Example:

          DimDate
             |
DimCustomer--+--FactSales--DimProduct
             |
         DimLocation

2. Snowflake Schema

Extended star schema with normalized dimension tables.

3. Slowly Changing Dimensions (SCD)

Techniques for handling historical changes:

  • Type 1: Overwrite
  • Type 2: Add new row
  • Type 3: Add new column

Conclusion

Data modeling is a critical skill that forms the foundation of effective database design. By understanding and applying these basic concepts and best practices, you can create robust, scalable, and maintainable data structures that serve your application's needs effectively.

Remember that data modeling is an iterative process, and it's important to regularly review and refine your models as requirements evolve and new insights emerge.

> share post onX(twitter)