Using Procedure Parameters (IN, OUT, INOUT) in MySQL
📖 Scenario: You work at a small bookstore. You want to create a stored procedure to manage book stock. The procedure will take the book's ID and the number of new books to add or remove. It will also return the updated stock count.
🎯 Goal: Create a MySQL stored procedure named update_stock that uses IN, OUT, and INOUT parameters to update and return the stock count for a book.
📋 What You'll Learn
Create a table named
books with columns book_id (INT) and stock (INT).Insert one book with
book_id 101 and stock 50.Create a stored procedure
update_stock with three parameters: IN p_book_id INT, INOUT p_change INT, and OUT p_new_stock INT.The procedure should update the stock by adding
p_change to the current stock of the book with p_book_id.The procedure should set
p_new_stock to the updated stock value.If the updated stock is less than zero, set stock to zero and
p_change to the actual change applied.💡 Why This Matters
🌍 Real World
Bookstores and inventory systems often need to update stock counts safely and return updated values to applications.
💼 Career
Knowing how to use procedure parameters is essential for database developers and administrators to write reusable and efficient database logic.
Progress0 / 4 steps