This article describes how to create and execute MySQL stored functions and procedures on your A2 Hosting account. You can use stored functions and procedures for a wide range of scenarios. For example, well-designed stored functions and procedures can enhance database security, improve data integrity, and increase performance.
MySQL stored functions provide a powerful and flexible way to manipulate and process data. You can define and run stored functions on any A2 Hosting server that uses MySQL.
To demonstrate a basic example of stored functions, let's start by creating a database that we can use for testing purposes. In the following SQL statement, replace username with your account username:
CREATE DATABASE username_test;
If you are using phpMyAdmin, click the name username_test to select the database. Otherwise, if you are using the mysql command-line program, type the following SQL statement:
Next, create a table in the username_test database named products. To do this, run the following SQL statement:
CREATE TABLE products (prod_id INT NOT NULL AUTO_INCREMENT, prod_name VARCHAR(20) NOT NULL, prod_cost FLOAT NOT NULL DEFAULT 0.0, prod_price FLOAT NOT NULL DEFAULT 0.0, PRIMARY KEY(prod_id));
The following SQL statement adds some sample data to the products table:
INSERT INTO products (prod_name, prod_cost, prod_price) VALUES ('Basic Widget',5.95,8.35),('Micro Widget',0.95,1.35),('Mega Widget',99.95,140.00);
Now that we have a database and a table to work with, we are ready to create a stored function. Let's create a function named calcProfit. This function takes two input parameters: the cost and the price of something. It calculates the profit by subtracting the cost from the price, and then returns the value to the calling expression.
To create this stored function, run the following MySQL statements:
DELIMITER $$ CREATE FUNCTION calcProfit(cost FLOAT, price FLOAT) RETURNS DECIMAL(9,2) BEGIN DECLARE profit DECIMAL(9,2); SET profit = price-cost; RETURN profit; END$$ DELIMITER ;
You can now execute the stored function in a database query. The following SQL statement demonstrates how to do this:
SELECT *, calcProfit(prod_cost,prod_price) AS profit FROM products;
This SQL statement returns the following results:
+---------+--------------+-----------+------------+--------+ | prod_id | prod_name | prod_cost | prod_price | profit | +---------+--------------+-----------+------------+--------+ | 1 | Basic Widget | 5.95 | 8.35 | 2.40 | | 2 | Micro Widget | 0.95 | 1.35 | 0.40 | | 3 | Mega Widget | 99.95 | 140 | 40.05 | +---------+--------------+-----------+------------+--------+
As you can see, the calcProfit function automatically calculates the profit (price minus the cost) for each product in the table.
Stored procedures are sometimes confused with stored functions, but they are different in some important ways. Stored procedures, for example, must be invoked with the CALL statement, whereas stored functions can be used directly in SQL expressions. You can define and run stored procedures on any A2 Hosting server that uses MySQL.
The following MySQL statements demonstrate how to create a very basic stored procedure named procedureTest. This procedure performs a simple lookup on the products table that we used in the stored function example above. Although this procedure does not have much practical use, it demonstrates the correct syntax and structure for declaring a stored procedure:
DELIMITER $$ CREATE PROCEDURE procedureTest() BEGIN SELECT prod_name FROM products; END$$ DELIMITER ;
To execute the stored procedure, use the following MySQL statement:
CALL procedureTest() \G
For more information about stored procedures and functions in MySQL, please visit http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html.
Subscribe to receive weekly cutting edge tips, strategies, and news you need to grow your web business.
No charge. Unsubscribe anytime.