Mysql中自定義函數的創(chuàng)建和執(zhí)行方式
Mysql自定義函數的創(chuàng)建和執(zhí)行
假設students表中包含id和name兩個字段,創(chuàng)建一個函數,函數的作用是根據id查找name
1.創(chuàng)建表,插入數據
create table students(id int,name varchar(100)); insert into students(id,name) values(1,'annie'),(2,'bell'),(3,'danny');
2.創(chuàng)建函數
DELIMITER // create function find_student(id int) returns varchar(100) READS SQL DATA begin ?? ?declare sname varchar(100) default ''; ? ? select students.name into sname from students where students.id=id; ? ? return sname; end // DELIMITER ;
需要注意的事項:
1)使用DELIMITER//修改分隔符
mysql的默認語句結束符號是分號,當mysql遇到分號時就自動執(zhí)行當前語句。因為函數定義時包含多條sql語句,所以使用DELIMITER //先將分隔符設置為//,等函數創(chuàng)建語句完成后,再將分隔符改回分號即可。
2)READS SQL DATA
之前我沒寫這句話,但是創(chuàng)建時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.執(zhí)行函數:select 函數名(參數值);
select find_student(3);
Mysql自定義函數創(chuàng)建失敗問題
案例
目前在項目中,執(zhí)行創(chuàng)建mysql的函數出錯,
mysql 創(chuàng)建函數出錯信息如下:
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的同步的,開啟這個參數,就可以創(chuàng)建成功了。
查看是否開啟:
show variables like '%func%'; +---------------------------------+-------+ | Variable_name | Value | +---------------------------------+-------+ | log_bin_trust_function_creators | ON | +---------------------------------+-------+ 1 row in set (0.00 sec)
為on則是開啟了
set global log_bin_trust_function_creators = 1;
可以通過這個命令設置,但是MySQL重啟后就失效了。
所有最后是通過修改MySQL數據庫的配置文件
在配置文件/etc/my.cnf的[mysqld]配置log_bin_trust_function_creators=1
修改完后重啟MySQL。
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
利用MyFlash實現(xiàn)MySQL數據閃回的操作指南
MySQL數據閃回是一種高級功能,它允許你在數據庫中恢復到某個特定的時間點,通常是事務開始或保存點的狀態(tài),以便處理數據錯誤或回滾意外更改,本文給大家介紹了如何利用MyFlash實現(xiàn)MySQL數據閃回,需要的朋友可以參考下2024-06-06