Before you start, you need
Add indexes for your lookups · Modify tables with ALTER TABLE · Protect columns with constraints
The problem
You will store order issues: each row will be one issue, with its own identity, a reference to the order, a code and a status. Turn the requirements into rules first, before writing SQL.
An order can have issues with different codes, but cannot repeat the same code. That is why the combination order_id and code must be unique. The status must be present and accept only open or resolved.
Work in three steps: create the table with those rules, add a priority with ALTER TABLE and create the search index. The starting priority is 2, and the allowed values go from 1 to 3 inclusive.
Check valid data and data that should be rejected: a reference that does not exist, a repeated code or a priority of 0 or 4. In the challenge the table and the score range will change, but you can follow the same plan.
How to think about it
Does a CHECK that allows values up to 30 really enforce the requirement «priority from 1 to 3»?
- Define one issue per row and its own identity. The reference points to an existing order.
- The code and status are required. The order_id-code pair cannot repeat, and the status accepts only two values.
- The full practice task adds the priority and the index. Test the rules with valid and invalid rows before judging the search.
Guided practice
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
Build the order issues table
Create order_issues with the rules in the list. Then add the priority and prepare the search index. Include all three steps in the same attempt.
- id: INTEGER PRIMARY KEY.
- order_id: INTEGER NOT NULL, referencing orders(id).
- code: TEXT NOT NULL; detail: optional TEXT.
- status: TEXT NOT NULL DEFAULT 'open', with a CHECK that allows only 'open' or 'resolved'.
- The pair order_id and code must be unique: UNIQUE (order_id, code).
- Add priority with ALTER TABLE: INTEGER NOT NULL DEFAULT 2, with a CHECK that allows 1 to 3 inclusive.
- Create idx_issues_status_priority on status and priority, in that order.
We check the requested structure, the valid rows, the default values and that invalid rows are rejected.
Every attempt resets the data, so include all the steps you need to check in a single attempt.
View tables and data
Open this panel to see the full data.
Define one issue per row, with a valid reference, a unique pair and restricted domains.
The second statement uses ALTER TABLE ... ADD COLUMN for priority.
The composite index must be called idx_issues_status_priority and use status, priority.
Solution explained
CREATE TABLE order_issues (
id INTEGER PRIMARY KEY,
order_id INTEGER NOT NULL,
code TEXT NOT NULL,
detail TEXT,
status TEXT NOT NULL DEFAULT 'open' CHECK (status IN ('open', 'resolved')),
UNIQUE (order_id, code),
FOREIGN KEY (order_id) REFERENCES orders(id)
);
ALTER TABLE order_issues ADD COLUMN priority INTEGER NOT NULL DEFAULT 2 CHECK (priority BETWEEN 1 AND 3);
CREATE INDEX idx_issues_status_priority ON order_issues(status, priority);The design keeps apart the initial definition, the later change and the access structure; the lab checks the final state of the schema.
Challenge
Solve this variation. Spot the part that has changed before you look for help.
Practice
Design another business rule
Create reviews to store a score from 1 to 5, and prepare its index. Use the following columns and rules:
- id: INTEGER PRIMARY KEY.
- customer_id: INTEGER NOT NULL REFERENCES customers(id).
- code: TEXT NOT NULL.
- score: INTEGER NOT NULL with a CHECK that allows 1 to 5 inclusive.
- The pair customer_id and code must be unique: UNIQUE (customer_id, code).
- Create idx_reviews_customer on customer_id.
We check the requested structure, the valid rows, the default values and that invalid rows are rejected.
Every attempt resets the data, so include all the steps you need to check in a single attempt.
View tables and data
Open this panel to see the full data.
List the rows that should be accepted and the ones that should be rejected.
Combine column constraints with uniqueness on a pair.
The index is created after reviews is defined.
Solution explained
CREATE TABLE reviews (id INTEGER PRIMARY KEY, customer_id INTEGER NOT NULL REFERENCES customers(id), code TEXT NOT NULL, score INTEGER NOT NULL CHECK (score BETWEEN 1 AND 5), UNIQUE (customer_id,code)); CREATE INDEX idx_reviews_customer ON reviews(customer_id);The score accepts its boundaries 1 and 5, rejects 0 and 6, and keeps uniqueness and the reference.
Key takeaways
- Start from the unit and the rules of the model.
- Constraints protect valid values.
- ALTER TABLE must preserve existing data.
- An index serves a query; it does not replace a constraint.