📖 Scenario: You are working for a small bookstore that wants to keep track of its books and authors. To do this well, you need to design tables in a database that store this information clearly and efficiently.
🎯 Goal: Build a simple database with two tables: one for books and one for authors. Learn why designing these tables properly helps keep data organized and easy to use.
📋 What You'll Learn
Create a table called authors with columns author_id (integer, primary key) and author_name (text).
Create a table called books with columns book_id (integer, primary key), title (text), and author_id (integer) to link to authors.
Add a foreign key constraint on books.author_id referencing authors.author_id.
Understand how this design helps avoid duplicate author data and keeps book information connected to authors.
💡 Why This Matters
🌍 Real World
Bookstores, libraries, and many businesses use databases with well-designed tables to keep their data clean and connected.
💼 Career
Understanding table design and foreign keys is essential for database administrators, developers, and data analysts to build reliable and efficient databases.
Progress0 / 4 steps
1
Create the authors table
Write a SQL statement to create a table called authors with two columns: author_id as an integer primary key, and author_name as text.
SQL
Hint
Use CREATE TABLE authors and define author_id as the primary key.
2
Create the books table
Write a SQL statement to create a table called books with three columns: book_id as an integer primary key, title as text, and author_id as an integer.
SQL
Hint
Remember to define book_id as the primary key and include author_id as an integer column.
3
Add a foreign key to link books to authors
Modify the books table creation to add a foreign key constraint on author_id that references authors.author_id.
SQL
Hint
Use FOREIGN KEY (author_id) REFERENCES authors(author_id) inside the books table definition.
4
Explain why this table design matters
Add a SQL comment at the end of your code explaining why linking books to authors with a foreign key helps keep data organized and avoids duplication.
SQL
Hint
Write a comment starting with -- that explains the benefit of using a foreign key here.
Practice
(1/5)
1. Why is it important to design tables properly in a database?
easy
A. To keep data organized and easy to find
B. To make the database look colorful
C. To increase the size of the database unnecessarily
D. To confuse users with complex structures
Solution
Step 1: Understand the purpose of table design
Good table design organizes data logically so it is easy to access and manage.
Step 2: Identify the benefits of proper design
Proper design improves speed and reduces errors in data handling.
Final Answer:
To keep data organized and easy to find -> Option A
Quick Check:
Good design = organized data [OK]
Hint: Good design means organized, fast, and error-free data [OK]
Common Mistakes:
Thinking design is about appearance
Ignoring data organization
Assuming bigger tables are better
2. Which of the following is the correct way to define a primary key in a SQL table?
easy
A. CREATE TABLE users (id INT, name VARCHAR(50) UNIQUE PRIMARY);
B. CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50));
C. CREATE TABLE users (id INT KEY PRIMARY, name VARCHAR(50));
D. CREATE TABLE users (id INT, name VARCHAR(50) PRIMARY);
Solution
Step 1: Review SQL syntax for primary key
The correct syntax places PRIMARY KEY after the column type.
Step 2: Check each option's syntax
CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50)); correctly uses 'id INT PRIMARY KEY'. Others misuse keywords or order.
Final Answer:
CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50)); -> Option B
Quick Check:
PRIMARY KEY follows column type [OK]
Hint: PRIMARY KEY goes right after the column type [OK]
Common Mistakes:
Placing PRIMARY before KEY
Using UNIQUE and PRIMARY together incorrectly
Misordering keywords
3. Given the table employees(id INT PRIMARY KEY, name VARCHAR(50), department_id INT) and departments(id INT PRIMARY KEY, name VARCHAR(50)), what will this query return?
SELECT e.name, d.name FROM employees e JOIN departments d ON e.department_id = d.id;
medium
A. A list of all employees without department names
B. A syntax error due to missing WHERE clause
C. A list of employee names with their department names
D. A list of department names only
Solution
Step 1: Understand the JOIN operation
The JOIN matches employees with departments where department_id equals department's id.
Step 2: Analyze the SELECT columns
The query selects employee names and their matching department names.
Final Answer:
A list of employee names with their department names -> Option C
Quick Check:
JOIN on keys returns combined rows [OK]
Hint: JOIN matches rows by keys to combine related data [OK]
Common Mistakes:
Thinking JOIN needs WHERE clause
Expecting only one table's data
Confusing JOIN with UNION
4. You have a table orders(order_id INT, customer_id INT, order_date DATE) but forgot to set a primary key. What problem might this cause?
medium
A. Duplicate orders might be stored without warning
B. The database will reject all inserts
C. Queries will run faster automatically
D. You cannot store dates without a primary key
Solution
Step 1: Understand the role of primary keys
Primary keys ensure each row is unique and prevent duplicates.
Step 2: Identify consequences of missing primary key
Without a primary key, duplicate rows can be inserted without errors.
Final Answer:
Duplicate orders might be stored without warning -> Option A
Quick Check:
No primary key = possible duplicates [OK]
Hint: Primary key prevents duplicate rows [OK]
Common Mistakes:
Thinking inserts are blocked without primary key
Assuming primary key affects date storage
Believing queries always run faster without keys
5. You want to design a table to store product reviews with columns: review_id, product_id, user_id, rating, and comment. Which design choice improves efficiency and avoids mistakes?
hard
A. Store all data in one text column to simplify
B. Make all columns PRIMARY KEY to ensure uniqueness
C. Use only product_id as PRIMARY KEY and ignore others
D. Set review_id as PRIMARY KEY and product_id, user_id as FOREIGN KEYS
Solution
Step 1: Identify keys for uniqueness and relationships
review_id uniquely identifies each review, so it should be PRIMARY KEY.
Step 2: Use FOREIGN KEYS for related data
product_id and user_id link to other tables, so they should be FOREIGN KEYS.
Step 3: Evaluate other options
Making all columns PRIMARY KEY is invalid; using one key ignores uniqueness; storing all data in one column loses structure.
Final Answer:
Set review_id as PRIMARY KEY and product_id, user_id as FOREIGN KEYS -> Option D
Quick Check:
Primary and foreign keys keep data correct and linked [OK]
Hint: Use primary key for unique ID, foreign keys for links [OK]
Common Mistakes:
Making multiple columns primary keys incorrectly
Ignoring foreign key relationships
Storing all data in one column
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.