mysql> show variables like ‘%func%’;
+—————————————–+——-+
| Variable_name | Value |
+—————————————–+——-+
| log_bin_trust_function_creators | ON |
+—————————————–+——-+
1 row in set (0.02 sec)
mysql> set global log_bin_trust_function_creators=1;
mysql> use xxx;
Database changed
delimiter $$是设置 $$为命令终止符号,代替分号,因为分号在begin…end中会用到;
mysql> delimiter $$
CREATE FUNCTION first_func(param1 varchar(5),parmam2 varchar(5),param3 varchar(10))
RETURNS TINYINT
BEGIN
RETURN 1;
END
$$ //函数创建成功后需恢复分号为命令终止符号。
mysql> delimiter ;
mysql> select first_func(‘aaa’,’bbb’,’ccc’);
+——————————-+
| first_func(‘aaa’,’bbb’,’ccc’) |
+——————————-+
| 1 |
+——————————-+
1 row in set (0.47 sec)
mysql> drop function first_func ;
Query OK, 0 rows affected (0.11 sec)
1) show function status
显示数据库中所有函数的基本信息
2)查看某个具体函数
mysql>show create function function;
1.进入mysql命令行
mysql>
2.用delimiter命令来把语句定界符从 ;变为//。这样就允许在程序体用;定界符传递到服务器,而不是被mysql自己来解释。
mysql> delimiter //
3.创建存储过程
mysql> CREATE PROCEDURE simpleproc (OUT param1 INT)
-> BEGIN
-> SELECT COUNT(*) INTO param1 FROM t;
-> END
-> //
Query OK, 0 rows affected (0.00 sec)mysql> delimiter ;
mysql> CALL simpleproc(@a);
Query OK, 0 rows affected (0.00 sec)
+——+
| @a |
+——+
| 3 |
+——+
1 row in set (0.00 sec)
DROP PROCEDURE num_from_employee ;
DROP FUNCTION name_from_employee ;
drop function if exists rand_strings;
create function rand_strings(str_length tinyint unsigned , str_type tinyint unsigned)
returns varchar(255)
begin
declare counter int unsigned default 0;
declare const_chars varchar(64) default ‘0123456789’;
declare result varchar(255) default ”;
if str_type = 1 then
set const_chars = ‘0123456789’;
elseif str_type = 2 then
set const_chars = ‘abcdefghijklmnopqrstuvwxyz’;
elseif str_type = 3 then
set const_chars = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’;
elseif str_type = 4 then
set const_chars = ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’;
elseif str_type = 5 then
set const_chars = ‘0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’;
elseif str_type = 6 then
set const_chars = ‘0123456789abcdefghijklmnopqrstuvwxyz’;
elseif str_type = 7 then
set const_chars = ‘0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ’;
else
set const_chars = ‘0123456789’;
end if;
while counter < str_length do
set result = concat(result,substr(const_chars,ceil(rand()*(length(const_chars)-1)),1));
set counter = counter + 1;
end while;
return result;
end
//
DELIMITER ;