将整数作为字符串存储在数据库中,有什么缺点吗?[英] Any drawbacks to storing an integer as a string in a database?

本文是小编为大家收集整理的关于将整数作为字符串存储在数据库中,有什么缺点吗?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

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

推荐答案

除非你真的需要整数的特性(也就是算术的能力),那么最好将产品 ID 存储为字符串.您永远不需要做任何事情,比如将两个产品 ID 相加,或者计算一组产品 ID 的平均值,因此不需要实际的数字类型.

将产品 ID 存储为字符串不太可能在性能上造成可衡量的差异.虽然存储大小会略有增加,但产品 ID 字符串的大小可能会比数据库行中的其他数据小得多.

如果数据提供者决定开始使用字母或符号字符,现在将产品 ID 存储为字符串将为您省去很多麻烦.没有真正的缺点.

本文地址:https://www.itbaoku.cn/post/597643.html

问题描述

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?

推荐答案

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.

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.

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.