目标

怎么样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内部函数的用法一样。

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

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

联系方式:david.louis.tian@outlook.com

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

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

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

  1. [置顶] MySQL -- 创建函数(Function

    目标 如何在MySQL数据库中创建函数(Function) 语法 CREATE FUNCTION func_name ( [func_parameter] ) //括号是必须的,参数是可选的 RETU ...

  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. mysql 创建函数set global log_bin_trust_function_creators=TRUE;

    <pre name="code" class="html">set global log_bin_trust_function_creators=T ...

随机推荐

  1. 17.1.1 How to Set Up Replication

    17.1.1 How to Set Up Replication 17.1.1.1 Setting the Replication Master Configuration 17.1.1.2 Sett ...

  2. 基于visual Studio2013解决算法导论之028散列表开放寻址

     题目 散列表 解决代码及点评 #include <iostream> #include <time.h> using namespace std; template & ...

  3. 模拟红外协议C程序——接收模块

    目的:方便程序的调试,提供效率,减少工作累,可以不在线调试编程时显示实时数据,特别产品不带显示的或者MCU是OPT的,有很大的帮助. 过程:将要看的数据发送出来,另一个板(一个带有显示的就OK了,显示 ...

  4. Eclipse3.6 添加JUnit源代码

    Eclipse中无法查看JUnit源代码,也无法设置源代码的jar. 解决方法: 1.  下载org.junit.source_4.8.1.v4_8_1_v20100427-1100.jar,放到ec ...

  5. pomelo 协议

    分析的是hybridconnector,使用的chatofpomelo-websocket(pomelo为0.7.0) 參考:https://github.com/NetEase/pomelo/wik ...

  6. word2vec 中的数学原理具体解释(五)基于 Negative Sampling 的模型

      word2vec 是 Google 于 2013 年开源推出的一个用于获取 word vector 的工具包,它简单.高效,因此引起了非常多人的关注. 因为 word2vec 的作者 Tomas ...

  7. MSSql跨数据库查询

    select d.PostTime ,l.name from DatabaseLog d ,lumigent.dbo.test l 一定要加.dbo,不加dbo不行啊 select * from lu ...

  8. c# 阶段总结

    然并卵然并卵然并卵然并卵然并卵然并卵然并卵

  9. Arduino 入门程序示例之一排 LED(2015-06-11)

    概述 最简单的一个 LED 的实验之后,自然是增加几个 LED,咱排成一排来玩吧.最后,再把一排的 LED 排成一个 8 字来玩——七段数码管. 示例程序 流水灯 第一个出场的肯定是经典的流水灯,也叫 ...

  10. java--方法和成员的继承,访问

    //在调用方法的时候,不是看句柄是哪一个类,而应该看对象是属于哪一个类的,属于哪一个类的,就调用哪一个类的成员和方法. //子类可以添加自己的新方法,但是子类对象的引用赋值给父类句柄之后,不能使用父类 ...