Which of the following best explains why stored procedures help centralize business logic in a database?
Think about how writing logic once in one place helps multiple programs use the same rules.
Stored procedures centralize logic by allowing it to be written once inside the database. This logic can then be called by many different applications, which keeps behavior consistent and easier to maintain.
What is a key maintenance benefit of centralizing logic in stored procedures?
Consider what happens when you fix a bug in one place that affects many users.
When logic is centralized in stored procedures, updating the procedure updates the logic for all users and applications that call it, simplifying maintenance and reducing errors.
Given the stored procedure CalculateDiscount that applies a 10% discount for orders over 100, what is the output of this call?
CALL CalculateDiscount(150);
DELIMITER $$ CREATE PROCEDURE CalculateDiscount(IN order_amount DECIMAL(10,2)) BEGIN IF order_amount > 100 THEN SELECT order_amount * 0.9 AS discounted_price; ELSE SELECT order_amount AS discounted_price; END IF; END$$ DELIMITER ;
Check if the order amount is greater than 100 and apply 10% discount if yes.
The procedure checks if the order amount is over 100. Since 150 > 100, it returns 150 * 0.9 = 135.
What error will occur when creating this stored procedure that centralizes user validation logic?
CREATE PROCEDURE ValidateUser(IN username VARCHAR(50))
BEGIN
IF username = NULL THEN
SELECT 'Invalid user' AS message;
ELSE
SELECT 'Valid user' AS message;
END IF;
END;Remember how to properly check for NULL values in SQL.
In SQL, comparing with NULL using '=' always returns false. The correct check is 'IS NULL'. This causes the IF condition to never be true.
You have a stored procedure that calculates totals for large datasets. Which optimization will best improve performance while keeping logic centralized?
Think about how databases handle many rows efficiently.
Set-based operations allow the database to process many rows at once, which is faster than processing rows one by one. This keeps logic centralized and efficient.