目标

如何在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. CSS 隐藏 visibility 属性

    定义和用法 visibility 属性规定元素是否可见. 提示:即使不可见的元素也会占据页面上的空间.请使用 "display" 属性来创建不占据页面空间的不可见元素. 说明 这个 ...

  2. PHP操作MySQL事务实例

    PHP与MYSQL事务处理 一般来说,事务都应该具备ACID特征.所谓ACID是Atomic(原子性),Consistent(一致性),Isolated(隔离性),Durable(持续性)四个词的首字 ...

  3. laravel使用总结(二)

    Mysql Eloquent 模型 新建Model 对应 表前缀 + 类名称 + s namespace App; use Illuminate\Database\Eloquent\Model; cl ...

  4. sort_main_extable

    参考:Linux异常表 1.函数调用关系 asmlinkage void __init start_kernel(void) -->sort_main_extable(); -->sort ...

  5. Flask+ Angularjs 实例: 创建博客

    允许任何用户注册 允许注册的用户登录 允许登录的用户创建博客 允许在首页展示博客 允许登录的用户退 后端 Flask-RESTful - Flask 的 RESTful 扩展 Flask-SQLAlc ...

  6. 第一阶段Sprint 对其他团队评价

    咱们的team 针对对“小学生网页四则运算”这个产品的评审,本人提出建议:1.第一阶段的产品Sprint不够好,无任务看板.无燃尽图.希望完善该产品的的Sprint,第二阶段的Sprint要认真写好. ...

  7. system sys,sysoper sysdba 的区别

    --===================================== -- system sys,sysoper sysdba 的区别 --========================= ...

  8. Python之虚拟机操作:利用VIX二次开发,实现自己的pyvix(系列一)成果展示和python实例

    在日常工作中,需要使用python脚本去自动化控制VMware虚拟机,现有的pyvix功能较少,而且不适合个人编程习惯,故萌发了开发一个berlin版本pyvix的想法,暂且叫其OpenPyVix.O ...

  9. 启动uwsgi报错error while loading shared libraries: libpcre.so.1:

    启动uwsgi时候报错: [root@ richie]# /usr/bin/uwsgi --ini /usr/local/nginx/conf/uwsgi.ini /usr/bin/uwsgi: er ...

  10. Java 线程池的原理与实现学习(二)

    java类库中提供的线程池简介: java提供的线程池更加强大,相信理解线程池的工作原理,看类库中的线程池就不会感到陌生了. execute(Runnable command):履行Ruannable ...