首页 > 数据库 >mysql中truncate表对auto_increment的影响

mysql中truncate表对auto_increment的影响

时间:2023-08-26 18:55:45浏览次数:45  
标签:abce 表对 truncate auto rows sec test root localhost


在mysql中,如果对表执行truncate操作后,会重新设置auto_increment的值,比如:

root@localhost (none)>use abce;
Database changed
root@localhost abce>create table test(id int not null auto_increment primary key,age int);
Query OK, 0 rows affected (0.02 sec)

root@localhost abce>insert into test(age) values(3),(4),(5);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

root@localhost abce>select * from test;
+----+------+
| id | age  |
+----+------+
|  1 |    3 |
|  2 |    4 |
|  3 |    5 |
+----+------+
3 rows in set (0.00 sec)

root@localhost abce>

对表执行truncate操作:

root@localhost abce>truncate table test;
Query OK, 0 rows affected (0.02 sec)

root@localhost abce>insert into test(age) values(6),(7),(8);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

root@localhost abce>select * from test;
+----+------+
| id | age  |
+----+------+
|  1 |    6 |
|  2 |    7 |
|  3 |    8 |
+----+------+
3 rows in set (0.01 sec)

root@localhost abce>

可以看到自增列又从1重新开始了。
但是有时候,因为数据之间的依赖等原因,不希望重复使用相同的自增值作为主键。这个时候可以使用alter table来重新设置auto_increment的值。例如:

root@localhost abce>truncate table test;
Query OK, 0 rows affected (0.03 sec)

root@localhost abce>alter table test auto_increment=4;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

root@localhost abce>insert into test(age) values(6),(7),(8);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

root@localhost abce>select * from test;
+----+------+
| id | age  |
+----+------+
|  4 |    6 |
|  5 |    7 |
|  6 |    8 |
+----+------+
3 rows in set (0.01 sec)

root@localhost abce>

 

标签:abce,表对,truncate,auto,rows,sec,test,root,localhost
From: https://www.cnblogs.com/abclife/p/17659059.html

相关文章

  • windows 桌面GUI自动化- 18.pywinauto 保存控件菜单树结构print_control_identifiers(
    前言.pywinauto可以使用print_control_identifiers()方法打印控件菜单树结构,这对我们查找控件非常方便。print_control_identifiers()查看相关源码defprint_control_identifiers(self,depth=None,filename=None):"""Printsthe'identifiers'......
  • Kali Linux 2023.3 发布(内部基础设施和 Kali Autopilot 重大更新)
    KaliLinux2023.3发布(内部基础设施和KaliAutopilot重大更新)请访问原文链接:https://sysin.org/blog/kali-linux-2023/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.org更新说明2023年8月23日,星期三KaliLinux2023.3发布(内部基础设施和Kali自动驾驶仪)......
  • windows 桌面GUI自动化- 17.pywinauto 设置全局等待时间Timings
    前言pywinauto查找窗口和控件时会有超时时间与轮询机制,可以通过timings模块设置全局等待时间。timings模块timings模块有三个模式可以设置timings.Timings.fast()快速模式timings.Timings.defaults()默认模式timings.Timings.slow()慢速模式以下是可以调整的......
  • 使用 AutoGPTQ 和 transformers 让大语言模型更轻量化
    大语言模型在理解和生成人类水平的文字方面所展现出的非凡能力,正在许多领域带来应用上的革新。然而,在消费级硬件上训练和部署大语言模型的需求也变得越来越难以满足。......
  • .NET6 使用 AutoMapper
    一、Net6环境下的.netcore项目里如何使用AutoMapper实现依赖注入。注: AutoMapper是一个对象-对象映射器,可以将一个对象映射到另一个对象。第一步,在Nuget引入AutoMapper、AutoMapper.Extensions.DependencyInjection这两个NuGet包 第二步,定义Profile,方便......
  • .NET6 配置 AutoMapper 与 AutoFac
    AutoMapper一、Net6环境下的.netcore项目里如何使用AutoMapper实现依赖注入。注:AutoMapper是一个对象-对象映射器,可以将一个对象映射到另一个对象。第一步,在Nuget引入AutoMapper、AutoMapper.Extensions.DependencyInjection这两个NuGet包 第二步,定义Prof......
  • windows 桌面GUI自动化- 16.pywinauto 鼠标操作与滚动列表
    前言pywinauto操作鼠标,滚动查看鼠标点击操作单击鼠标左键,coords参数是点击的坐标位置frompywinautoimportmouse#单击鼠标左键mouse.click(coords=(100,100))鼠标右击mouse.right_click(coords=(100,200))鼠标双击mouse.double_click(coords=(100,200))鼠标......
  • uiautomator2 截图+压缩图片+放入allure报告中
    defsave_screenshot(self,screenshot_path):"""截图保存到某个路径:paramscreenshot_path::return:"""self.d.screenshot(screenshot_path) screenshot_path=f&quo......
  • Autofac 基本使用
    背景:.Net6.0常规依赖注入需要不断修改Program.cs文件需求:实现Repository、IRepository、Razor动态生成并依赖注入(AutoBuildHelper先记录未优化)引入Autofac、Autofac.Extensions.DependencyInjectionProgram.cs添加以下代码:builder.Host.UseServiceProviderFactory(newAuto......
  • Autofac报错No constructors on type 'xxx' can be found with the constructor finde
    Noconstructorsontype'JK.TitanData.Repository.Fact_Sales_GMVTargetTotalRepository'canbefoundwiththeconstructorfinder'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'.Seehttps://autofac.rtfd.io/help/no-construct......