Home  >  Article  >  Database  >  How to create a function in mysql

How to create a function in mysql

coldplay.xixi
coldplay.xixiOriginal
2020-09-29 14:47:1512035browse

How to create a function in mysql: first check whether the function of creating a function is turned on; then if the value at Value is OFF, you need to turn it on; then when creating the function, first select the database; finally test it .

How to create a function in mysql

More related free learning recommendations: mysql tutorial(video)

How to create a function in mysql:

1. Check whether the function of creating a function is turned on:

mysql> show variables like '%func%';
+-----------------------------------------+-------+
| Variable_name                            | Value |
+-----------------------------------------+-------+
| log_bin_trust_function_creators | ON    |
+-----------------------------------------+-------+
1 row in set (0.02 sec)

2. If the value at Value is OFF, you need to turn it on.

mysql> set global log_bin_trust_function_creators=1;

3. When creating a function, select the database first.

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

After the function is successfully created, the semicolon must be restored as the command termination symbol.

mysql> delimiter ;

4. Test:

mysql> select first_func('aaa','bbb','ccc');
+-------------------------------+
| first_func('aaa','bbb','ccc') |
+-------------------------------+
|                             1 |
+-------------------------------+
1 row in set (0.47 sec)

5. Delete function:

mysql> drop function first_func ;
Query OK, 0 rows affected (0.11 sec)

6. View function

1 ) show function status

Display basic information of all functions in the database

2) View a specific function

 mysql>show create function function;

The above is the detailed content of How to create a function in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn