目标

如何在MySQL数据库中创建函数(Function)

语法

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

示例

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

  1. create database hr;
  2. use hr;
  3. create table employees
  4. (
  5. employee_id int(11) primary key not null auto_increment,
  6. employee_name varchar(50) not null,
  7. employee_sex varchar(10) default '男',
  8. hire_date datetime not null default current_timestamp,
  9. employee_mgr int(11),
  10. employee_salary float default 3000,
  11. department_id int(11)
  12. );
  13. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('David Tian','男',10,7500,1);
  14. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Black Xie','男',10,6600,1);
  15. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Moses Wang','男',10,4300,1);
  16. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Rena Ruan','女',10,5300,1);
  17. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Sunshine Ma','女',10,6500,2);
  18. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Scott Gao','男',10,9500,2);
  19. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Warren Si','男',10,7800,2);
  20. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Kaishen Yang','男',10,9500,3);
  21. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Simon Song','男',10,5500,3);
  22. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Brown Guan','男',10,5000,3);
  23. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Eleven Chen','女',10,3500,2);
  24. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Cherry Zhou','女',10,5500,4);
  25. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Klause He','男',10,4500,5);
  26. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Maven Ma','男',10,4500,6);
  27. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Stephani Wang','女',10,5500,7);
  28. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Jerry Guo','男',10,8500,1);
  29. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Gerardo Garza','男',10,25000,8);
  30. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Derek Wu','男',10,5500,5);
  31. select * from employees;

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

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

调用函数

mysql——函数的使用方法与MySQL内部函数的使用方法一样。

[置顶] MySQL -- 创建函数(Function的更多相关文章

  1. MySQL 创建函数(Function)

    目标 怎么样MySQL创建数据库功能(Function) 语法 CREATE FUNCTION func_name ( [func_parameter] ) //括号是必须的,參数是可选的 RETUR ...

  2. 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 D ...

  3. Mysql创建函数出错

    目前在项目中,执行创建mysql的函数出错, mysql 创建函数出错信息如下: Error Code: 1227. Access denied; you need (at least one of) ...

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

    MySQL创建函数报ERROR 1418错误,不能创建函数,根据官方提示是说,不能创建函数可能是一个安全设置方面的配置或功能未开启原因,下面我们一起来看.   错误 ERROR 1418 (HY000 ...

  5. MySQL 创建函数失败提示1418

    MySQL 创建函数失败提示1418 在创建函数时,往往会遇到创建函数失败的情形,除去书写的创建函数的sql语句本身语法错误之外,还会碰到一个错误就是, 1418:This function has ...

  6. mysql 创建函数This function has none of DETERMINISTIC, NO SQL, or READS

    今天在mysql 5.6上创建函数的时候 发现报错: ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or R ...

  7. MySQL 创建函数报错 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

    问题描述 通过Navicat客户端,创建MySQL函数(根据的当前节点查询其左右叶子节点)时报错,报错信息如下: This function has none of DETERMINISTIC, NO ...

  8. 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 mys ...

  9. 变量声明置顶规则、函数声明及函数表达式和函数的arguments属性初始化

    一.变量声明和变量赋值: if (!("a" in window)) { ; } alert(a);//a为? 你可能认为alert出来的结果是1,然后实际结果是“undefine ...

随机推荐

  1. Codeforces Round #510 #C Array Product

    http://codeforces.com/contest/1042/problem/C 给你一个有n个元素序列,有两个操作:1,选取a[i]和a[j],删除a[i],将$a[i]*a[j]$赋值给a ...

  2. LeetCode 岛屿的最大面积

    给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被水包围着. 找到给定的二维数组中 ...

  3. windows 使用git上传代码至github

    1. 首先创建github账户 2. 创建github项目 3.  windows安装git工具 ·下载地址:https://git-for-windows.github.io/ ,下载直接安装即可, ...

  4. The Fourth Day

    迭代器 迭代器:迭代的工具 .什么是迭代:指的是一个重复的过程,每次重复称为一次迭代,并且每次重复的结果是下一次重复的初始值 例: while True: print('====>'') l=[ ...

  5. [转载] Python数据类型知识点全解

    [转载] Python数据类型知识点全解 1.字符串 字符串常用功能 name = 'derek' print(name.capitalize()) #首字母大写 Derek print(name.c ...

  6. python 有4个数字1234,能组成多少个互不相同且无重复的三位数数字。

    def output(): count = 0 for i in range(1,5): for j in range(1, 5): for k in range(1, 5): if i==j or ...

  7. ORACLE 查询所有用户调度作业

    --查询所有用户调度作业:select * from ALL_SCHEDULER_JOBS; --查询当前用户调度作业:select * from USER_SCHEDULER_JOBS; --查询当 ...

  8. java 枚举类型的使用

    应用  http://blog.csdn.net/qq_27093465/article/details/52180865 原理 http://blog.csdn.net/javazejian/art ...

  9. 电子邮件中的to、cc、bcc

    电子邮件中的to.cc(carbon copy)和bcc(blind carbon copy),分别是收件人.抄送.密送 to 收件人 你想要给其发邮件的人 cc 抄送人 cc和to是一样的,但是cc ...

  10. BZOJ 4001 [TJOI2015]概率论 ——找规律

    题目太神了,证明还需要用到生成函数. 鉴于自己太菜,直接抄别人的结果好了. #include <map> #include <cmath> #include <queue ...