Module 7 · Designing tables

Declare primary and foreign keys

By the end you will be able to declare how a row is identified and which table it refers to.

About 14 min

0 of 52 lessons

Before you start, you need

Create tables and choose data types · Connect tables with keys and relationships

Declare identity and reference

You already know that an identifier tells rows apart and that a reference connects tables. Now you are going to write those rules when creating the table, so that the database can check them.

id INTEGER PRIMARY KEY declares the identifier of each request. No two requests may have the same id. That is its primary key.

customer_id INTEGER REFERENCES customers(id) says that a request's customer must exist in customers.id. It is a foreign key. The customers table must exist before the table that references it.

customer_id can repeat: a customer can have several requests, each with its own identity. Do not add UNIQUE to that reference if you need to allow several. To require that the customer is never missing, you would use NOT NULL, which is taught later.

You can also declare the reference at the end of the table as FOREIGN KEY (customer_id) REFERENCES customers(id). In this example it expresses the same connection.

Minimal syntax
CREATE TABLE requests (
  id INTEGER PRIMARY KEY,
  customer_id INTEGER REFERENCES customers(id)
);

See it in action

We want each request to have an identity and to be able to point to a customer.

SQL
CREATE TABLE requests (
  id INTEGER PRIMARY KEY,
  customer_id INTEGER REFERENCES customers(id)
);

Result in the lab: requests created.

  1. PRIMARY KEY makes id the identity of requests. No two requests may share the same key.
  2. REFERENCES customers(id) requires an existing customer id whenever the reference is present.
  3. customer_id can repeat: two requests from the same customer are still two different requests.
  4. This definition allows a missing reference. If your rule required a customer every time, you would need NOT NULL, which is explained later.

Practice with help

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

Declare the reference to a customer

Not started

Create requests with id INTEGER PRIMARY KEY and customer_id INTEGER REFERENCES customers(id), 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.

Your turn

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

Practice

Declare another relationship

Not started

Create readings with id INTEGER PRIMARY KEY and book_id INTEGER REFERENCES books(id), 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.

Key takeaways

  • PRIMARY KEY identifies rows in the table.
  • REFERENCES declares which identity a column points to.
  • A reference can be repeated in a one-to-many relationship.

Next: Protect columns with constraints

Go deeper

The practice tasks use integer keys. In SQLite, INTEGER PRIMARY KEY has special behaviour for generating identities. Other kinds of key, and composite keys, need checking on your engine; do not assume they all generate an id automatically.