📖 Scenario: Imagine you run a small bookstore. You want to keep track of your books and sales. You could write all this information in simple text files, but it can get messy and slow as your store grows.This project will help you understand why using a database is better than just using files.
🎯 Goal: You will create a simple table to store book information, add a configuration for a minimum stock level, write a query to find books that need restocking, and finalize the setup with a primary key.
📋 What You'll Learn
Create a table called Books with columns BookID, Title, and Stock
Add a variable min_stock to set the minimum stock level
Write a query to select books where Stock is less than min_stock
Add a primary key constraint on BookID
💡 Why This Matters
🌍 Real World
Stores, libraries, and many businesses use databases to keep their data organized and easy to access instead of messy files.
💼 Career
Knowing how to create tables, set variables, and write queries is essential for database jobs and managing data efficiently.
Progress0 / 4 steps
1
Create the Books table
Write a SQL statement to create a table called Books with columns BookID as integer, Title as text, and Stock as integer.
SQL
Hint
Use CREATE TABLE Books (BookID INTEGER, Title TEXT, Stock INTEGER);
2
Set minimum stock level
Add a SQL variable called min_stock and set it to 5 to represent the minimum stock level.
SQL
Hint
Use DECLARE min_stock INTEGER DEFAULT 5; to set the variable.
3
Query books needing restock
Write a SQL query to select all columns from Books where the Stock is less than the variable min_stock.
SQL
Hint
Use SELECT * FROM Books WHERE Stock < min_stock; to find books to restock.
4
Add primary key to Books table
Modify the Books table creation statement to add a primary key constraint on the BookID column.
SQL
Hint
Add PRIMARY KEY after BookID INTEGER in the table definition.
Practice
(1/5)
1. Why do we prefer databases over simple files for storing data?
easy
A. Databases cannot store large amounts of data.
B. Files are faster to search than databases.
C. Databases allow multiple users to access and update data safely at the same time.
D. Files automatically protect data better than databases.
Solution
Step 1: Understand data access needs
Multiple users often need to read and write data simultaneously in real life, like in banks or stores.
Step 2: Compare files and databases
Simple files do not handle multiple users well and can cause data loss or errors. Databases manage this safely.
Final Answer:
Databases allow multiple users to access and update data safely at the same time. -> Option C
Quick Check:
Multiple safe access = Databases [OK]
Hint: Databases handle many users safely, files do not [OK]
Common Mistakes:
Thinking files are faster to search than databases
Believing files protect data better automatically
Assuming databases cannot store large data
2. Which of the following is the correct reason why databases are better than files?
easy
A. Databases support concurrent access with data integrity.
B. Databases do not allow data updates once saved.
C. Files automatically create backups.
D. Files can handle complex queries easily.
Solution
Step 1: Review database features
Databases allow many users to access and update data at the same time without corrupting it.
Step 2: Check file limitations
Files do not manage concurrent access well and do not ensure data integrity during updates.
Final Answer:
Databases support concurrent access with data integrity. -> Option A
Quick Check:
Concurrent safe access = Databases [OK]
Hint: Databases keep data safe when many users update [OK]
Common Mistakes:
Believing databases prevent any data updates
Thinking files create backups automatically
Assuming files handle complex queries easily
3. Consider a system where multiple users try to update a customer record stored in a simple file simultaneously. What is the most likely outcome?
medium
A. All updates will be saved correctly without any data loss.
B. The system will convert the file into a database automatically.
C. The file will automatically lock and queue updates safely.
D. Some updates may overwrite others causing data loss or corruption.
Solution
Step 1: Understand file behavior with multiple writes
Simple files do not manage simultaneous writes well, so one update can overwrite another.
Step 2: Compare with database behavior
Databases use locks and transactions to prevent such data loss, but files lack this feature.
Final Answer:
Some updates may overwrite others causing data loss or corruption. -> Option D