MySQL/自增主键:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
无编辑摘要
Kangkk​(留言 | 贡献)
第49行: 第49行:
=== identity ===
=== identity ===
{{Shc_out|<syntaxhighlight lang="mysql">
{{Shc_out|<syntaxhighlight lang="mysql">
select @@IDENTITY;</syntaxhighlight>|<syntaxhighlight lang="bash">
SELECT @@IDENTITY;</syntaxhighlight>|<syntaxhighlight lang="bash">
+------------+
+------------+
| @@IDENTITY |
| @@IDENTITY |

2022年2月7日 (一) 08:11的版本

标题文字

自增主键id

查询自增主键id

LAST_INSERT_ID

mysql>
SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
|                0 |
+------------------+
1 row in set (0.00 sec)
LAST_INSERT_ID 基于 Connection。

max

mysql>
select max(id) from user;
+---------+
| max(id) |
+---------+
|      14 |
+---------+
1 row in set (0.01 sec)

存储过程

mysql>
DELIMITER $$
DROP PROCEDURE IF EXISTS `test` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `test`(in name varchar(100), out oid int)
BEGIN
  select max(id) from user into oid;
  select oid;
END $$
DELIMITER ;
call test('', @id);
+------+
| oid  |
+------+
|   14 |
+------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

identity

mysql>
SELECT @@IDENTITY;
+------------+
| @@IDENTITY |
+------------+
|          0 |
+------------+
1 row in set (0.00 sec)