CREATE [AGGREGATE] FUNCTIONfunction_nameRETURNS {STRING|INTEGER|REAL} SONAMEshared_library_name
A user-defined function (UDF) is a way to extend MySQL with a
new function that works like a native (built-in) MySQL function
such as ABS() or
CONCAT().
function_name is the name that should
be used in SQL statements to invoke the function. The
RETURNS clause indicates the type of the
function's return value.
shared_library_name is the basename
of the shared object file that contains the code that implements
the function. The file must be located in a directory that is
searched by your system's dynamic linker.
To create a function, you must have the
INSERT and privilege for the
mysql database. This is necessary because
CREATE FUNCTION adds a row to the
mysql.func system table that records the
function's name, type, and shared library name. If you do not
have this table, you should run the
mysql_fix_privilege_tables script to create
it. See Section 4.4.5, “mysql_fix_privilege_tables — Upgrade MySQL System Tables”.
An active function is one that has been loaded with
CREATE FUNCTION and not removed with
DROP FUNCTION. All active functions are
reloaded each time the server starts, unless you start
mysqld with the
--skip-grant-tables option. In this case, UDF
initialization is skipped and UDFs are unavailable.
For instructions on writing user-defined functions, see Section 19.2.4, “Adding a New User-Defined Function”. For the UDF mechanism to work, functions must be written in C or C++ (or another language that can use C calling conventions), your operating system must support dynamic loading and you must have compiled mysqld dynamically (not statically).
AGGREGATE is a new option for MySQL 3.23. An
AGGREGATE function works exactly like a
native MySQL aggregate (summary) function such as
SUM or
COUNT(). For
AGGREGATE to work, your
mysql.func table must contain a
type column. If your
mysql.func table does not have this column,
you should run the mysql_fix_privilege_tables
script to create it.
To upgrade the shared library associated with a UDF, 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 crash.

User Comments
You should quote the library name, but not the function name.
Eg: CREATE FUNCTION myfirst_udf RETURNS INTEGER soname "udf_test.so"
It is not possible to specify a path in the library name. MySQL will search through the standard lib dirs. A good place to put your library is '/usr/lib' or '/usr/local/lib' on linux.
'plugin_dir' must be a directory that is searched by your system's dynamic linker by default or will not work.
For example, on windows if you define
plugin_dir=E:\\MySQL\\MySQL Server 5.1\\plugin
will not work if you don't add it to the PATH environment.
Add your own comment.