Inventory Management with Stored Procedures
This challenge focuses on designing and implementing stored procedures in SQL to manage an inventory system. Efficient inventory management is crucial for businesses to track stock levels, prevent shortages, and optimize ordering processes. You will create stored procedures to handle common inventory operations like adding new items, updating quantities, and retrieving inventory reports.
Problem Description
You are tasked with creating a set of stored procedures for a simple inventory management system. The system tracks items with the following attributes: item_id (INT, Primary Key, Auto-Increment), item_name (VARCHAR(255), Not Null), description (TEXT), and quantity (INT, Not Null, Default 0). The database table is named inventory.
You need to implement the following stored procedures:
AddInventoryItem(itemName VARCHAR(255), description TEXT): Adds a new item to theinventorytable. Theitem_nameis required, and thedescriptionis optional. Theitem_idwill be automatically generated. Thequantitydefaults to 0.UpdateInventoryQuantity(itemId INT, newQuantity INT): Updates thequantityof an existing item. TheitemIdmust exist in theinventorytable. ThenewQuantitymust be a non-negative integer.GetInventoryReport(): Returns a report showing all items in the inventory, includingitem_id,item_name,description, andquantity. The report should be sorted byitem_namein ascending order.GetLowStockItems(threshold INT): Returns a report of items with aquantityless than or equal to the providedthreshold. The report should includeitem_id,item_name,description, andquantity. The report should be sorted byquantityin ascending order.
Expected Behavior:
- Stored procedures should handle invalid inputs gracefully (e.g., non-existent
itemId, negativenewQuantity). While explicit error handling (likeRAISE) isn't strictly required for this challenge, the procedures should not cause the database to crash or produce incorrect results due to bad input. - The
UpdateInventoryQuantityprocedure should update the quantity only if the provideditemIdexists. - The
GetInventoryReportandGetLowStockItemsprocedures should return an empty result set if the table is empty.
Examples
Example 1:
Input: AddInventoryItem('Laptop', 'High-performance laptop for developers')
Output: (No direct output, but a new row is inserted into the inventory table with item_id = 1, item_name = 'Laptop', description = 'High-performance laptop for developers', quantity = 0)
Explanation: A new item is added to the inventory with the given name and description, and the quantity is set to the default value of 0.
Example 2:
Input: UpdateInventoryQuantity(1, 10)
Output: (No direct output, but the quantity for item_id 1 is updated to 10)
Explanation: The quantity of the item with item_id 1 is updated to 10.
Example 3:
Input: GetLowStockItems(5)
Output:
item_id | item_name | description | quantity
------- | ---------- | ----------- | --------
1 | Laptop | ... | 0
2 | Mouse | ... | 2
Explanation: This assumes item_id 1 (Laptop) has a quantity of 0 and item_id 2 (Mouse) has a quantity of 2. All other items have a quantity greater than 5. The result set contains only items with a quantity less than or equal to 5, sorted by quantity.
Constraints
item_namecannot be longer than 255 characters.newQuantitymust be a non-negative integer (0 or greater).thresholdinGetLowStockItemsmust be a non-negative integer.- The database system is assumed to be a standard SQL implementation (e.g., MySQL, PostgreSQL, SQL Server). The specific syntax might need minor adjustments depending on the chosen database.
- Performance is not a primary concern for this challenge, but avoid excessively inefficient queries.
Notes
-
Consider using parameterized queries to prevent SQL injection vulnerabilities.
-
Think about how to handle potential errors, such as attempting to update a non-existent item. While explicit error handling isn't mandatory, the procedures should not fail catastrophically.
-
Pseudocode for the stored procedures:
AddInventoryItem(itemName VARCHAR(255), description TEXT)
BEGIN INSERT INTO inventory (item_name, description) VALUES (itemName, description); ENDUpdateInventoryQuantity(itemId INT, newQuantity INT)
BEGIN UPDATE inventory SET quantity = newQuantity WHERE item_id = itemId; ENDGetInventoryReport()
BEGIN SELECT item_id, item_name, description, quantity FROM inventory ORDER BY item_name ASC; ENDGetLowStockItems(threshold INT)
BEGIN SELECT item_id, item_name, description, quantity FROM inventory WHERE quantity <= threshold ORDER BY quantity ASC; END