Sub topic 1
The Example Schema and the Constraints That Guard It
Prerequisites
You should know how to read a basic SELECT query with a WHERE clause. You should also understand that a table contains rows and columns, and that a primary key identifies a row. A foreign key connects a column in one table to a key in another table.
This chapter uses a small customer-order system. Later chapters will add, change, and remove rows from these tables. First, understand the rules that protect the data.
The Example Tables
A table definition describes the columns that each row must contain. It can also define constraints. A constraint is a rule that the database checks when data changes.
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) UNIQUE,
status VARCHAR(20) NOT NULL DEFAULT 'active'
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL REFERENCES customers (id),
total NUMERIC(10, 2) NOT NULL,
status VARCHAR(20) NOT NULL DEFAULT 'new'
);The customers table stores one row for each customer. The orders table stores orders placed by customers. The customer_id column connects an order to a customer.
An order must refer to a real customer because customer_id has a foreign key constraint. The referenced customer must be inserted before the order.
What Each Constraint Does
| Constraint | Rule in this schema | Purpose |
|---|---|---|
| `PRIMARY KEY` | `id` must identify one row | Prevents duplicate row identifiers |
| `NOT NULL` | `name`, `status`, and `total` need values | Prevents required data from being missing |
| `UNIQUE` | Customer email addresses cannot repeat | Prevents duplicate values in a column |
| `DEFAULT` | A missing status receives a value | Supplies a value automatically |
| `REFERENCES` | An order's customer must exist | Protects the relationship between tables |
A primary key is both unique and non-null. In this example, customers.id and orders.id are primary keys.
NOT NULL does not mean that a value must be meaningful. It only means that the special missing marker NULL is not allowed. An empty string, such as '', is still a value and may be accepted in a text column unless another rule rejects it.
UNIQUE usually allows one or more NULL values because NULL means unknown rather than equal to another value. Exact behavior can vary by database system.
A default is used only when an INSERT statement leaves out that column. It does not replace a value that the statement explicitly supplies.
How the Tables Relate
The relationship is usually described as one-to-many: one customer can have many orders, while each order belongs to one customer. The database checks this relationship when an order is inserted or when a customer is deleted or updated.
A Safe Mental Model
Before changing rows, ask:
- Which table is changing?
- Which columns are required?
- Could the new value violate uniqueness?
- Does every foreign key value refer to an existing row?
- If a parent row is removed, what should happen to its related orders?
These questions help you predict whether an INSERT, UPDATE, or DELETE will succeed. They also prepare you to interpret constraint errors instead of treating them as mysterious failures.
Exercise
Why would inserting an order with customer_id = 999 fail when no customer has id 999?
Show answer
The foreign key constraint requires orders.customer_id to match an existing customers.id, so the database rejects the order.