Now let’s add some sample data to work with. First, check what users you currently have:
SELECT * FROM users;
If you need to add more users for this lesson, add some new ones:
INSERT INTO users (username, email) VALUES
('diana', 'diana@example.com'),
('erik', 'erik@example.com'),
('fiona', 'fiona@example.com');
Now add some posts. Important: Replace the user IDs (4, 5, 6) with the actual IDs from your users table:
INSERT INTO posts (title, content, user_id) VALUES
('Getting Started with Backend', 'Excited to learn about servers', 4),
('Database Fundamentals', 'Sharing insights about SQL and data modeling.', 5),
('My Second Post', 'More thoughts on web development.', 4),
('Hello World API', 'Writing about building her first API endpoint.', 6),
('Advanced Queries', 'Back with tips on optimising database queries.', 5);
Important notes
- The
user_idvalues (4, 5, 6) must match existing user IDs from your users table. - Diana (user ID 4) has two posts.
- Erik (user ID 5) has two posts.
- Fiona (user ID 6) has one post.
- If you try to insert a post with
user_id = 999(a non-existent user), MySQL will reject it because of the foreign key constraint.
View Your Data
Check that everything was inserted correctly:
SELECT * FROM posts;
SELECT * FROM users;
In Workbench, you can see how the data is connected by looking at the user_id values in posts and matching them to the id values in users.