問題描述
我有一個(gè) MySQL 表,其主鍵字段開啟了 AUTO_INCREMENT.在閱讀了這里的其他帖子后,我注意到有人遇到了同樣的問題,答案也各不相同.有些人建議不要使用此功能,有些人則表示無法修復(fù)".
I have a MySQL table with a primary key field that has AUTO_INCREMENT on. After reading other posts on here I've noticed people with the same problem and with varied answers. Some recommend not using this feature, others state it can't be 'fixed'.
我有:
table: course
fields: courseID, courseName
示例:表中的記錄數(shù):18.如果我刪除記錄 16、17 和 18 - 我希望輸入的下一條記錄的 courseID 為 16,但是它將是 19,因?yàn)樽詈筝斎氲?courseID 是 18.
Example: number of records in the table: 18. If I delete records 16, 17 and 18 - I would expect the next record entered to have the courseID of 16, however it will be 19 because the last entered courseID was 18.
我的 SQL 知識(shí)并不驚人,但是否可以通過查詢(或 phpMyAdmin 界面中的設(shè)置)刷新或更新此計(jì)數(shù)?
My SQL knowledge isn't amazing but is there anyway to refresh or update this count with a query (or a setting in the phpMyAdmin interface)?
此表將與數(shù)據(jù)庫中的其他表相關(guān)聯(lián).
This table will relate to others in a database.
考慮到所有建議,我決定忽略這個(gè)問題".我將簡單地刪除和添加記錄,同時(shí)讓自動(dòng)增量完成它的工作.我想數(shù)字是什么并不重要,因?yàn)樗鼉H用作唯一標(biāo)識(shí)符并且沒有(如上所述)業(yè)務(wù)含義.
Given all the advice, I have decided to ignore this 'problem'. I will simply delete and add records whilst letting the auto increment do it's job. I guess it doesn't really matter what the number is since it's only being used as a unique identifier and doesn't have a (as mentioned above) business meaning.
對于那些我可能對我的原始帖子感到困惑的人:我不想使用這個(gè)字段來知道我有多少記錄.我只是想讓數(shù)據(jù)庫看起來整潔,并具有更多的一致性.
For those who I may have confused with my original post: I do not wish to use this field to know how many records I have. I just wanted the database to look neat and have a bit more consistency.
推薦答案
您嘗試做的事情聽起來很危險(xiǎn),因?yàn)槟遣皇?AUTO_INCREMENT
的預(yù)期用途.
What you're trying to do sounds dangerous, as that's not the intended use of AUTO_INCREMENT
.
如果您真的想找到最低的未使用密鑰值,請完全不要使用 AUTO_INCREMENT
,并手動(dòng)管理您的密鑰.但是,這不是推薦的做法.
If you really want to find the lowest unused key value, don't use AUTO_INCREMENT
at all, and manage your keys manually. However, this is NOT a recommended practice.
退一步問為什么需要回收鍵值?" 未簽名的INT
(或BIGINT
)是否不提供足夠大的密鑰空間?
Take a step back and ask "why you need to recycle key values?" Do unsigned INT
(or BIGINT
) not provide a large enough key space?
你真的會(huì)擁有更多 18,446,744,073,709,551,615
在應(yīng)用程序的整個(gè)生命周期中的唯一記錄?
Are you really going to have more than 18,446,744,073,709,551,615
unique records over the course of your application's lifetime?
這篇關(guān)于MySQL刪除后自動(dòng)遞增的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!