2. Data Modeling: Entities, Attributes & Relationships
Entities, Attributes & Relationships
Data modeling is the process of identifying the entities in your system, their attributes, and how they relate to each other. It's the foundation of schema design. A good data model makes your schema easier to design, understand, and maintain.
Entities are the objects or concepts in your system — users, products, orders, posts, comments. Attributes are the properties of entities — name, email, price, title, content. Relationships describe how entities are connected — a user has many orders, a product belongs to a category, a post has many comments. Understanding these concepts is essential for schema design.
This module covers entity-relationship modeling, cardinality, and the key decisions you make when modeling your data. You'll learn to identify entities, define attributes, and design relationships that accurately represent your domain.
Entities and Attributes
Entities are the objects or concepts in your system that you need to store data about. They're typically nouns — User, Product, Order, Post, Comment. Good entities are atomic, meaning they represent a single concept. They're also stable, meaning they don't change frequently. Attributes are the properties of entities — name, email, price, title, content. Attributes have data types — string, number, date, boolean. Choosing the right data type is important for data integrity and performance.
When identifying entities, ask: what are the objects in your system? What do you need to store data about? What are the business concepts? When identifying attributes, ask: what information do you need to know about each entity? What are the properties? Be comprehensive but avoid redundancy. Each attribute should be atomic and not derivable from other attributes.
-- Entity: User (Relational) CREATE TABLE users ( id UUID PRIMARY KEY, email TEXT UNIQUE NOT NULL, name TEXT NOT NULL, created_at TIMESTAMPTZ DEFAULT NOW(), updated_at TIMESTAMPTZ DEFAULT NOW() ); -- Entity: Product (Relational) CREATE TABLE products ( id UUID PRIMARY KEY, name TEXT NOT NULL, description TEXT, price NUMERIC(10,2) NOT NULL CHECK (price >= 0), stock INTEGER NOT NULL DEFAULT 0 CHECK (stock >= 0), created_at TIMESTAMPTZ DEFAULT NOW() ); -- Entity: Order (Relational) CREATE TABLE orders ( id UUID PRIMARY KEY, user_id UUID REFERENCES users(id) ON DELETE CASCADE, status TEXT DEFAULT 'pending' CHECK (status IN ('pending', 'paid', 'shipped', 'delivered')), total NUMERIC(10,2) NOT NULL DEFAULT 0, created_at TIMESTAMPTZ DEFAULT NOW() );
Relationships and Cardinality
Relationships describe how entities are connected. The cardinality of a relationship describes how many entities are involved. Understanding cardinality is essential for schema design.
-- One-to-one: User has one Profile CREATE TABLE profiles ( id UUID PRIMARY KEY, user_id UUID UNIQUE REFERENCES users(id) ON DELETE CASCADE, bio TEXT, avatar_url TEXT, created_at TIMESTAMPTZ DEFAULT NOW() ); -- One-to-many: User has many Orders CREATE TABLE orders ( id UUID PRIMARY KEY, user_id UUID REFERENCES users(id) ON DELETE CASCADE, order_date DATE, total DECIMAL(10,2) ); -- Many-to-many: Products and Categories CREATE TABLE product_categories ( product_id UUID REFERENCES products(id) ON DELETE CASCADE, category_id UUID REFERENCES categories(id) ON DELETE CASCADE, PRIMARY KEY (product_id, category_id) ); -- Self-referencing: Categories with sub-categories CREATE TABLE categories ( id UUID PRIMARY KEY, name TEXT UNIQUE NOT NULL, description TEXT, parent_category_id UUID REFERENCES categories(id) ON DELETE SET NULL, created_at TIMESTAMPTZ DEFAULT NOW() );
Knowledge Check
Ready to test your understanding of 2. Data Modeling: Entities, Attributes & Relationships?