当整数列上的 auto_increment 达到数据库中的 max_value 时会发生什么? [英] What happens when auto_increment on integer column reaches the max_value in databases?

查看:26
本文介绍了当整数列上的 auto_increment 达到数据库中的 max_value 时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施一个数据库应用程序,我将同时使用 JavaDB 和 MySQL 作为数据库.我的表中有一个 ID 列,其类型为整数,我使用数据库 auto_increment-function 作为值.

I am implementing a database application and I will use both JavaDB and MySQL as database. I have an ID column in my tables that has integer as type and I use the databases auto_increment-function for the value.

但是当我收到超过 2(或 4)0 亿个帖子并且整数不够时会发生什么?整数是溢出并继续还是抛出了我可以处理的异常?

But what happens when I get more than 2 (or 4) billion posts and integer is not enough? Is the integer overflowed and continues or is an exception thrown that I can handle?

是的,我可以更改为 long 作为数据类型,但是我如何检查何时需要?而且我认为如果我使用 long 作为 ID 列的数据类型,则获取 last_inserted_id() 函数会出现问题.

Yes, I could change to long as datatype, but how do I check when that is needed? And I think there is problem with getting the last_inserted_id()-functions if I use long as datatype for the ID-column.

推荐答案

Jim Martin 来自 §3.6.9.MySQL 文档的使用 AUTO_INCREMENT":

Jim Martin's comment from §3.6.9. "Using AUTO_INCREMENT" of the MySQL documentation:

以防万一有任何问题,AUTO_INCREMENT 字段/DOES NOT WRAP/.一旦达到字段大小的限制,INSERT 就会产生错误.(根据杰里米科尔的说法)

Just in case there's any question, the AUTO_INCREMENT field /DOES NOT WRAP/. Once you hit the limit for the field size, INSERTs generate an error. (As per Jeremy Cole)

使用 MySQL 5.1.45 进行快速测试导致错误:

A quick test with MySQL 5.1.45 results in an error of:

ERROR 1467 (HY000): 无法从存储引擎读取自增值

ERROR 1467 (HY000): Failed to read auto-increment value from storage engine

您可以在插入时测试该错误并采取适当的措施.

You could test for that error on insert and take appropriate action.

这篇关于当整数列上的 auto_increment 达到数据库中的 max_value 时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆