A foreign key connects related tables by requiring a column’s value to match an existing value in another table’s key column. In a haiku app, each haiku can record its author using the username of an existing user. section 8.2.2 section 8.2.4
The referenced column is typically a primary key. Here, users.username uniquely identifies a user, while haikus.author points to it. This models the one-to-many relationship in which one user can be associated with multiple haikus. section 8.2.2
Connecting users and haikus
The schema declares the relationship with FOREIGN KEY (author) REFERENCES users(username). After inserting alice into users, rows in haikus can use alice as their author. The database also supplies haiku_id and created_at through their declared types and defaults. section 8.2.4
Figure 8.4 Database schema with a foreign key relationship between the users and haikus tables
Use the schema diagram to see users.username connected to haikus.author and to distinguish each table’s primary key. section 8.2.2

















