Documentation Home
MySQL 9.5 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 41.4Mb
PDF (A4) - 41.5Mb
Man Pages (TGZ) - 272.3Kb
Man Pages (Zip) - 378.2Kb
Info (Gzip) - 4.1Mb
Info (Zip) - 4.1Mb


15.7.4.1 CREATE FUNCTION Statement for Loadable Functions

CREATE [AGGREGATE] FUNCTION [IF NOT EXISTS]function_name    RETURNS {STRING|INTEGER|REAL|DECIMAL}    SONAMEshared_library_name

This statement loads the loadable function namedfunction_name. (CREATE FUNCTION is also used to created stored functions; seeSection 15.1.21, “CREATE PROCEDURE and CREATE FUNCTION Statements”.)

A loadable function is a way to extend MySQL with a new function that works like a native (built-in) MySQL function such asABS() orCONCAT(). SeeAdding a Loadable Function.

function_name is the name that should be used in SQL statements to invoke the function. TheRETURNS clause indicates the type of the function's return value.DECIMAL is a legal value afterRETURNS, but currentlyDECIMAL functions return string values and should be written likeSTRING functions.

IF NOT EXISTS prevents an error from occurring if there already exists a loadable function with the same name. It doesnot prevent an error from occurring if there already exists a built-in function having the same name.IF NOT EXISTS is also supported forCREATE FUNCTION statements. SeeFunction Name Resolution.

TheAGGREGATE keyword, if given, signifies that the function is an aggregate (group) function. An aggregate function works exactly like a native MySQL aggregate function such asSUM() orCOUNT().

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 theplugin_dir system variable. For more information, seeSection 7.7.1, “Installing and Uninstalling Loadable Functions”.

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

CREATE FUNCTION also adds the function to the Performance Schemauser_defined_functions table that provides runtime information about installed loadable functions. SeeSection 29.12.22.12, “The user_defined_functions Table”.

Note

Like themysql.func system table, the Performance Schemauser_defined_functions table lists loadable functions installed usingCREATE FUNCTION. Unlike themysql.func table, theuser_defined_functions table also lists loadable functions installed automatically by server components or plugins. This difference makesuser_defined_functions preferable tomysql.func for checking which loadable functions are installed.

During the normal startup sequence, the server loads functions registered in themysql.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 aDROP FUNCTION statement, upgrade the shared library, and then issue aCREATE FUNCTION statement. If you upgrade the shared library first and then useDROP FUNCTION, the server may unexpectedly shut down.