13.7.4.1 CREATE FUNCTION Statement for Loadable Functions用于可加载函数的CREATE FUNCTION语句

CREATE [AGGREGATE] FUNCTION function_name
    RETURNS {STRING|INTEGER|REAL|DECIMAL}
    SONAME shared_library_name

This statement loads the loadable function named function_name. 此语句加载名为function_name的可加载函数。(CREATE FUNCTION is also used to created stored functions; see Section 13.1.17, “CREATE PROCEDURE and CREATE FUNCTION Statements”.)CREATE FUNCTION也用于创建存储函数;参见第13.1.17节,“创建过程和创建函数语句”。)

A loadable function is a way to extend MySQL with a new function that works like a native (built-in) MySQL function such as ABS() or CONCAT(). See Adding a Loadable Function.

function_name is the name that should be used in SQL statements to invoke the function. function_name是应在SQL语句中用于调用函数的名称。The RETURNS clause indicates the type of the function's return value. RETURNS子句指示函数返回值的类型。DECIMAL is a legal value after RETURNS, but currently DECIMAL functions return string values and should be written like STRING functions.DECIMALRETURNS后的合法值,但当前DECIMAL函数返回字符串值,应该像字符串函数一样编写。

The AGGREGATE keyword, if given, signifies that the function is an aggregate (group) function. An aggregate function works exactly like a native MySQL aggregate function such as SUM() or COUNT().如果给定聚合关键字,则表示该函数是聚合(组)函数。聚合函数的工作方式与本机MySQL聚合函数(如SUM()COUNT())完全相同。

shared_library_name is the base name of the shared library file containing the code that implements the function. The file must be located in the plugin directory. This directory is given by the value of the plugin_dir system variable. For more information, see Section 5.7.1, “Installing and Uninstalling Loadable Functions”.

CREATE FUNCTION requires the INSERT privilege for the mysql system schema because it adds a row to the mysql.func system table to register the function.

CREATE FUNCTION also adds the function to the Performance Schema user_defined_functions table that provides runtime information about installed loadable functions. See Section 27.12.21.8, “The user_defined_functions Table”.

Note注意

Like the mysql.func system table, the Performance Schema user_defined_functions table lists loadable functions installed using CREATE FUNCTION. Unlike the mysql.func table, the user_defined_functions table also lists loadable functions installed automatically by server components or plugins. This difference makes user_defined_functions preferable to mysql.func for checking which loadable functions are installed.

During the normal startup sequence, the server loads functions registered in the mysql.func table. If the server is started with the --skip-grant-tables option, functions registered in the table are not loaded and are unavailable.

Note注意

To upgrade the shared library associated with a loadable function, issue a DROP FUNCTION statement, upgrade the shared library, and then issue a CREATE FUNCTION statement. If you upgrade the shared library first and then use DROP FUNCTION, the server may unexpectedly shut down.