在数据库中存储整数作为字符串的任何缺点? [英] Any drawbacks to storing an integer as a string in a database?

查看:171
本文介绍了在数据库中存储整数作为字符串的任何缺点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要存储的产品的id值。现在他们都是整数,但我不确定数据提供者在未来会引入字母或符号到混合,所以我在辩论是否现在将其存储为整数或字符串。

I have id values for products that I need store. Right now they are all integers, but I'm not sure if the data provider in the future will introduce letters or symbols into that mix, so I'm debating whether to store it now as integer or string.

将值保存为字符串是否有性能或其他缺点?

Are there performance or other disadvantages to saving the values as strings?

推荐答案

需要一个整数的功能(即执行算术的能力),那么最好将产品ID存储为字符串。您永远不需要执行任何操作,例如将两个产品ID添加在一起,或计算一组产品ID的平均值,因此不需要实际的数字类型。

Unless you really need the features of an integer (that is, the ability to do arithmetic), then it is probably better for you to store the product IDs as strings. You will never need to do anything like add two product IDs together, or compute the average of a group of product IDs, so there is no need for an actual numeric type.

将产品ID存储为字符串不太可能导致性能的可衡量差异。虽然存储大小会略有增加,但产品ID字符串的大小可能远小于数据库行中其余部分的数据。

It is unlikely that storing product IDs as strings will cause a measurable difference in performance. While there will be a slight increase in storage size, the size of a product ID string is likely to be much smaller than the data in the rest of your database row anyway.

如果数据提供者决定开始使用字母或符号字符,今天将产品ID存储为字符串将会为您节省很多时间。没有真正的缺点。

Storing product IDs as strings today will save you much pain in the future if the data provider decides to start using alphabetic or symbol characters. There is no real downside.

这篇关于在数据库中存储整数作为字符串的任何缺点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆