自动递增-每年自动重置 [英] Auto-increment - automatic reset for each year
问题描述
MySQL/InnoDB
MySQL/InnoDB
就我而言,我的收据应按年计算; 1/2015、2/2015 ... 556/2015等.明年到来时,计数器应从1重新开始,并且收据应计为1/2016、2/2016 ...
In my case my receipts should be counted on yearly basis; 1/2015, 2/2015 ... 556/2015 and so on. When next year comes, the counter should start from 1 again and receipts should be counted as 1/2016, 2/2016 ...
如何定义auto_increment字段,该字段将每年重置一次?
How to define auto_increment field which will reset itself on yearly basis?
RCID | RCNO | RCYEAR | ...
=====+======+========+====
200 | 1 | 2015 |
201 | 2 | 2015 |
... | ... | 2015 |
756 | 556 | 2015 | <- last receipt in 2015
757 | 1 | 2016 | <- yearly counter restarted
注意:RCID是标准PK自动递增字段.
NOTE: RCID is standard PK auto incremented field.
推荐答案
这不是AUTO_INCREMENT
的正确用法.如果您想要像您描述的那样特别的东西,请自己动手.
This is not a proper usage of AUTO_INCREMENT
. If you want something special like you describe, do it yourself.
MyISAM具有类似的功能(PRIMARY KEY中的第二列可能是类似AUTO_INCREMENT的).但是,如今,InnoDB成为首选. 我的博客
MyISAM has a feature like that (the second column in a PRIMARY KEY could be AUTO_INCREMENT like that). But InnoDB is preferred these days. A way to simulate it is in my blog
这篇关于自动递增-每年自动重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!