MySql5.0 and later supports stored procedures. I recently had time to study this.
Format:
CREATE PROCEDURE process name ([process parameters[,...]]) |
CREATE FUNCTION function name ([function parameters[,...]]) |
Procedure parameters:
[ IN | OUT | INOUT ] Parameter name Parameter type
Function parameters:
Parameter name Parameter type
Return type:
Valid MYSQL data type
Characteristics:
LANGUAGE SQL |
Procedure body/function body: The format is as follows:
BEGIN |
I don't care about proprietary features here. These are incompatible with the SQL specification, so the relevant content of characteristic (feature) is not considered.
//
There are several points to note during the development process:
1. Stored procedure comments: MySQL supports the use of -- or /**/ comments, where the former is a line comment and the latter is a segment comment
. 2. Variables are first declared with declare. Temporary variables can be directly modified with the @ prefix for reference.
3. When editing directly using the MySQL Administrator manager, you can directly enter the following function text;
however, if you automatically import stored procedures or functions in the script, because MySQL defaults to " ";" is the delimiter, then each sentence of the procedure body
is compiled by MySQL as a stored procedure, and the compilation process will report an error; therefore, the DELIMITER keyword must be used to declare in advance that
the delimiter will be restored when the current segment delimiter is used up. As shown below:
DELIMITER $$ |
4. MySQL supports a large number of built-in functions, some of which are consistent with large commercial databases such as oracle, informix, sybase, etc., but there are also some functions with inconsistent names but consistent functions; or some with consistent names but different functions. This is especially useful for DBAs who have transferred from database development should pay attention to these.
5. Debugging of stored procedures or functions: I have not yet studied the various toolkits provided by MySQL, and I am not sure whether it provides debugging tools. However, compilation errors are relatively easy to find; as for debugging business processes, you can use a comparison The stupid way is to create a debugging table and insert a record at each process point in the package body to observe the program execution process. This is also a more convenient and stupid method. ^_^
The following are two examples, which provide a string encryption algorithm. Each time you call it with the same input parameters, you will get a different encryption result.
The algorithm is relatively simple and not strong. They are implemented in the form of functions and procedures as follows:
(1) Function
eg:
/**/ |
CREATE FUNCTION fun_addmm(inpass varchar(10)) RETURNS varchar(11)
BEGIN
declare string_in varchar(39);
declare string_out varchar(78);
declare offset tinyint(2);
declare outpass varchar(30) default ';
declare len tinyint;
/*declare i tinyint;*/
(2) Process
CREATE PROCEDURE `pro_addmm`(IN inpass varchar(10),OUT outpass varchar(11)) |
The execution results are as follows:
mysql> call pro_addmm('zhouys',@a); |
The encryption algorithm has several weaknesses:
1. It does not support upper and lower case
2. It does not support Chinese
3. The encryption strength is not enough
. People who are interested can study how to write the decryption function, so I won’t go into details here.