Sub topic 17
Putting It Together: A Small Order-Tracking Schema
Assume you know basic SELECT, INSERT, UPDATE, and DELETE statements. You should also understand tables, rows, columns, and common data types. This walkthrough uses PostgreSQL syntax, especially GENERATED ALWAYS AS IDENTITY.
An order-tracking system needs related tables:
customersstores people who place orders.ordersstores each order and links it to a customer.productsstores items that can be purchased.order_itemsconnects products to orders.
The constraints make invalid relationships and values fail at the database boundary.
Creating the tables
Create the parent tables before the tables that reference them. A primary key identifies each row. Identity columns generate key values automatically.
CREATE TABLE customers (
customer_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
email VARCHAR(255) NOT NULL UNIQUE,
full_name VARCHAR(100) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE products (
product_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name VARCHAR(100) NOT NULL,
price NUMERIC(10, 2) NOT NULL CHECK (price >= 0)
);
CREATE TABLE orders (
order_id INTEGER GENERATED ALWAYS AS IDENTITY,
customer_id INTEGER NOT NULL,
status VARCHAR(20) NOT NULL DEFAULT 'new',
total_amount NUMERIC(10, 2) NOT NULL,
CONSTRAINT pk_orders PRIMARY KEY (order_id),
CONSTRAINT fk_orders_customer
FOREIGN KEY (customer_id)
REFERENCES customers (customer_id)
ON DELETE RESTRICT,
CONSTRAINT ck_orders_total CHECK (total_amount >= 0),
CONSTRAINT ck_orders_status
CHECK (status IN ('new', 'paid', 'shipped', 'cancelled'))
);
CREATE TABLE order_items (
order_id INTEGER NOT NULL,
product_id INTEGER NOT NULL,
quantity INTEGER NOT NULL CHECK (quantity > 0),
PRIMARY KEY (order_id, product_id),
FOREIGN KEY (order_id)
REFERENCES orders (order_id)
ON DELETE CASCADE,
FOREIGN KEY (product_id)
REFERENCES products (product_id)
);The order_items table has a composite primary key. A composite key uses multiple columns together. Here, an order can contain a product only once. Its quantity can then be updated rather than duplicated in a second row.
| Rule | Protection provided |
|---|---|
| `NOT NULL` | A required value cannot be omitted |
| `UNIQUE` | Two customers cannot share an email address |
| `FOREIGN KEY` | An order must reference an existing customer |
| `CHECK` | Amounts, quantities, and statuses must be valid |
| `ON DELETE CASCADE` | Deleting an order also deletes its items |
Understanding the relationships
ON DELETE RESTRICT prevents deleting a customer while orders still reference that customer. This protects order history. ON DELETE CASCADE removes an order's dependent items when the order itself is deleted. Choose cascading behavior only when deleting the child rows is intentional.
Predicting rejected statements
After inserting a customer, another customer with the same email violates UNIQUE. An order with an unknown customer_id violates the foreign key. An order with total_amount = -5.00 violates its CHECK constraint. Each statement raises an error, and the rejected row is not inserted.
Exercise
Exercise
What happens when an order is deleted, and why is that different from deleting its customer?
Show answer
The order's rows in order_items are deleted by ON DELETE CASCADE. Deleting the customer is rejected by ON DELETE RESTRICT while orders still reference that customer.