0
0
MySQLquery~30 mins

Why stored procedures centralize logic in MySQL - See It in Action

Choose your learning style9 modes available
Why Stored Procedures Centralize Logic
📖 Scenario: You work for a company that manages customer orders. The company wants to ensure that the logic for adding new orders is consistent and secure. Instead of writing the same SQL commands in many places, they want to use a stored procedure to centralize the logic.
🎯 Goal: Build a stored procedure in MySQL that inserts a new order into an orders table. This procedure will centralize the logic for adding orders, making it easier to maintain and secure.
📋 What You'll Learn
Create an orders table with columns order_id (auto-increment primary key), customer_name (VARCHAR), and order_amount (DECIMAL).
Create a stored procedure called add_order that takes customer_name and order_amount as input parameters.
The stored procedure should insert a new row into the orders table using the input parameters.
Demonstrate calling the stored procedure to add a new order.
💡 Why This Matters
🌍 Real World
Companies use stored procedures to keep important database operations consistent and secure, avoiding repeated code in many places.
💼 Career
Database developers and administrators often write stored procedures to centralize business logic, improving maintainability and security.
Progress0 / 4 steps
1
Create the orders table
Write a SQL statement to create a table called orders with these columns: order_id as an auto-increment primary key, customer_name as VARCHAR(100), and order_amount as DECIMAL(10,2).
MySQL
Need a hint?

Use CREATE TABLE with AUTO_INCREMENT for the primary key.

2
Create the stored procedure add_order
Write a SQL statement to create a stored procedure called add_order that takes two input parameters: in_customer_name (VARCHAR(100)) and in_order_amount (DECIMAL(10,2)). The procedure should insert a new row into the orders table using these parameters.
MySQL
Need a hint?

Use CREATE PROCEDURE with IN parameters and an INSERT statement inside BEGIN ... END.

3
Call the stored procedure to add an order
Write a SQL statement to call the stored procedure add_order with 'Alice' as customer_name and 150.75 as order_amount.
MySQL
Need a hint?

Use the CALL statement with the procedure name and parameters.

4
Explain why stored procedures centralize logic
Add a SQL comment explaining why using the stored procedure add_order centralizes the logic for adding orders in the database.
MySQL
Need a hint?

Write a comment starting with -- explaining that the procedure keeps the insert logic in one place.