MySQL 创建函数(Function)

目标

怎么样MySQL创建数据库功能(Function)

语法

CREATE FUNCTION func_name ( [func_parameter] ) //括号是必须的,參数是可选的
RETURNS type
[ characteristic ...] routine_body
  • CREATE FUNCTION 用来创建函数的keyword;
  • func_name 表示函数的名称;
  • func_parameters为函数的參数列表,參数列表的形式为:[IN|OUT|INOUT] param_name type
  1. IN:表示输入參数。
  2. OUT:表示输出參数;
  3. INOUT:表示既能够输入也能够输出;
  4. param_name:表示參数的名称;
  5. type:表示參数的类型,该类型能够是MySQL数据库中的随意类型;

演示样例

创建演示样例数据库、演示样例表与插入样例数据脚本:

create database hr;
use hr;

create table employees
(
	employee_id int(11) primary key not null auto_increment,
	employee_name varchar(50) not null,
	employee_sex varchar(10) default '男',
	hire_date datetime not null default current_timestamp,
	employee_mgr int(11),
	employee_salary float default 3000,
	department_id int(11)
);

insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('David Tian','男',10,7500,1);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Black Xie','男',10,6600,1);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Moses Wang','男',10,4300,1);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Rena Ruan','女',10,5300,1);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Sunshine Ma','女',10,6500,2);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Scott Gao','男',10,9500,2);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Warren Si','男',10,7800,2);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Kaishen Yang','男',10,9500,3);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Simon Song','男',10,5500,3);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Brown Guan','男',10,5000,3);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Eleven Chen','女',10,3500,2);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Cherry Zhou','女',10,5500,4);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Klause He','男',10,4500,5);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Maven Ma','男',10,4500,6);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Stephani Wang','女',10,5500,7);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Jerry Guo','男',10,8500,1);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Gerardo Garza','男',10,25000,8);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Derek Wu','男',10,5500,5);

select * from employees;

创建函数-依据ID获取员工姓名与员工工资

DELIMITER //
CREATE FUNCTION GetEmployeeInformationByID(id INT)
RETURNS VARCHAR(300)
BEGIN
	RETURN(SELECT CONCAT('employee name:',employee_name,'---','salary: ',employee_salary) FROM employees WHERE employee_id=id);
END//
DELIMITER ;

调用函数

在MySQL——函数的用法与MySQL内部函数的用法一样。

<很多其它精彩内容。见后面更新...>

假设您们在尝试的过程中遇到什么问题或者我的代码有错误的地方,请给予指正,很感谢!

联系方式:[email protected]

版权@:转载请注明出处。

版权声明:本文博客原创文章,博客,未经同意,不得转载。

时间: 2024-10-17 08:52:16

MySQL 创建函数(Function)的相关文章

mysql 创建函数ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_f

mysql 创建函数的时候 报错 ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)ERROR 1418 (HY000

MySQL创建函数报“ERROR 1418 ”错误,不能创建函数

MySQL创建函数报ERROR 1418错误,不能创建函数,根据官方提示是说,不能创建函数可能是一个安全设置方面的配置或功能未开启原因,下面我们一起来看. 错误 ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less

数据库系列之mysql 自定义函数function,函数和存储过程的区别

mysql 自定义函数function,函数和存储过程的区别 https://blog.csdn.net/u010365819/article/details/80470448 1.MySQL自定义函数简介 在MySQL中使用自定义函数也需要相应的要求,语法如下, 创建新函数: Create function function_name(参数列表) returns返回值类型 函数体内容 相关说明, 函数名:应该合法的标识符,并且不应该与已有的关键字冲突.一个函数应该属于某数据库,可以使用db_n

Mysql 创建函数出现This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA

This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary mysql的设置默认是不允许创建函数 解决办法1: 执行: SET GLOBAL log_bin_trust_function_creators = 1; 不过 重启了 就失效了 注意: 有主从复制的时候 从机必须要设置  不然会导致主从同步失败 解决办法2: 在my.cnf里面设置 log-bin-tr

mysql 创建view,function,procudure的注意事项

创建 view 会有 DEFINER=admin@localhost 可以通过Navicat修改,设计View-->adanced修改创建 function 会有 DEFINER=admin@localhost 可以通过Navicat修改,双击函数-->adanced修改存储过程 会有 DEFINER=admin@localhost 可以通过Navicat修改,双击函数-->adanced修改 show create event event_awr_resetpartition SHOW

MySQL 创建函数

delimiter $$ create function myFunction() returns varchar(255) deterministic begin DECLARE totalCredits varchar(200); set totalCredits='大家好aa'; return totalCredits; end$$ delimiter

Mysql创建函数时报错

先去查询  show variables like '%func%' ; 这个语句,如果该语句最后输出的值是OFF 那么就用下面的语句去修改就可以:set global log_bin_trust_function_creators = 1 然后再创建,就OK了!

mysql中创建函数

MySql创建函数 一.查看创建函数的功能是否开启: mysql> show variables like '%func%'; +-----------------------------------------+-------+ | Variable_name                            | Value | +-----------------------------------------+-------+ | log_bin_trust_function_crea

Mysql的函数使用方法

今天有点临时需求要计算一张表的结果,不想写代码,想到了mysql的自定义函数.碰到了很多问题,为了方便一下使用,在此记录一下. 需求:一张表中,有比分,需要查询出比赛id和比赛结果. 分析: 单表查询没啥的,困难就困难在怎么判断比分之后返回想要的结果. 这里我把比赛结果分别定义代号,1主场胜.2平局.3客场胜 函数逻辑: 接收两个参数,判断参数的大小,分别返回结果. 实现: Navicat操作: navicat for mysql 工具内--创建函数 这里分别是存储过程和函数,我们选择函数 设置