Table 11.19. Miscellaneous Functions
Name | Description |
---|---|
DEFAULT() | Return the default value for a table column |
GET_LOCK() | Get a named lock |
INET_ATON() | Return the numeric value of an IP address |
INET_NTOA() | Return the IP address from a numeric value |
IS_FREE_LOCK() | Checks whether the named lock is free |
IS_USED_LOCK() | Checks whether the named lock is in use. Return connection identifier if true. |
MASTER_POS_WAIT() | Block until the slave has read and applied all updates up to the specified position |
NAME_CONST() | Causes the column to have the given name |
RAND() | Return a random floating-point value |
RELEASE_LOCK() | Releases the named lock |
SLEEP() | Sleep for a number of seconds |
UUID_SHORT() | Return an integer-valued universal identifier |
UUID() | Return a Universal Unique Identifier (UUID) |
VALUES() | Defines the values to be used during an INSERT |
Returns the default value for a table column. An error results if the column has no default value.
mysql>
UPDATE t SET i = DEFAULT(i)+1 WHERE id < 100;
Formats the number
X
to a format like'#,###,###.##'
, rounded toD
decimal places, and returns the result as a string. For details, see Section 11.5, “String Functions”.Tries to obtain a lock with a name given by the string
str
, using a timeout oftimeout
seconds. Returns1
if the lock was obtained successfully,0
if the attempt timed out (for example, because another client has previously locked the name), orNULL
if an error occurred (such as running out of memory or the thread was killed with mysqladmin kill). If you have a lock obtained withGET_LOCK()
, it is released when you executeRELEASE_LOCK()
, execute a newGET_LOCK()
, or your connection terminates (either normally or abnormally). Locks obtained withGET_LOCK()
do not interact with transactions. That is, committing a transaction does not release any such locks obtained during the transaction.This function can be used to implement application locks or to simulate record locks. Names are locked on a server-wide basis. If a name has been locked by one client,
GET_LOCK()
blocks any request by another client for a lock with the same name. This enables clients that agree on a given lock name to use the name to perform cooperative advisory locking. But be aware that it also enables a client that is not among the set of cooperating clients to lock a name, either inadvertently or deliberately, and thus prevent any of the cooperating clients from locking that name. One way to reduce the likelihood of this is to use lock names that are database-specific or application-specific. For example, use lock names of the formdb_name.str
orapp_name.str
.mysql>
SELECT GET_LOCK('lock1',10);
-> 1 mysql>SELECT IS_FREE_LOCK('lock2');
-> 1 mysql>SELECT GET_LOCK('lock2',10);
-> 1 mysql>SELECT RELEASE_LOCK('lock2');
-> 1 mysql>SELECT RELEASE_LOCK('lock1');
-> NULLThe second
RELEASE_LOCK()
call returnsNULL
because the lock'lock1'
was automatically released by the secondGET_LOCK()
call.If multiple clients are waiting for a lock, the order in which they will acquire it is undefined and depends on factors such as the thread library in use. In particular, applications should not assume that clients will acquire the lock in the same order that they issued the lock requests.
NoteBefore MySQL 5.5.3, if a client attempts to acquire a lock that is already held by another client, it blocks according to the
timeout
argument. If the blocked client terminates, its thread does not die until the lock request times out.This function is unsafe for statement-based replication. Beginning with MySQL 5.5.1, a warning is logged if you use this function when
binlog_format
is set toSTATEMENT
. (Bug#47995)Given the dotted-quad representation of a network address as a string, returns an integer that represents the numeric value of the address. Addresses may be 4- or 8-byte addresses.
mysql>
SELECT INET_ATON('209.207.224.40');
-> 3520061480The generated number is always in network byte order. For the example just shown, the number is calculated as 209×2563 + 207×2562 + 224×256 + 40.
INET_ATON()
also understands short-form IP addresses:mysql>
SELECT INET_ATON('127.0.0.1'), INET_ATON('127.1');
-> 2130706433, 2130706433NoteWhen storing values generated by
INET_ATON()
, it is recommended that you use anINT UNSIGNED
column. If you use a (signed)INT
column, values corresponding to IP addresses for which the first octet is greater than 127 cannot be stored correctly. See Section 10.6, “Out-of-Range and Overflow Handling”.Given a numeric network address in network byte order (4 or 8 byte), returns the dotted-quad representation of the address as a string.
As of MySQL 5.5.3, the return value is a nonbinary string in the connection character set. Before 5.5.3, the return value is a binary string.
mysql>
SELECT INET_NTOA(3520061480);
-> '209.207.224.40'Checks whether the lock named
str
is free to use (that is, not locked). Returns1
if the lock is free (no one is using the lock),0
if the lock is in use, andNULL
if an error occurs (such as an incorrect argument).This function is unsafe for statement-based replication. Beginning with MySQL 5.5.1, a warning is logged if you use this function when
binlog_format
is set toSTATEMENT
. (Bug#47995)Checks whether the lock named
str
is in use (that is, locked). If so, it returns the connection identifier of the client that holds the lock. Otherwise, it returnsNULL
.This function is unsafe for statement-based replication. Beginning with MySQL 5.5.1, a warning is logged if you use this function when
binlog_format
is set toSTATEMENT
. (Bug#47995)MASTER_POS_WAIT(
log_name
,log_pos
[,timeout
])This function is useful for control of master/slave synchronization. It blocks until the slave has read and applied all updates up to the specified position in the master log. The return value is the number of log events the slave had to wait for to advance to the specified position. The function returns
NULL
if the slave SQL thread is not started, the slave's master information is not initialized, the arguments are incorrect, or an error occurs. It returns-1
if the timeout has been exceeded. If the slave SQL thread stops whileMASTER_POS_WAIT()
is waiting, the function returnsNULL
. If the slave is past the specified position, the function returns immediately.If a
timeout
value is specified,MASTER_POS_WAIT()
stops waiting whentimeout
seconds have elapsed.timeout
must be greater than 0; a zero or negativetimeout
means no timeout.This function is unsafe for statement-based replication. Beginning with MySQL 5.5.1, a warning is logged if you use this function when
binlog_format
is set toSTATEMENT
. (Bug#47995)Returns the given value. When used to produce a result set column,
NAME_CONST()
causes the column to have the given name. The arguments should be constants.mysql>
SELECT NAME_CONST('myname', 14);
+--------+ | myname | +--------+ | 14 | +--------+This function is for internal use only. The server uses it when writing statements from stored programs that contain references to local program variables, as described in Section 19.7, “Binary Logging of Stored Programs”, You might see this function in the output from mysqlbinlog.
Releases the lock named by the string
str
that was obtained withGET_LOCK()
. Returns1
if the lock was released,0
if the lock was not established by this thread (in which case the lock is not released), andNULL
if the named lock did not exist. The lock does not exist if it was never obtained by a call toGET_LOCK()
or if it has previously been released.The
DO
statement is convenient to use withRELEASE_LOCK()
. See Section 12.2.3, “DO
Syntax”.This function is unsafe for statement-based replication. Beginning with MySQL 5.5.1, a warning is logged if you use this function when
binlog_format
is set toSTATEMENT
. (Bug#47995)Sleeps (pauses) for the number of seconds given by the
duration
argument, then returns 0. IfSLEEP()
is interrupted, it returns 1. The duration may have a fractional part given in microseconds.This function is unsafe for statement-based replication. Beginning with MySQL 5.5.1, a warning is logged if you use this function when
binlog_format
is set toSTATEMENT
. (Bug#47995)Returns a Universal Unique Identifier (UUID) generated according to “DCE 1.1: Remote Procedure Call” (Appendix A) CAE (Common Applications Environment) Specifications published by The Open Group in October 1997 (Document Number C706, http://www.opengroup.org/public/pubs/catalog/c706.htm).
A UUID is designed as a number that is globally unique in space and time. Two calls to
UUID()
are expected to generate two different values, even if these calls are performed on two separate computers that are not connected to each other.A UUID is a 128-bit number represented by a
utf8
string of five hexadecimal numbers inaaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee
format:The first three numbers are generated from a timestamp.
The fourth number preserves temporal uniqueness in case the timestamp value loses monotonicity (for example, due to daylight saving time).
The fifth number is an IEEE 802 node number that provides spatial uniqueness. A random number is substituted if the latter is not available (for example, because the host computer has no Ethernet card, or we do not know how to find the hardware address of an interface on your operating system). In this case, spatial uniqueness cannot be guaranteed. Nevertheless, a collision should have very low probability.
Currently, the MAC address of an interface is taken into account only on FreeBSD and Linux. On other operating systems, MySQL uses a randomly generated 48-bit number.
mysql>
SELECT UUID();
-> '6ccd780c-baba-1026-9564-0040f4311e29'WarningThe
UUID()
function returns a string using the character set defined by thecharacter_set_server
parameter. If you are using UUID values in your tables and these columns are indexed the character set of your column or table should match the character set used when theUUID()
was called. If you do not use the same character set for the column and the UUID value, the indexes on those columns will not be used, which may lead to a reduction in performance and locked tables during operations as the table is searched sequentially for the value.You can convert between different character sets when using UUID-based strings using the
CONVERT()
function.NoteUUID()
does not work with statement-based replication.Returns a “short” universal identifier as a 64-bit unsigned integer (rather than a string-form 128-bit identifier as returned by the
UUID()
function).The value of
UUID_SHORT()
is guaranteed to be unique if the following conditions hold:The
server_id
of the current host is unique among your set of master and slave serversserver_id
is between 0 and 255You do not set back your system time for your server between mysqld restarts
You do not invoke
UUID_SHORT()
on average more than 16 million times per second between mysqld restarts
The
UUID_SHORT()
return value is constructed this way:(server_id & 255) << 56 + (server_startup_time_in_seconds << 24) + incremented_variable++;
mysql>
SELECT UUID_SHORT();
-> 92395783831158784Note that
UUID_SHORT()
does not work with statement-based replication.In an
INSERT ... ON DUPLICATE KEY UPDATE
statement, you can use theVALUES(
function in thecol_name
)UPDATE
clause to refer to column values from theINSERT
portion of the statement. In other words,VALUES(
in thecol_name
)UPDATE
clause refers to the value ofcol_name
that would be inserted, had no duplicate-key conflict occurred. This function is especially useful in multiple-row inserts. TheVALUES()
function is meaningful only in theON DUPLICATE KEY UPDATE
clause ofINSERT
statements and returnsNULL
otherwise. See Section 12.2.5.3, “INSERT ... ON DUPLICATE KEY UPDATE
Syntax”.mysql>
INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)
->ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);