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.
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.
CREATE TABLE requests (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id)
);Result in the lab: requests created.
- PRIMARY KEY makes id the identity of requests. No two requests may share the same key.
- REFERENCES customers(id) requires an existing customer id whenever the reference is present.
- customer_id can repeat: two requests from the same customer are still two different requests.
- 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
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.
Each request has its own identity.
The other column points to customers(id).
PRIMARY KEY goes on id and REFERENCES on customer_id.
Solution explained
CREATE TABLE requests (id INTEGER PRIMARY KEY, customer_id INTEGER REFERENCES customers(id));id tells requests apart and customer_id references customers. Several requests can belong to the same customer.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Declare another relationship
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.
First identify what a row represents.
The reference points to the book's identity.
Use REFERENCES books(id) on book_id.
Solution explained
CREATE TABLE readings (id INTEGER PRIMARY KEY, book_id INTEGER REFERENCES books(id));Each reading has its own identity and can reference an existing book. book_id can repeat.
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.
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.