首頁  >  文章  >  資料庫  >  Mysql中自訂函數的建立和執行方法是什麼

Mysql中自訂函數的建立和執行方法是什麼

WBOY
WBOY轉載
2023-04-17 16:07:031477瀏覽

    Mysql自訂函數的建立和執行

    假設students表中包含id和name兩個字段,建立一個函數,函數的作用是根據id查找name

    1.建立表,插入資料

    create table students(id int,name varchar(100));
    insert into students(id,name) values(1,'annie'),(2,'bell'),(3,'danny');

    2.建立函數

    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遇到分號時就自動執行目前語句。因為函數定義時包含多個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)#sql

    #my#my#my#my#my#my#my#my#my#my#my#my#my#my#了函式的類型:

    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 函數名稱(參數值);

    select find_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 sater logging is enabled (you *might* want to use the less safe log_bin_trustator_func​​tion_v #    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)

        at com.mysql.jdbc.MysqlIO.checkE#orPacket(MyhIO.java:3976) java:3976) checkErrorPacket(MysqlIO.java:3912)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:871)

        at com.mysql.bbc.java:871)
        at com.mysqlIO.bbc. #    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2739)
        at com.mysql.jdbc.ConnectionImpl execSQL(ConnectionImpl.java:2440)
        at com.mysql.jdbc.StatementImpl.executeInternal(StatementImpl.java:845)
        at com.mysql.bc.java:845)
        at com.mysql.bc.javaStatementImpl. #    ... 35 more


    這是因為有一個安全參數沒有開啟,log_bin_trust_function_creators 預設為0,是不允許function的同步的,開啟這個參數,就可以創建成功了。

    查看是否開啟:

    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。

    以上是Mysql中自訂函數的建立和執行方法是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

    陳述:
    本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除