问题描述
我有一个表,其中我已在 10000 处启动自动增量 ID,以便我保留 10000 个值以供管理员手动插入值.但是,当插入 MyTable(ID,Name,Value) VALUES(500,"Test","Test") 时,我做了一个选择,它忽略了我给它的 ID 并推入下一个 10,000 范围.有关解决此问题的任何建议或可能有什么问题?上面的代码当然是伪代码,但如果没有意义,我可以给出一个真实的代码示例.
i have a table in which i have started the auto increment id at 10000 so that i have 10000 values reserved for manual insertion of values from admins. However when doing an insert into MyTable(ID,Name,Value) VALUES(500,"Test","Test") i do a select and it ignored my ID i gave it and pushes in into the next 10,000 range. Any suggestions on fixing this or what may be wrong? The code above is of course pseudo but i can give a real code example if it doesnt make sense.
推荐答案
好的,在你得到问题的答案之前,我必须警告你关于你正在尝试做的极其不良做法.不要误会我的意思,很多人都试图做你正在做的事情,但这根本不是事情应该做的.
Ok, before you get the answer to your question I have to warn you about extremely bad practice you're trying to do there. Don't get me wrong, many have tried to do what you're doing and it's simply not the way things should work.
您的自动递增 ID 是主键.主键用于唯一标识表中的一行.就是这样.除此之外没有其他特殊含义.
Your auto_incremented ID is a primary key. Primary key is used to uniquely identify a row in a table. That's it. It has no other special meaning besides that.
那对你来说意味着什么?这意味着您将为管理员保留"1 - 10k 的想法是错误的.为什么不好?因为您正在篡改主键.您永远不应该决定主键的值应该是什么,出于多种原因(例如一致性),这是数据库的工作.另一个不好的原因是您将某人限制为只有 10k 个可能的条目.另一方面,您将如何计算管理员的下一个条目是什么?如果您输入了 1、2、3、4,然后删除了 ID = 3 的条目怎么办?那会发生什么?你的下一个序列值是什么?3 还是 5?
So what does that mean for you? It means that your idea that you will "reserve" 1 - 10k for admins is bad. Why is it bad? Because you're tampering with the primary key. You should never decide what the value of primary key should be, that's databases' job for many reasons (consistency for example).The other thing why it's bad is that you have limited someone to only 10k possible entries.On the other hand, how will you calculate what the next entry for admins is? What if you have entered 1, 2, 3, 4 and then you delete entry with ID = 3? What happens then? What's your next in sequence value? 3 or 5?
话虽如此,您可能应该重新考虑您的策略.为什么不添加一个字段isAdmin"来告诉您管理员是否发布了某些内容?
Having said that, you should probably rethink your strategy. Why not add a field "isAdmin" that will tell you whether an admin posted something or not?
这篇关于插入自动增量字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!