MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
A stored function in MySQL is a set of SQL statements that perform a specific task and return a single value. You can use stored functions in statements wherever an expression is used.
Here is a basic syntax to create a stored function:
CREATE FUNCTION function_name ([parameters]) RETURNS type [characteristic ...] routine_body
function_name
: The name of the function.parameters
: The parameters of the function, if any.type
: The data type of the value that the function returns.characteristic
: Characteristics of the function. For example: DETERMINISTIC
, SQL DATA ACCESS
, COMMENT
, etc.routine_body
: The body of the function, i.e., the SQL statements that the function executes.Here's an example of a function that gets the full name of an employee given the employee_id
:
DELIMITER // CREATE FUNCTION get_full_name(employee_id INT) RETURNS VARCHAR(255) DETERMINISTIC BEGIN DECLARE full_name VARCHAR(255); SELECT CONCAT(first_name, ' ', last_name) INTO full_name FROM employees WHERE id = employee_id; RETURN full_name; END // DELIMITER ;
In this example, the function get_full_name
takes one parameter, employee_id
, and returns a VARCHAR(255)
. It queries the employees
table and concatenates the first_name
and last_name
fields to form the full name of the employee with the given employee_id
.
You can call this function in a SQL statement like this:
SELECT get_full_name(1);
This would return the full name of the employee with an ID of 1.
Remember that the user who is calling the function must have the EXECUTE
privilege for it.
Creating stored functions in MySQL:
CREATE FUNCTION
statement.DELIMITER // CREATE FUNCTION my_function() RETURNS INT BEGIN -- SQL statements DECLARE result INT; SELECT COUNT(*) INTO result FROM my_table; RETURN result; END // DELIMITER ;
How to define functions in MySQL:
CREATE FUNCTION
statement, specifying the return type and body of the function.DELIMITER // CREATE FUNCTION my_function() RETURNS INT BEGIN -- SQL statements DECLARE result INT; SELECT COUNT(*) INTO result FROM my_table; RETURN result; END // DELIMITER ;
MySQL stored function examples:
DELIMITER // CREATE FUNCTION my_function() RETURNS INT BEGIN -- SQL statements DECLARE result INT; SELECT COUNT(*) INTO result FROM my_table; RETURN result; END // DELIMITER ; -- Call the function SELECT my_function() AS result;
Parameters in MySQL stored functions:
DELIMITER // CREATE FUNCTION my_function(param INT) RETURNS INT BEGIN -- SQL statements using parameter DECLARE result INT; SELECT COUNT(*) INTO result FROM my_table WHERE column1 = param; RETURN result; END // DELIMITER ;
Calling stored functions in MySQL:
SELECT my_function(param) AS result;
Debugging MySQL stored functions:
SELECT
statements to print or log intermediate results.DELIMITER // CREATE FUNCTION my_function() RETURNS INT BEGIN -- Debugging with SELECT statements SELECT 'Start of function'; -- Rest of the SQL statements SELECT 'End of function'; END // DELIMITER ;
Handling errors in MySQL stored functions:
DECLARE CONTINUE HANDLER
for specific conditions.DELIMITER // CREATE FUNCTION my_function() RETURNS INT BEGIN DECLARE custom_error CONDITION FOR SQLSTATE '45000'; DECLARE CONTINUE HANDLER FOR custom_error -- Error handling statements -- Rest of the SQL statements END // DELIMITER ;
Advanced techniques for MySQL stored functions:
DELIMITER // CREATE FUNCTION advanced_function(param INT) RETURNS INT BEGIN -- Conditional logic IF param > 0 THEN -- Statements RETURN 1; ELSE RETURN 0; END IF; -- Looping construct DECLARE i INT DEFAULT 1; WHILE i <= param DO -- Statements SET i = i + 1; END WHILE; -- Dynamic SQL SET @sql_query = CONCAT('SELECT * FROM my_table WHERE column1 = ', param); PREPARE stmt FROM @sql_query; EXECUTE stmt; DEALLOCATE PREPARE stmt; -- Return value RETURN 42; END // DELIMITER ;