Skip to content

Instantly share code, notes, and snippets.

@lizTheDeveloper
Created October 24, 2023 18:59
Show Gist options
  • Save lizTheDeveloper/a78ee4747b13263452249e592601574e to your computer and use it in GitHub Desktop.
Save lizTheDeveloper/a78ee4747b13263452249e592601574e to your computer and use it in GitHub Desktop.
A lesson on how to approach data modeling and schema design in Postgres.

How to Think About Data Model and Schema in Postgres

Introduction

When working with databases like Postgres, understanding how to design your data model and schema is crucial. This lesson will guide you through the key considerations and questions to ask yourself.

Key Concepts

Tables

  • What is it?: A table is a collection of related data held in a structured format.
  • Questions to Ask:
    • What tables do I need?
    • What is the relationship between these tables?

Columns

  • What is it?: Columns define the data type and contain the data for the table.
  • Questions to Ask:
    • What data types do I need?
    • Are any columns optional?

Relationships

  • What is it?: Relationships between tables define how data in one table relates to data in another.
  • Questions to Ask:
    • Do I need one-to-one, one-to-many, or many-to-many relationships?
    • What are the foreign keys?

Indexes

  • What is it?: Indexes improve the speed of data retrieval operations.
  • Questions to Ask:
    • What queries will I run most often?
    • Which columns should be indexed?

Common Use Patterns

  • Normalization: Breaking down tables to eliminate redundancy.
  • Denormalization: Combining tables to improve query performance.

Cheat Sheet

  • CREATE TABLE: To create a new table.
  • ALTER TABLE: To modify an existing table.
  • DROP TABLE: To delete a table.

Exercise

Create a simple schema for a blog application. Include tables for users, posts, and comments.

Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment