Module 7 · Designing tables

Practice: design a table with rules and an index

By the end you will be able to create a table with rules, expand it and prepare a search.

About 30 min

0 of 52 lessons

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

Not started

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.

Challenge

Solve this variation. Spot the part that has changed before you look for help.

Practice

Design another business rule

Not started

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.

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.

Next: Project: query a library database