Continuing my previous MySql Stored Prochedure, in this article I will explain about MySql Stored Function.
Different with MySql Stored Prochedure, MySql Stored Function, has to return a value. MySql Stored Function is a declaration function saved in MySql that will run some method/prochedure then return value to the user. To be remembered, MySql Stored Function only return a single value. Therefore it is imposible to return table.
What is the benefit of MySql Stored Function?
- Run faster than SQL command send from application
- Increase the performance of the application
- Reduce requseted command. How? Instead sending multiple command. The application may send only one trigger, and MySql will do the rest
- MySql Stored Function can be accessed by many type of application
- More secure. How? MySql Stored Function, only reveal the requested result to user.
What is the drawback of MySql Stored Function?
- Database server will have more load to do. In this case, it make high usage of memory and processor.
- You cant debug MySql Stored Function.
Lets build MySql Stored Function
First, make the function.
DROP FUNCTION IF EXISTS find_root//
create function find_root (source double) returns double
declare root double;
set root = sqrt(source);
Second, Call the function.
In this case, the MySql Stored Function will find the square root of any value inserted.
Thats all about my article. Hope this MySql Stored Function will be useful for you.