php中mysql每月自动从1开始递增
我有一个解决方案,我需要一个表中的 id 从 1 开始每个月。我正在考虑使用月份名称作为主键以及 id 自动递增。我是不是方法错了?有人有什么想法吗?
I have solution where i need that id in one table starts every month from 1. I'm thinking of using month name as primary key together with id auto incerement. Am I wrong way? Does someone have any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MYISAM 表的示例:
Example with MYISAM table:
确切地。
NB 的评论应该是一个答案。在这里引用一下:
每个网络程序员新手都应该把它刻在桌子上,并每天大声朗读 3 遍。
Exactly.
A comment from the N.B. should be an answer. Quoting it here:
Every newbie web-programmer should engrave it on their table and read aloud for the 3 times a day.
主键的特点是它们通常需要简单。大多数系统使用数字的原因是因为在代码中,您只需传递数字来表示数据片段,而不必请求所有数据(如果不需要)。
您可以拥有一个多列主键,其中一列是月年字符串,另一列是数字自动增量,那么自动增量将在月份更改时自动重用数字。这样做的优点是可以直接绑定到表定义中,从而使系统迁移变得更加容易。
来源:输入此处链接说明
The thing with primary keys is that they need to be simple, normally. the reason most systems use numbers is because then in code, you just pass the number around to represent the piece of data, with out necessarily requesting all the data, if you dont need it.
You can have a multi-column primary key with one column being a month-year string and another being a numerical auto-increment, then the auto-increment will automatically re-use numbers when the month changes. This has the advantage of being tied directly into the table definition, which makes moving the system easier.
Source: enter link description here