• ADADADADAD

    Mysql中自定义函数怎么创建[ mysql数据库 ]

    mysql数据库 时间:2024-11-25 13:35:27

    作者:文/会员上传

    简介:

    Mysql自定义函数的创建和执行假设students表中包含id和name两个字段,创建一个函数,函数的作用是根据id查找name1.创建表,插入数据createtablestudents(idint,namevarchar(100))

    以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。

        Mysql自定义函数的创建和执行

        假设students表中包含id和name两个字段,创建一个函数,函数的作用是根据id查找name

        1.创建表,插入数据
        createtablestudents(idint,namevarchar(100));insertintostudents(id,name)values(1,'annie'),(2,'bell'),(3,'danny');
        2.创建函数
        DELIMITER//createfunctionfind_student(idint)returnsvarchar(100)READSSQLDATAbegindeclaresnamevarchar(100)default'';selectstudents.nameintosnamefromstudentswherestudents.id=id;returnsname;end//DELIMITER;

        需要注意的事项:

        1)使用DELIMITER//修改分隔符

        mysql的默认语句结束符号是分号,当mysql遇到分号时就自动执行当前语句。因为函数定义时包含多条sql语句,所以使用DELIMITER //先将分隔符设置为//,等函数创建语句完成后,再将分隔符改回分号即可。

        2)READS SQL DATA

        之前我没写这句话,但是创建时mysql报错,提示Error Code: 1418. 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)

        上网查了一下,意思是没有声明mysql函数的类型:

        mysql开启了bin-log, 我们就必须指定我们的函数是否是哪种类型:

          1 DETERMINISTIC 不确定的

          2 NO SQL 没有SQl语句,当然也不会修改数据

          3 READS SQL DATA 只是读取数据,当然也不会修改数据

          4 MODIFIES SQL DATA 要修改数据

          5 CONTAINS SQL 包含了SQL语句

          所以我加上了READS SQL DATA

          3)使用局部变量

          变量定义:我这里使用declare sname varchar(100) default ‘’;定义了局部变量sname,

          变量使用:

          可以使用select students.name into sname from students where students.id=id;为变量赋值

          也可以直接使用set语句来赋值,如set sname=‘test’

          3.执行函数:select 函数名(参数值);
          selectfind_student(3);
          Mysql自定义函数创建失败问题案例

          目前在项目中,执行创建mysql的函数出错,

          mysql 创建函数出错信息如下:

          Caused by: java.sql.SQLException: 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)
          at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)
          at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3976)
          at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3912)
          at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:871)
          at com.mysql.jdbc.MysqlIO.readAllResults(MysqlIO.java:2373)
          at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2739)
          at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2482)
          at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2440)
          at com.mysql.jdbc.StatementImpl.executeInternal(StatementImpl.java:845)
          at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:745)
          ... 35 more

          这是因为有一个安全参数没有开启,log_bin_trust_function_creators 默认为0,是不允许function的同步的,开启这个参数,就可以创建成功了。

          查看是否开启:

          showvariableslike'%func%';+---------------------------------+-------+|Variable_name|Value|+---------------------------------+-------+|log_bin_trust_function_creators|ON|+---------------------------------+-------+1rowinset(0.00sec)

          为on则是开启了

          setgloballog_bin_trust_function_creators=1;

          可以通过这个命令设置,但是MySQL重启后就失效了。

          所有最后是通过修改MySQL数据库的配置文件

          在配置文件/etc/my.cnf的[mysqld]配置log_bin_trust_function_creators=1

          修改完后重启MySQL。

    Mysql中自定义函数怎么创建.docx

    将本文的Word文档下载到电脑

    推荐度:

    下载
    热门标签: mysql