Behind the shop

Database

The design this shop runs on. The diagrams are those of the project report; the row counts are read from PostgreSQL now.

Entities7in the ER diagram
Tables9in PostgreSQL
Rows now108over all tables

Seven entities: user account, author, publisher, category, book, order and review. Two relationships are many-to-many (a book can have several authors, an order can contain several books), so each of them becomes a table of its own, book_author and order_item. That gives nine tables.

1. ER diagram

Open the diagram alone

The entities with their attributes, and the seven relationships between them: an author writes a book, a publisher publishes it, a category classifies it; a user places an order, an order contains books; a user writes a review, and a review is about a book.

ER diagram of the book store: seven entities and their relationships

2. Relational schema

Open the diagram alone

The nine tables with their primary keys (PK) and foreign keys (FK), as created by db/01_schema.sql.

Relational schema: nine tables with primary and foreign keys

3. From the diagram to the tables

six decisions, one per step; lines from db/01_schema.sql
CREATE TABLE book (
    book_id          INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    isbn             VARCHAR(13)   NOT NULL UNIQUE,
    title            VARCHAR(200)  NOT NULL,
    publication_year INTEGER,
    price            NUMERIC(8, 2) NOT NULL CHECK (price > 0),
    ...
);

Step 1 of 6

An entity becomes a table

Every entity of the ER diagram is one table and every attribute one column. book_id is the primary key: PostgreSQL gives each new book the next number, and that number identifies the row.

Back
CREATE TABLE book (
    ...
    publisher_id     INTEGER NOT NULL REFERENCES publisher (publisher_id) ON DELETE RESTRICT,
    category_id      INTEGER NOT NULL REFERENCES category (category_id)  ON DELETE RESTRICT
);

Step 2 of 6

One-to-many becomes a foreign key

A book has exactly one publisher and one category, so the book row stores their ids. NOT NULL makes them mandatory. ON DELETE RESTRICT refuses to delete a publisher or a category that still has books.

CREATE TABLE book_author (
    book_id   INTEGER NOT NULL REFERENCES book (book_id)     ON DELETE CASCADE,
    author_id INTEGER NOT NULL REFERENCES author (author_id) ON DELETE CASCADE,
    PRIMARY KEY (book_id, author_id)
);

Step 3 of 6

Many-to-many becomes its own table

A book can have several authors and an author several books. Neither table can hold that in one column, so book_author keeps one row per pair. The two ids together are the primary key, so the same pair cannot be stored twice.

CREATE TABLE orders (
    order_id     INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    user_id      INTEGER NOT NULL REFERENCES user_account (user_id)
                 ON DELETE RESTRICT,
    ...
    total_amount NUMERIC(10, 2) NOT NULL CHECK (total_amount >= 0)
);

CREATE TABLE order_item (
    order_id   INTEGER NOT NULL REFERENCES orders (order_id) ON DELETE CASCADE,
    book_id    INTEGER NOT NULL REFERENCES book (book_id)    ON DELETE RESTRICT,
    quantity   INTEGER       NOT NULL CHECK (quantity > 0),
    unit_price NUMERIC(8, 2) NOT NULL CHECK (unit_price > 0),
    PRIMARY KEY (order_id, book_id)
);

Step 4 of 6

An order and its lines

order_item links an order to its books and adds quantity and unit_price. The price is stored on the line, so an old order keeps its price when the book price changes. Deleting an order deletes its lines (CASCADE); a book that was sold cannot be deleted (RESTRICT).

-- user_account
role    VARCHAR(10) NOT NULL DEFAULT 'customer'
        CHECK (role IN ('admin', 'customer')),
email   VARCHAR(100) NOT NULL UNIQUE,

-- book
stock_quantity INTEGER NOT NULL DEFAULT 0 CHECK (stock_quantity >= 0),

-- orders
status  VARCHAR(10) NOT NULL DEFAULT 'pending'
        CHECK (status IN ('pending', 'shipped', 'delivered', 'cancelled')),

-- review
rating  INTEGER NOT NULL CHECK (rating BETWEEN 1 AND 5),
UNIQUE (user_id, book_id)        -- one review per user per book

Step 5 of 6

Rules the database enforces itself

CHECK and UNIQUE are tested by PostgreSQL on every INSERT and UPDATE, whatever the application does: stock never goes below zero, a rating is 1 to 5, a role or status is one of the listed words, and a user has one review per book.

Browser, Flask application and PostgreSQL: request, SQL, rows, HTML page

Step 6 of 6

How a page uses all this

The browser asks for an address, Flask finds the function for it, the function sends SQL to PostgreSQL, the rows come back and fill an HTML template. The statements of every page are listed at its bottom, under "SQL behind this page".

4. Tables and rows

counted now with SELECT COUNT(*)
TableHoldsRows now
user_account customers and admins who can log in 5
author people who write books 15
publisher companies that publish books 7
category the shelves of the shop 6
book the books on sale, with price and stock 17
book_author which author wrote which book many-to-many 18
orders one row per order of a customer 10
order_item the books of an order, with quantity and unit price many-to-many 17
review one rating per user per book 13

5. How a page is built

Open the diagram alone

Every page works in the same four steps: request, SQL, rows, HTML page. The SQL of the page you are looking at is listed at the bottom of every page, under "SQL behind this page".

Browser, Flask application and PostgreSQL: request, SQL, rows, HTML page

This site is a student project made for the CMPE341 Database Design & Management course (Group 2). It is not a real shop.