Mysql中Identity 详细介绍
更新时间:2016年10月20日 09:02 点击:2345
假如表中包含一列为auto_increment,
如果是Myisam类型的引擎,那么在删除了最新一笔数据,无论是否重启Mysql,下一次插入之后仍然会使用上次删除的最大ID+1.
mysql> create table test_myisam (id int not null auto_increment primary key, name char(5)) engine=myisam; Query OK, 0 rows affected (0.04 sec) mysql> insert into test_myisam (name) select ‘a‘; Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> insert into test_myisam (name) select ‘b‘; Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> insert into test_myisam (name) select ‘c‘; Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> insert into test_myisam (name) select name from test_myisam; Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from test_myisam; +----+------+ | id | name | +----+------+ | 1 | a | | 2 | b | | 3 | c | | 4 | a | | 5 | b | | 6 | c | +----+------+ 6 rows in set (0.00 sec) mysql> delete from test_myisam where id=6; Query OK, 1 row affected (0.00 sec)
mysql> insert into test_myisam(name) select ‘d‘; Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> select * from test_myisam; +----+------+ | id | name | +----+------+ | 1 | a | | 2 | b | | 3 | c | | 4 | a | | 5 | b | | 7 | d | +----+------+ 6 rows in set (0.00 sec)
下面是对Innodb表的测试。
mysql> create table test_innodb(id int not null auto_increment primary key, name char(5)) engine=innodb; Query OK, 0 rows affected (0.26 sec) mysql> insert into test_innodb (name)select ‘a‘; Query OK, 1 row affected (0.06 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> insert into test_innodb (name)select ‘b‘; Query OK, 1 row affected (0.06 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> insert into test_innodb (name)select ‘c‘; Query OK, 1 row affected (0.07 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> select * from test_innodb; +----+------+ | id | name | +----+------+ | 1 | a | | 2 | b | | 3 | c | +----+------+ 3 rows in set (0.00 sec) mysql> delete from test_innodb where id=3; Query OK, 1 row affected (0.05 sec) mysql> insert into test_innodb (name)select ‘d‘; Query OK, 1 row affected (0.20 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> select * from test_innodb; +----+------+ | id | name | +----+------+ | 1 | a | | 2 | b | | 4 | d | +----+------+ 3 rows in set (0.00 sec) mysql> exit Bye [2@a data]$ mysql -uroot -pwsdad Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server version: 5.5.37-log Source distribution Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> use wison Database changed mysql> delete from test_innodb where id=4; Query OK, 1 row affected (0.07 sec) mysql> exit Bye [2@a data]$ sudo service mysql restart Shutting down MySQL... SUCCESS! Starting MySQL.. SUCCESS! [2@a data]$ mysql -uroot -pwison Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.37-log Source distribution Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> use wison Database changed mysql> insert into test_innodb (name) select ‘z‘; Query OK, 1 row affected (0.07 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> select * from test_innodb; +----+------+ | id | name | +----+------+ | 1 | a | | 2 | b | | 3 | z | +----+------+ 3 rows in set (0.00 sec)
可以看到在mysql数据库没有重启时,innodb的表新插入数据会是之前被删除的数据再加1.
但是当Mysql服务被重启后,再向InnodB的自增表表里插入数据,那么会使用当前Innodb表里的最大的自增列再加1.
原因:
Myisam类型存储引擎的表将最大的ID值是记录到数据文件中,不管是否重启最大的ID值都不会丢失。但是InnoDB表的最大的ID值是存在内存中的,若不重启Mysql服务,新加入数据会使用内存中最大的数据+1.但是重启之后,会使用当前表中最大的值再+1
感谢阅读此文,希望能帮助到大家,谢谢大家对本站的支持!
相关文章
- 这篇文章主要介绍了MySQL性能监控软件Nagios的安装及配置教程,这里以CentOS操作系统为环境进行演示,需要的朋友可以参考下...2015-12-14
- 新版 Mysql 中加入了对 JSON Document 的支持,可以创建 JSON 类型的字段,并有一套函数支持对JSON的查询、修改等操作,下面就实际体验一下...2016-08-23
深入研究mysql中的varchar和limit(容易被忽略的知识)
为什么标题要起这个名字呢?commen sence指的是那些大家都应该知道的事情,但往往大家又会会略这些东西,或者对这些东西一知半解,今天我总结下自己在mysql中遇到的一些commen sense类型的问题。 ...2015-03-15- 这篇文章主要介绍了MySQL 字符串拆分操作(含分隔符的字符串截取),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-22
- 一、先说一下为什么要分表:当一张的数据达到几百万时,你查询一次所花的时间会变多,如果有联合查询的话,有可能会死在那儿了。分表的目的就在于此,减小数据库的负担,缩短查询时间。根据个人经验,mysql执行一个sql的过程如下:1...2014-05-31
- 我们自己鼓捣mysql时,总免不了会遇到这个问题:插入中文字符出现乱码,虽然这是运维先给配好的环境,但是在自己机子上玩的时候咧,总得知道个一二吧,不然以后如何优雅的吹牛B。...2015-03-15
- 这几天在centos下装mysql,这里记录一下安装的过程,方便以后查阅Mysql5.5.37安装需要cmake,5.6版本开始都需要cmake来编译,5.5以后的版本应该也要装这个。安装cmake复制代码 代码如下: [root@local ~]# wget http://www.cm...2015-03-15
- 宿主机使用网线的时候,客户机在Bridged Adapter模式下,使用Atheros AR8131 PCI-E Gigabit Ethernet Controller上网没问题。 宿主机使用无线的时候,客户机在Bridged Adapter模式下,使用可选项里唯一一个WIFI选项,Microsoft Virtual Wifi Miniport Adapter也无法上网,故弃之。...2013-09-19
- 首先要声明一点,大部分情况下,修改MySQL密码是需要有mysql里的root权限的...2013-09-11
- MySQL命令行导出数据库: 1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录 如我输入的命令行:cd C:/Program Files/MySQL/MySQL Server 4.1/bin (或者直接将windows的环境变量path中添加该目录) ...2013-09-26
- 一、连接Mysql格式: mysql -h主机地址 -u用户名 -p用户密码1、连接到本机上的MYSQL。首先打开DOS窗口,然后进入目录mysql/bin,再键入命令mysql -u root -p,回车后提示你输密码.注意用户名前可以有空格也可以没有空格,但是密...2015-11-08
- Navicat for MySQL注册码用来激活 Navicat for MySQL 软件,只要拥有 Navicat 注册码就能激活相应的 Navicat 产品。这篇文章主要介绍了Navicat for MySQL 11注册码\激活码汇总,需要的朋友可以参考下...2020-11-23
- 这篇文章主要介绍了mysql IS NULL使用索引案例讲解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...2021-08-14
- 这篇文章主要介绍了基于PostgreSQL和mysql数据类型对比兼容,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-12-25
- 一、准备编译环境,安装所需依赖包yum groupinstall 'Development' -y yum install openssl openssl-devel zlib zlib-devel -y yum install readline-devel pcre-devel ncurses-devel bison-devel cmake -y二、编译安...2015-10-21
Mysql中 show table status 获取表信息的方法
这篇文章主要介绍了Mysql中 show table status 获取表信息的方法的相关资料,需要的朋友可以参考下...2016-03-12- 这篇文章主要为大家分享了20分钟MySQL基础入门教程,快速掌握MySQL基础知识,真正了解MySQL,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-12-02
- 传统的关系数据库一般由数据库(database)、表(table)、记录(record)三个层次概念组成,MongoDB是由数据库(database)、集合(collection)、文档对象(document)三个层次组成。MongoDB对于关系型数据库里的表,但是集合中没有列、行和关...2013-09-11
- 这篇文章主要介绍了Delphi远程连接Mysql的实现方法,需要的朋友可以参考下...2020-06-30
- 这篇文章主要为大家详细介绍了MySQL远程连接不上的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2017-01-26