Stored Procedures in MySQL
CREATION OF STORED PROCEDURES IN MySQL
1. AIM
To study and implement Stored Procedures in MySQL.
2. THEORY
✔ What is a Stored Procedure?
A Stored Procedure is a precompiled block of SQL statements stored in the database and executed on demand.
✔ Advantages of Stored Procedures
-
Improves performance (precompiled)
-
Ensures reusability of code
-
Helps maintain data integrity
-
Reduces network traffic
-
Provides security by restricting direct table access
3. GENERAL SYNTAX (MySQL)
MySQL requires the DELIMITER command when creating procedures to avoid conflict with semicolons.
✔ Calling a Procedure
4. EXAMPLES
⭐ Example 1: Procedure Without Parameters
▶ Create a table for demonstration
▶ Insert sample data
⭐ Procedure to Display All Employees
▶ Call the Procedure:
⭐ Example 2: Procedure With IN Parameter
✔ Procedure to get salary of an employee
▶ Call:
⭐ Example 3: Procedure With OUT Parameter
✔ Procedure to get total number of employees
▶ Call:
⭐ Example 4: Procedure with INOUT Parameter
✔ Procedure to increase salary by a percentage
▶ Call:
⭐ Example 5: Procedure Containing IF, ELSEIF, ELSE
▶ Call:
⭐ Example 6: Procedure With Loop
▶ Call:
⭐ Example 7: Procedure That Inserts Data
▶ Call:
⭐ Example 8: Procedure With Error Handling (SIGNAL)
▶ Test it:
5. LAB PRACTICE QUESTIONS
-
Create a procedure to display all students from the student table.
-
Write a procedure that calculates total price of products.
-
Create a procedure that increases salary by a given percentage.
-
Write a procedure to delete a record based on ID.
-
Create a procedure to return highest salary using an OUT parameter.
-
Write a procedure to display even numbers up to N using LOOP.
6. RESULT
Stored procedures were successfully created, executed, and tested in MySQL.
Comments
Post a Comment