Table 12.20 Information Functions信息函数
BENCHMARK() | |
CHARSET() | |
COERCIBILITY() | |
COLLATION() | |
CONNECTION_ID() | |
CURRENT_ROLE() | |
CURRENT_USER() , CURRENT_USER | |
DATABASE() | |
FOUND_ROWS() | |
ICU_VERSION() | |
LAST_INSERT_ID() | |
ROLES_GRAPHML() | |
ROW_COUNT() | |
SCHEMA() | DATABASE() 的同义词 |
SESSION_USER() | USER() 的同义词 |
SYSTEM_USER() | USER() 的同义词 |
USER() | |
VERSION() |
The BENCHMARK()
function executes the expression expr
repeatedly count
times. BENCHMARK()
函数的作用是:将表达式expr
重复执行count
次。It may be used to time how quickly MySQL processes the expression. 它可以用来计时MySQL处理表达式的速度。The result value is 结果值为0
, or NULL
for inappropriate arguments such as a NULL
or negative repeat count.0
,对于不适当的参数(如NULL
或负重复计数),结果值为NULL
。
The intended use is from within the mysql client, which reports query execution times:预期用途来自mysql客户端,它报告查询执行时间:
mysql> SELECT BENCHMARK(1000000,AES_ENCRYPT('hello','goodbye'));
+---------------------------------------------------+
| BENCHMARK(1000000,AES_ENCRYPT('hello','goodbye')) |
+---------------------------------------------------+
| 0 |
+---------------------------------------------------+
1 row in set (4.74 sec)
The time reported is elapsed time on the client end, not CPU time on the server end. 报告的时间是客户端的运行时间,而不是服务器端的CPU时间。It is advisable to execute 建议多次执行BENCHMARK()
several times, and to interpret the result with regard to how heavily loaded the server machine is.BENCHMARK()
,并根据服务器的负载情况来解释结果。
BENCHMARK()
is intended for measuring the runtime performance of scalar expressions, which has some significant implications for the way that you use it and interpret the results:BENCHMARK()
用于测量标量表达式的运行时性能,这对使用标量表达式和解释结果的方式有一些重要影响:
Only scalar expressions can be used. 只能使用标量表达式。Although the expression can be a subquery, it must return a single column and at most a single row. 尽管表达式可以是子查询,但它必须返回单个列,最多只能返回单个行。For example, 例如,如果表BENCHMARK(10, (SELECT * FROM t))
fails if the table t
has more than one column or more than one row.t
有多个列或多个行,则BENCHMARK(10, (SELECT * FROM t))
失败。
Executing a 执行SELECT
statement expr
N
times differs from executing SELECT BENCHMARK(
in terms of the amount of overhead involved. N
, expr
)SELECT
语句expr
N
次与执行SELECT BENCHMARK(
的开销量不同。N
, expr
)The two have very different execution profiles and you should not expect them to take the same amount of time. 这两种方法有非常不同的执行配置文件,您不应该期望它们花费相同的时间。The former involves the parser, optimizer, table locking, and runtime evaluation 前者涉及解析器、优化器、表锁定和运行时求值,各求值N
times each. N
次。The latter involves only runtime evaluation 后者只涉及N
times, and all the other components just once. N
次运行时求值,而所有其他组件只涉及一次。Memory structures already allocated are reused, and runtime optimizations such as local caching of results already evaluated for aggregate functions can alter the results. 已经分配的内存结构将被重用,运行时优化(如已为聚合函数计算的结果的本地缓存)可以更改结果。Use of 因此,BENCHMARK()
thus measures performance of the runtime component by giving more weight to that component and removing the “noise” introduced by the network, parser, optimizer, and so forth.BENCHMARK()
的使用通过赋予运行时组件更多的权重并消除网络、解析器、优化器等引入的“噪声”来衡量该组件的性能。
Returns the character set of the string argument.返回字符串参数的字符集。
mysql>SELECT CHARSET('abc');
-> 'utf8' mysql>SELECT CHARSET(CONVERT('abc' USING latin1));
-> 'latin1' mysql>SELECT CHARSET(USER());
-> 'utf8'
Returns the collation coercibility value of the string argument.返回字符串参数的排序规则强制值。
mysql>SELECT COERCIBILITY('abc' COLLATE utf8_swedish_ci);
-> 0 mysql>SELECT COERCIBILITY(USER());
-> 3 mysql>SELECT COERCIBILITY('abc');
-> 4 mysql>SELECT COERCIBILITY(1000);
-> 5
The return values have the meanings shown in the following table. 返回值的含义如下表所示。Lower values have higher precedence.较低的值具有较高的优先级。
0 | COLLATE clauseCOLLATE 子句的值 | |
---|---|---|
1 | ||
2 | ||
3 | USER() return valueUSER() 返回值 | |
4 | ||
5 | ||
5 | NULL or an expression derived from NULL NULL 或从NULL 派生的表达式 |
For more information, see Section 10.8.4, “Collation Coercibility in Expressions”.有关更多信息,请参阅第10.8.4节,“表达式中的排序规则强制性”。
Returns the collation of the string argument.返回字符串参数的排序规则。
mysql>SELECT COLLATION('abc');
-> 'utf8_general_ci' mysql>SELECT COLLATION(_utf8mb4'abc');
-> 'utf8mb4_0900_ai_ci' mysql>SELECT COLLATION(_latin1'abc');
-> 'latin1_swedish_ci'
Returns the connection ID (thread ID) for the connection. 返回连接的连接ID(线程ID)。Every connection has an ID that is unique among the set of currently connected clients.每个连接都有一个ID,该ID在当前连接的客户机集中是唯一的。
The value returned by CONNECTION_ID()
is the same type of value as displayed in the ID
column of the INFORMATION_SCHEMA.PROCESSLIST
table, the Id
column of SHOW PROCESSLIST
output, and the PROCESSLIST_ID
column of the Performance Schema threads
table.CONNECTION_ID()
返回的值与INFORMATION_SCHEMA.PROCESSLIST
表的ID
列、SHOW PROCESSLIST
输出的ID
列和实施架构threads
表的PROCESSLIST_ID
列中显示的值类型相同。
mysql> SELECT CONNECTION_ID();
-> 23786
Changing the session value of the 更改ppseudo_thread_id
system variable changes the value returned by the CONNECTION_ID()
function.seudo_thread_id
系统变量的会话值将更改CONNECTION_id()
函数返回的值。
Returns a 返回一个utf8
string containing the current active roles for the current session, separated by commas, or NONE
if there are none. utf8
字符串,其中包含当前会话的当前活动角色,用逗号分隔;如果没有活动角色,则返回NONE
。The value reflects the setting of the 该值反映了sql_quote_show_create
system variable.sql_quote_show_create
系统变量的设置。
Suppose that an account is granted roles as follows:假设帐户被授予以下角色:
GRANT 'r1', 'r2' TO 'u1'@'localhost'; SET DEFAULT ROLE ALL TO 'u1'@'localhost';
In sessions for 在u1
, the initial CURRENT_ROLE()
value names the default account roles. u1
的会话中,初始CURRENT_ROLE()
值命名默认帐户角色。Using 使用SET ROLE
changes that:SET ROLE
更改:
mysql>SELECT CURRENT_ROLE();
+-------------------+ | CURRENT_ROLE() | +-------------------+ | `r1`@`%`,`r2`@`%` | +-------------------+ mysql>SET ROLE 'r1'; SELECT CURRENT_ROLE();
+----------------+ | CURRENT_ROLE() | +----------------+ | `r1`@`%` | +----------------+
Returns the user name and host name combination for the MySQL account that the server used to authenticate the current client. 返回服务器用于验证当前客户端的MySQL帐户的用户名和主机名组合。This account determines your access privileges. 此帐户决定您的访问权限。The return value is a string in the 返回值是utf8
character set.utf8
字符集中的字符串。
The value of CURRENT_USER()
can differ from the value of USER()
.CURRENT_USER()
的值可以与USER()
的值不同。
mysql>SELECT USER();
-> 'davida@localhost' mysql>SELECT * FROM mysql.user;
ERROR 1044: Access denied for user ''@'localhost' to database 'mysql' mysql>SELECT CURRENT_USER();
-> '@localhost'
The example illustrates that although the client specified a user name of 该示例说明,尽管客户端指定了davida
(as indicated by the value of the USER()
function), the server authenticated the client using an anonymous user account (as seen by the empty user name part of the CURRENT_USER()
value). davida
的用户名(如USER()
函数的值所示),但服务器使用匿名用户帐户对客户端进行了身份验证(如CURRENT_USER()
值的空用户名部分所示)。One way this might occur is that there is no account listed in the grant tables for 一种可能发生这种情况的方法是,在davida
.davida
的授权表中没有列出账户。
Within a stored program or view, 在存储程序或视图中,CURRENT_USER()
returns the account for the user who defined the object (as given by its DEFINER
value) unless defined with the SQL SECURITY INVOKER
characteristic. CURRENT_USER()
返回定义对象的用户的帐户(由其DEFINER
值给定),除非使用SQL SECURITY INVOKER
特性定义。In the latter case, 在后一种情况下,CURRENT_USER()
returns the object's invoker.CURRENT_USER()
返回对象的调用程序。
Triggers and events have no option to define the 触发器和事件没有定义SQL SECURITY
characteristic, so for these objects, CURRENT_USER()
returns the account for the user who defined the object. SQL SECURITY
特性的选项,因此对于这些对象,CURRENT_USER()
返回定义该对象的用户的帐户。To return the invoker, use 要返回调用程序,请使用USER()
or SESSION_USER()
.USER()
或SESSION_USER()
。
The following statements support use of the 下面的语句支持使用CURRENT_USER()
function to take the place of the name of (and, possibly, a host for) an affected user or a definer; in such cases, CURRENT_USER()
is expanded where and as needed:CURRENT_USER()
函数来代替受影响的用户或定义者的名称(可能还有一个主机);在这种情况下,CURRENT_USER()
会根据需要在以下位置展开:
For information about the implications that this expansion of 有关CURRENT_USER()
has for replication, see Section 17.5.1.8, “Replication of CURRENT_USER()”.CURRENT_USER()
的此扩展对复制的影响的信息,请参阅第17.5.1.8节,“CURRENT_USER()的副本”。
Returns the default (current) database name as a string in the 以utf8
character set. utf8
字符集中的字符串形式返回默认(当前)数据库名称。If there is no default database, 如果没有默认数据库,DATABASE()
returns NULL
. DATABASE()
将返回NULL
。Within a stored routine, the default database is the database that the routine is associated with, which is not necessarily the same as the database that is the default in the calling context.在存储的例程中,默认数据库是与例程关联的数据库,它不一定与调用上下文中的默认数据库相同。
mysql> SELECT DATABASE();
-> 'test'
If there is no default database, 如果没有默认数据库,DATABASE()
returns NULL
.DATABASE()
将返回NULL
。
The 从MySQL8.0.17开始,SQL_CALC_FOUND_ROWS
query modifier and accompanying FOUND_ROWS()
function are deprecated as of MySQL 8.0.17; expect them to be removed in a future version of MySQL. SQL_CALC_FOUND_ROWS
查询修饰符和附带的FOUND_ROWS()
函数就不再使用了;希望在MySQL的未来版本中删除它们。As a replacement, considering executing your query with 作为替代,考虑使用LIMIT
, and then a second query with COUNT(*)
and without LIMIT
to determine whether there are additional rows. LIMIT
执行查询,然后使用COUNT(*)
而不用LIMIT
执行第二个查询,以确定是否有其他行。For example, instead of these queries:例如,不要用这些查询:
SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name
WHERE id > 100 LIMIT 10;
SELECT FOUND_ROWS();
Use these queries instead:请改用这种查询:
SELECT * FROMtbl_name
WHERE id > 100 LIMIT 10; SELECT COUNT(*) FROMtbl_name
WHERE id > 100;
COUNT(*)
is subject to certain optimizations. COUNT(*)
会受到某些优化的影响。SQL_CALC_FOUND_ROWS
causes some optimizations to be disabled.SQL_CALC_FOUND_ROWS
导致禁用某些优化。
A SELECT
statement may include a LIMIT
clause to restrict the number of rows the server returns to the client. SELECT
语句可以包含LIMIT
子句来限制服务器返回给客户机的行数。In some cases, it is desirable to know how many rows the statement would have returned without the 在某些情况下,最好知道该语句不用LIMIT
, but without running the statement again. LIMIT
将返回多少行,但不必再次运行该语句。To obtain this row count, include an 要获取此行计数,请在SQL_CALC_FOUND_ROWS
option in the SELECT
statement, and then invoke FOUND_ROWS()
afterward:SELECT
语句中包含一个SQL_CALC_FOUND_ROWS
选项,然后调用FOUND_ROWS()
:
mysql>SELECT SQL_CALC_FOUND_ROWS * FROM
->tbl_name
WHERE id > 100 LIMIT 10;
mysql>SELECT FOUND_ROWS();
The second 第二个SELECT
returns a number indicating how many rows the first SELECT
would have returned had it been written without the LIMIT
clause.SELECT
返回一个数字,指示如果第一个SELECT
在没有LIMIT
子句的情况下被写入,它将返回多少行。
In the absence of the 如果在最近一次成功的SQL_CALC_FOUND_ROWS
option in the most recent successful SELECT
statement, FOUND_ROWS()
returns the number of rows in the result set returned by that statement. SELECT
语句中没有SQL_CALC_FOUND_ROWS
选项,FOUND_ROWS()
将返回该语句返回的结果集中的行数。If the statement includes a 如果语句包含LIMIT
clause, FOUND_ROWS()
returns the number of rows up to the limit. LIMIT
子句,则FOUND_ROWS()
返回达到限制的行数。For example, 例如,如果语句包含FOUND_ROWS()
returns 10 or 60, respectively, if the statement includes LIMIT 10
or LIMIT 50, 10
.LIMIT 10
或LIMIT 50,10
,则FOUND_ROWS()
分别返回10或60。
The row count available through 通过FOUND_ROWS()
is transient and not intended to be available past the statement following the SELECT SQL_CALC_FOUND_ROWS
statement. FOUND_ROWS()
可用的行计数是暂时的,不适用于SELECT SQL_CALC_FOUND_ROWS
语句后面的语句。If you need to refer to the value later, save it:如果以后需要引用该值,请保存它:
mysql>SELECT SQL_CALC_FOUND_ROWS * FROM ... ;
mysql>SET @rows = FOUND_ROWS();
If you are using 如果您使用的是SELECT SQL_CALC_FOUND_ROWS
, MySQL must calculate how many rows are in the full result set. SELECT SQL_CALC_FOUND_ROWS
,MySQL必须计算完整结果集中有多少行。However, this is faster than running the query again without 但是,这比不带LIMIT
, because the result set need not be sent to the client.LIMIT
地再次运行查询要快,因为结果集不需要发送到客户机。
当您要限制查询返回的行数,但也要确定完整结果集中的行数而无需再次运行查询时,SQL_CALC_FOUND_ROWS
and FOUND_ROWS()
can be useful in situations when you want to restrict the number of rows that a query returns, but also determine the number of rows in the full result set without running the query again. SQL_CALC_FOUND_ROWS
和FOUND_ROWS()
非常有用。An example is a Web script that presents a paged display containing links to the pages that show other sections of a search result. 一个例子是一个Web脚本,它显示一个页面显示,其中包含指向显示搜索结果其他部分的页面的链接。Using 使用FOUND_ROWS()
enables you to determine how many other pages are needed for the rest of the result.find_ROWS()
可以确定结果的其余部分还需要多少其他页面。
The use of 与简单的SQL_CALC_FOUND_ROWS
and FOUND_ROWS()
is more complex for UNION
statements than for simple SELECT
statements, because LIMIT
may occur at multiple places in a UNION
. SELECT
语句相比,对于UNION
语句,SQL_CALC_FOUND_ROWS
和FOUND_ROWS()
的使用更为复杂,因为LIMIT
可能发生在UNION
中的多个位置。It may be applied to individual 它可以应用于SELECT
statements in the UNION
, or global to the UNION
result as a whole.UNION
中的单个SELECT
语句,也可以作为一个整体应用于UNION
结果。
The intent of SQL_CALC_FOUND_ROWS
for UNION
is that it should return the row count that would be returned without a global LIMIT
. UNION
的SQL_CALC_FOUND_ROWS
的目的是它应该返回没有全局限制的行计数。The conditions for use of 使用SQL_CALC_FOUND_ROWS
with UNION
are:SQL_CALC_FOUND_ROWS
配合UNION
的条件是:
The SQL_CALC_FOUND_ROWS
keyword must appear in the first SELECT
of the UNION
.SQL_CALC_FOUND_ROWS
关键字必须出现在UNION
的第一个SELECT
中。
The value of 只有使用FOUND_ROWS()
is exact only if UNION ALL
is used. UNION ALL
时,FOUND_ROWS()
的值才是精确的。/ins>If 如果使用UNION
without ALL
is used, duplicate removal occurs and the value of FOUND_ROWS()
is only approximate.UNION
而不用ALL
,则会删除重复项,并且FOUND_ROWS()
的值仅为近似值。
If no 如果LIMIT
is present in the UNION
, SQL_CALC_FOUND_ROWS
is ignored and returns the number of rows in the temporary table that is created to process the UNION
.UNION
中没有LIMIT
,则忽略SQL_CALC_FOUND_ROWS
行,并返回为处理UNION
而创建的临时表中的行数。
Beyond the cases described here, the behavior of 除了这里描述的情况之外,FOUND_ROWS()
is undefined (for example, its value following a SELECT
statement that fails with an error).FOUND_ROWS()
的行为是未定义的(例如,它的值在SELECT
语句之后出现错误)。
FOUND_ROWS()
is not replicated reliably using statement-based replication. FOUND_ROWS()
使用基于语句的复制无法可靠地复制。This function is automatically replicated using row-based replication.此函数使用基于行的复制自动复制。
The version of the International Components for Unicode (ICU) library used to support regular expression operations (see Section 12.8.2, “Regular Expressions”). 用于支持正则表达式操作的国际Unicode组件(ICU)库的版本(请参阅第12.8.2节“正则表达式”)。This function is primarily intended for use in test cases.此函数主要用于测试用例。
LAST_INSERT_ID()
, LAST_INSERT_ID(
expr
)
With no argument, 在没有参数的情况下,LAST_INSERT_ID()
returns a BIGINT UNSIGNED
(64-bit) value representing the first automatically generated value successfully inserted for an AUTO_INCREMENT
column as a result of the most recently executed INSERT
statement. LAST_INSERT_ID()
返回一个BIGINT UNSIGNED
(64位)值,该值表示作为最近执行的INSERT
语句的结果,为自动增量列成功插入的第一个自动生成的值。The value of 如果没有成功插入行,则LAST_INSERT_ID()
remains unchanged if no rows are successfully inserted.LAST_INSERT_ID()
的值保持不变。
With an argument, 对于参数,LAST_INSERT_ID()
returns an unsigned integer.LAST_INSERT_ID()
返回一个无符号整数。
For example, after inserting a row that generates an 例如,插入生成AUTO_INCREMENT
value, you can get the value like this:AUTO_INCREMENT
值的行后,可以得到如下值:
mysql> SELECT LAST_INSERT_ID();
-> 195
The currently executing statement does not affect the value of 当前执行的语句不影响LAST_INSERT_ID()
. LAST_INSERT_ID()
的值。Suppose that you generate an 假设您用一条语句生成一个AUTO_INCREMENT
value with one statement, and then refer to LAST_INSERT_ID()
in a multiple-row INSERT
statement that inserts rows into a table with its own AUTO_INCREMENT
column. AUTO_INCREMENT
值,然后在多行INSERT
语句中引用LAST_INSERT_ID()
,该语句将行插入到具有自己的AUTO_INCREMENT
列的表中。The value of 在第二条语句中,LAST_INSERT_ID()
remains stable in the second statement; its value for the second and later rows is not affected by the earlier row insertions. LAST_INSERT_ID()
的值保持稳定;第二行和后面行的值不受前面行插入的影响。(You should be aware that, if you mix references to (您应该知道,如果将对LAST_INSERT_ID()
and LAST_INSERT_ID(
, the effect is undefined.)expr
)LAST_INSERT_ID()
和LAST_INSERT_ID(
的引用混合使用,则效果是未定义的。)expr
)
If the previous statement returned an error, the value of 如果上一条语句返回错误,则LAST_INSERT_ID()
is undefined. LAST_INSERT_ID()
的值未定义。For transactional tables, if the statement is rolled back due to an error, the value of 对于事务表,如果由于错误而回滚语句,则LAST_INSERT_ID()
is left undefined. LAST_INSERT_ID()
的值将保持未定义状态。For manual 对于手动ROLLBACK
, the value of LAST_INSERT_ID()
is not restored to that before the transaction; it remains as it was at the point of the ROLLBACK
.ROLLBACK
,LAST_INSERT_ID()
的值不会恢复到事务之前的值;它保持在ROLLBACK
点的状态。
Within the body of a stored routine (procedure or function) or a trigger, the value of 在存储例程(过程或函数)或触发器的主体中,LAST_INSERT_ID()
changes the same way as for statements executed outside the body of these kinds of objects. LAST_INSERT_ID()
的值的更改方式与在这类对象的主体外部执行的语句的更改方式相同。The effect of a stored routine or trigger upon the value of 存储的例程或触发器对以下语句显示的LAST_INSERT_ID()
that is seen by following statements depends on the kind of routine:LAST_INSERT_ID()
的值的影响取决于例程的类型:
If a stored procedure executes statements that change the value of 如果存储过程执行的语句更改了LAST_INSERT_ID()
, the changed value is seen by statements that follow the procedure call.LAST_INSERT_ID()
的值,则更改的值将由过程调用后面的语句看到。
For stored functions and triggers that change the value, the value is restored when the function or trigger ends, so statements coming after it do not see a changed value.对于更改值的存储函数和触发器,值在函数或触发器结束时恢复,因此后面的语句看不到更改的值。
The ID that was generated is maintained in the server on a per-connection basis. 生成的ID在服务器中按每个连接进行维护。This means that the value returned by the function to a given client is the first 这意味着函数返回给给定客户机的值是为影响该客户机的AUTO_INCREMENT
value generated for most recent statement affecting an AUTO_INCREMENT
column by that client. AUTO_INCREMENT
列的最新语句生成的第一个AUTO_INCREMENT
值。This value cannot be affected by other clients, even if they generate 此值不受其他客户端的影响,即使它们生成自己的AUTO_INCREMENT
values of their own. AUTO_INCREMENT
值。This behavior ensures that each client can retrieve its own ID without concern for the activity of other clients, and without the need for locks or transactions.这种行为确保每个客户机都可以检索自己的ID,而不必关心其他客户机的活动,也不需要锁或事务。
The value of 如果将行的LAST_INSERT_ID()
is not changed if you set the AUTO_INCREMENT
column of a row to a non-“magic” value (that is, a value that is not NULL
and not 0
).AUTO_INCREMENT
列设置为非“magic”值(即不为NULL
且不为0的值),则不会更改LAST_INSERT_ID()
的值。
If you insert multiple rows using a single 如果使用单个INSERT
statement, LAST_INSERT_ID()
returns the value generated for the first inserted row only. INSERT
语句插入多行,LAST_insert_ID()
将仅返回为第一个插入行生成的值。The reason for this is to make it possible to reproduce easily the same 这样做的原因是可以对其他服务器轻松地复制相同的INSERT
statement against some other server.INSERT
语句。
For example:例如:
mysql>USE test;
mysql>CREATE TABLE t (
id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
name VARCHAR(10) NOT NULL
);
mysql>INSERT INTO t VALUES (NULL, 'Bob');
mysql>SELECT * FROM t;
+----+------+ | id | name | +----+------+ | 1 | Bob | +----+------+ mysql>SELECT LAST_INSERT_ID();
+------------------+ | LAST_INSERT_ID() | +------------------+ | 1 | +------------------+ mysql>INSERT INTO t VALUES
(NULL, 'Mary'), (NULL, 'Jane'), (NULL, 'Lisa');
mysql>SELECT * FROM t;
+----+------+ | id | name | +----+------+ | 1 | Bob | | 2 | Mary | | 3 | Jane | | 4 | Lisa | +----+------+ mysql>SELECT LAST_INSERT_ID();
+------------------+ | LAST_INSERT_ID() | +------------------+ | 2 | +------------------+
Although the second 尽管第二个INSERT
statement inserted three new rows into t
, the ID generated for the first of these rows was 2
, and it is this value that is returned by LAST_INSERT_ID()
for the following SELECT
statement.INSERT
语句在t
中插入了三个新行,但是为第一行生成的ID是2
,并且正是这个值由跟着SELECT
语句的LAST_INSERT_ID()
返回。
If you use 如果使用INSERT IGNORE
and the row is ignored, the LAST_INSERT_ID()
remains unchanged from the current value (or 0 is returned if the connection has not yet performed a successful INSERT
) and, for non-transactional tables, the AUTO_INCREMENT
counter is not incremented. INSERT IGNOR
E并忽略该行,则LAST_INSERT_ID()
将保持当前值不变(如果连接尚未成功执行插入,则返回0),并且对于非事务性表,AUTO_INCREMENT
计数器不会递增。For 对于InnoDB
tables, the AUTO_INCREMENT
counter is incremented if innodb_autoinc_lock_mode
is set to 1
or 2
, as demonstrated in the following example:InnoDB
表,如果innodb_autoinc_lock_mode
设置为1
或2
,则AUTO_INCREMENT
计数器将递增,如下例所示:
mysql>USE test;
mysql>SELECT @@innodb_autoinc_lock_mode;
+----------------------------+ | @@innodb_autoinc_lock_mode | +----------------------------+ | 1 | +----------------------------+ mysql>CREATE TABLE `t` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`val` INT(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `i1` (`val`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
# Insert two rows mysql>INSERT INTO t (val) VALUES (1),(2);
# With auto_increment_offset=1, the inserted rows # result in an AUTO_INCREMENT value of 3 mysql>SHOW CREATE TABLE t\G
*************************** 1. row *************************** Table: t Create Table: CREATE TABLE `t` ( `id` int(11) NOT NULL AUTO_INCREMENT, `val` int(11) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `i1` (`val`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1 # LAST_INSERT_ID() returns the first automatically generated # value that is successfully inserted for the AUTO_INCREMENT column mysql>SELECT LAST_INSERT_ID();
+------------------+ | LAST_INSERT_ID() | +------------------+ | 1 | +------------------+ # The attempted insertion of duplicate rows fail but errors are ignored mysql>INSERT IGNORE INTO t (val) VALUES (1),(2);
Query OK, 0 rows affected (0.00 sec) Records: 2 Duplicates: 2 Warnings: 0 # With innodb_autoinc_lock_mode=1, the AUTO_INCREMENT counter # is incremented for the ignored rows mysql>SHOW CREATE TABLE t\G
*************************** 1. row *************************** Table: t Create Table: CREATE TABLE `t` ( `id` int(11) NOT NULL AUTO_INCREMENT, `val` int(11) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `i1` (`val`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1 # The LAST_INSERT_ID is unchanged because the previous insert was unsuccessful mysql>SELECT LAST_INSERT_ID();
+------------------+ | LAST_INSERT_ID() | +------------------+ | 1 | +------------------+
For more information, see Section 15.6.1.6, “AUTO_INCREMENT Handling in InnoDB”.有关更多信息,请参阅第15.6.1.6节,“InnoDB中的自动增量处理”。
If 如果expr
is given as an argument to LAST_INSERT_ID()
, the value of the argument is returned by the function and is remembered as the next value to be returned by LAST_INSERT_ID()
. expr
作为LAST_INSERT_ID()
的参数提供,则该参数的值将由函数返回,并作为LAST_INSERT_ID()
返回的下一个值被记住。This can be used to simulate sequences:这可用于模拟序列:
Create a table to hold the sequence counter and initialize it:创建一个表来保存序列计数器并对其进行初始化:
mysql>CREATE TABLE sequence (id INT NOT NULL);
mysql>INSERT INTO sequence VALUES (0);
Use the table to generate sequence numbers like this:使用该表生成如下序列号:
mysql>UPDATE sequence SET id=LAST_INSERT_ID(id+1);
mysql>SELECT LAST_INSERT_ID();
The UPDATE
statement increments the sequence counter and causes the next call to LAST_INSERT_ID()
to return the updated value. UPDATE
语句递增序列计数器,并使下一次调用LAST_INSERT_ID()
返回更新的值。The SELECT
statement retrieves that value. SELECT
语句检索该值。The mysql_insert_id()
C API function can also be used to get the value. mysql_insert_id()
C API函数也可以用来获取值。See mysql_insert_id().请参阅mysql_insert_id()
。
You can generate sequences without calling 您可以在不调用LAST_INSERT_ID()
, but the utility of using the function this way is that the ID value is maintained in the server as the last automatically generated value. LAST_INSERT_ID()
的情况下生成序列,但这样使用函数的实用性在于,ID值在服务器中作为最后一个自动生成的值进行维护。It is multi-user safe because multiple clients can issue the 它是多用户安全的,因为多个客户端可以发出UPDATE
statement and get their own sequence value with the SELECT
statement (or mysql_insert_id()
), without affecting or being affected by other clients that generate their own sequence values.UPDATE
语句并使用SELECT
语句(或mysql_insert_id()
)获取自己的序列值,而不会影响或受到生成自己序列值的其他客户端的影响。
Note that 请注意,mysql_insert_id()
is only updated after INSERT
and UPDATE
statements, so you cannot use the C API function to retrieve the value for LAST_INSERT_ID(
after executing other SQL statements like expr
)SELECT
or SET
.mysql_insert_id()
只在INSERT
和UPDATE
语句之后更新,因此在执行其他SQL语句(如SELECT
或SET
)之后,不能使用C API函数检索LAST_INSERT_ID(
的值。expr
)
Returns a 返回一个utf8
string containing a GraphML document representing memory role subgraphs. utf8
字符串,其中包含表示内存角色子图的GraphML文档。The 要查看ROLE_ADMIN
privilege (or the deprecated SUPER
privilege) is required to see content in the <graphml>
element. <graphml>
元素中的内容,需要使用ROLE_ADMIN
权限(或不推荐使用的SUPER
权限)。Otherwise, the result shows only an empty element:否则,结果仅显示空元素:
mysql> SELECT ROLES_GRAPHML();
+---------------------------------------------------+
| ROLES_GRAPHML() |
+---------------------------------------------------+
| <?xml version="1.0" encoding="UTF-8"?><graphml /> |
+---------------------------------------------------+
ROW_COUNT()
returns a value as follows:ROW_COUNT()
返回如下值:
DDL statements: 0. DDL语句:0。This applies to statements such as 这适用于诸如CREATE TABLE
or DROP TABLE
.CREATE TABLE
或DROP TABLE
之类的语句。
DML statements other than 除SELECT
: The number of affected rows. SELECT
之外的DML语句:受影响的行数。This applies to statements such as 这适用于诸如UPDATE
, INSERT
, or DELETE
(as before), but now also to statements such as ALTER TABLE
and LOAD DATA
.UPDATE
、INSERT
或DELETE
之类的语句(与以前一样),但现在也适用于诸如ALTER TABLE
和LOAD DATA
之类的语句。
SELECT
: -1 if the statement returns a result set, or the number of rows “affected” if it does not. SELECT
:如果语句返回结果集,则返回-1,否则返回“受影响”的行数。For example, for 例如,对于SELECT * FROM t1
, ROW_COUNT()
returns -1. SELECT * FROM t1
,ROW_COUNT()
返回-1。For 对于SELECT * FROM t1 INTO OUTFILE '
, file_name
'ROW_COUNT()
returns the number of rows written to the file.SELECT * FROM t1 INTO OUTFILE '
,file_name
'ROW_COUNT()
返回写入文件的行数。
SIGNAL
statements: 0.SIGNAL
语句:0。
For 对于UPDATE
statements, the affected-rows value by default is the number of rows actually changed. UPDATE
语句,默认情况下,“受影响的行”值是实际更改的行数。If you specify the 如果在连接mysqld时将CLIENT_FOUND_ROWS
flag to mysql_real_connect()
when connecting to mysqld, the affected-rows value is the number of rows “found”; that is, matched by the WHERE
clause.CLIENT_FOUND_ROWS
标志指定为mysql_real_connect()
,则受影响的ROWS值是“找到”的行数;也就是说,匹配WHERE
子句的行数。
For 对于REPLACE
statements, the affected-rows value is 2 if the new row replaced an old row, because in this case, one row was inserted after the duplicate was deleted.REPLACE
语句,如果新行替换了旧行,则受影响的行值为2,因为在这种情况下,在删除重复行之后插入了一行。
For 对于INSERT ... ON DUPLICATE KEY UPDATE
statements, the affected-rows value per row is 1 if the row is inserted as a new row, 2 if an existing row is updated, and 0 if an existing row is set to its current values. INSERT ... ON DUPLICATE KEY UPDATE
语句,如果将行作为新行插入,则每行受影响的行值为1;如果更新现有行,则每行受影响的行值为2;如果将现有行设置为其当前值,则每行受影响的行值为0。If you specify the 如果指定CLIENT_FOUND_ROWS
flag, the affected-rows value is 1 (not 0) if an existing row is set to its current values.CLIENT_FOUND_ROWS
标志,则如果现有行设置为其当前值,则受影响的行值为1(而不是0)。
The ROW_COUNT()
value is similar to the value from the mysql_affected_rows()
C API function and the row count that the mysql client displays following statement execution.ROW_COUNT()
值类似于mysql_impacted_rows()
C API函数的值,以及mysql客户机在执行语句后显示的行数。
mysql>INSERT INTO t VALUES(1),(2),(3);
Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql>SELECT ROW_COUNT();
+-------------+ | ROW_COUNT() | +-------------+ | 3 | +-------------+ 1 row in set (0.00 sec) mysql>DELETE FROM t WHERE i IN(1,2);
Query OK, 2 rows affected (0.00 sec) mysql>SELECT ROW_COUNT();
+-------------+ | ROW_COUNT() | +-------------+ | 2 | +-------------+ 1 row in set (0.00 sec)
ROW_COUNT()
is not replicated reliably using statement-based replication. ROW_COUNT()
使用基于语句的复制无法可靠地复制。This function is automatically replicated using row-based replication.使用基于行的复制自动复制此函数。
This function is a synonym for 此函数是DATABASE()
.DATABASE()
的同义词。
SESSION_USER()
is a synonym for USER()
.SESSION_USER()
是USER()
的同义词。
SYSTEM_USER()
is a synonym for USER()
.SYSTEM_USER()
是USER()
的同义词。
The SYSTEM_USER()
function is distinct from the SYSTEM_USER
privilege. SYSTEM_USER()
函数不同于SYSTEM_USER
权限。The former returns the current MySQL account name. 前者返回当前MySQL帐户名。The latter distinguishes the system user and regular user account categories (see Section 6.2.11, “Account Categories”).后者区分系统用户和常规用户帐户类别(见第6.2.11节“帐户类别”)。
Returns the current MySQL user name and host name as a string in the 以utf8
character set.utf8
字符集中的字符串形式返回当前MySQL用户名和主机名。
mysql> SELECT USER();
-> 'davida@localhost'
The value indicates the user name you specified when connecting to the server, and the client host from which you connected. 该值表示连接到服务器时指定的用户名,以及连接的客户端主机。The value can be different from that of 该值可以与CURRENT_USER()
.CURRENT_USER()
的值不同。
Returns a string that indicates the MySQL server version. 返回指示MySQL服务器版本的字符串。The string uses the 字符串使用utf8
character set. utf8
字符集。The value might have a suffix in addition to the version number. 除了版本号之外,该值可能还有一个后缀。See the description of the 有关版本系统变量的说明,请参见第5.1.8节,“服务器系统变量”。version
system variable in Section 5.1.8, “Server System Variables”.
This function is unsafe for statement-based replication. 此函数对于基于语句的复制不安全。A warning is logged if you use this function when 如果在binlog_format
is set to STATEMENT
.binlog_format
设置为STATEMENT
时使用此函数,则会记录警告。
mysql> SELECT VERSION();
-> '8.0.25-standard'