Custom function in MySQL comes handy if we have some sort of calculation what to make, in order to get a value what can be stored. This implies that we want to push some logic into the database which is not a to popular idea among engineers.
A function always gives one value as a result like ‘select pi(); gives us 3.141593’.
To create a function the type of the input value(s) need to be specified as well as the output, result.
A real-world example would be to make a function to calculate VAT for some product.
create function vat(nr decimal(8,2)) -- could be two or more input values with comma separated returns decimal(8,2) deterministic return nr*0.23; -- to use the function select vat(1000);
The unique function created can be used within an insert statement, like
insert into someTable(netPrice, vatAmount) values (1000, vat(1000));