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

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
无编辑摘要
第27行: 第27行:


=== 存储过程 ===
=== 存储过程 ===
{{Shc_out|<syntaxhighlight lang="bash" highlight="8">
{{Shc_out|<syntaxhighlight lang="mysql" highlight="8">
DELIMITER $$
DELIMITER $$
DROP PROCEDURE IF EXISTS `test` $$
DROP PROCEDURE IF EXISTS `test` $$
第45行: 第45行:


Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
</syntaxhighlight>|>mysql}}
</syntaxhighlight>|mysql>}}


=== identity ===
=== identity ===
{{Shc_out|<syntaxhighlight lang="bash">
{{Shc_out|<syntaxhighlight lang="mysql">
select @@IDENTITY;</syntaxhighlight>|<syntaxhighlight lang="bash">
select @@IDENTITY;</syntaxhighlight>|<syntaxhighlight lang="bash">
+------------+
+------------+
第56行: 第56行:
+------------+
+------------+
1 row in set (0.00 sec)
1 row in set (0.00 sec)
</syntaxhighlight>|$|/home/kangkk}}
</syntaxhighlight>|mysql>}}

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

标题文字

自增主键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)