MySQL字段类型
我的数据库中有一个表,用于存储用户操作的记录。目前,包含用户 ID 的列设置为 int(11),但是我正在对代码进行一些更改,在其中添加临时用户 ID。
为了区分临时 ID 和常规 ID,我在 id 前面添加了 0。
示例:4——普通用户; 023 -- 临时
但是,当我将此 ID 填充到 ym 表中时,零将被丢弃。我需要将其更改为什么字段类型才能保持所有 ID 完好无损?
I have a table in my db where I store records of user actions. Currently the column that contains user IDs is set to int(11), however i am making some changes to my code where I will be adding temporary user IDs.
To differentiate the temporary IDs from the regular ones, I prepend 0 to the id.
Example: 4 -- regular user; 023 -- temporary
However when I populate this ID into ym table the zero gets discarded. What field type do I need to change it to to keep all IDs in tact?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果你想在 id 前面加上 0 前缀,你可以将其更改为 varchar
但你可能想尝试一下。
添加新列:
然后迁移您的 id
You could change it to an varchar if you want to prefix the id's with a 0
But you might want to try this.
Add a new column:
Then migrate your id's
我认为你必须使用 varchar 字段,但请注意,如果你有一个,这将消除你的 auto_increment 。
I think you'll have to go with a varchar field but note that this will eliminate your auto_increment if you have one.
用户 ID 是一个 int,int 是二进制数。前导零与没有前导零的数字相同。
我建议否定该数字以指示临时 ID。
The user ID is an int and ints are binary numbers. A leading zero is the SAME as the number without a leading zero.
I would suggest negating the number to indicate a temporary id.
你不能在 int 之前添加 0((01 == 1)——大多数情况下,但我不会讨论其中的变幻莫测)。
只需添加一个类型列即可。您稍后可以随时删除该列。
You can't add a 0 before an int ( (01 == 1) -- mostly but I'm not going to get into the vagaries of that).
Just add a type column. You can always drop the column later.