数据库中的整数与字符串 [英] Integer vs String in database

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

问题描述

在数据库中定义数据类型时,我一直在选择使用整数还是字符串来存储某些数字"数据时遇到问题.

When defining datatypes in a database, I have always had a problem with choosing whether to use integers or strings to store certain 'numerical' data.

假设我正在构建 然而 另一个 地址 Book 并且有一个邮政编码字段.如果邮政编码始终是一个 4 位数字,我应该将其存储为哪种数据类型?整数还是字符串?从技术上讲,它是一个整数,但我没有对其进行任何类型的计算,我只是将它吐到表格中.如果我想按邮政编码对表格进行排序,您的意见会改变吗?

Say I am building Yet Another Address Book and there is a post code field. Provided that post codes are always a 4 digit number, which data type do I store it as? Integer or string? Technically it is an integer, but I'm not doing any sort of calculations on it, I'm just spitting it out into a table. Would your opinion change if I want to sort the table by post code?

现在,我不傻.我确实认识到对整数的有效需求,例如页面浏览量和唯一用户或登录用户和访客用户.但是如何存储一个 torrent 中有多少文件呢?整数还是字符串?

Now, I'm not stupid. I do recognize a valid need for integers, such as page views and unique users or logged in users and guest users. But what about for storing how many files are in a torrent? Integer or string?

推荐答案

在我的国家,邮政编码也总是 4 位数字.但第一个数字可以为零.

In my country, post-codes are also always 4 digits. But the first digit can be zero.

如果将0700"存储为整数,会出现很多问题:

If you store "0700" as an integer, you can get a lot of problems:

  • 它可以读为八进制值
  • 如果它被正确读取为十进制值,它会变成700"
  • 当你得到值700"时,你一定要记得加上零
  • 我你不加零,以后你怎么知道700"是0700",还是有人打错了7100"?

从技术上讲,我们的邮政编码实际上是字符串,即使它总是 4 位数字.

Technically, our post codes is actually strings, even if it is always 4 digits.

您可以将它们存储为整数,以节省空间.但请记住,这是一个简单的数据库技巧,并注意前导零.

You can store them as integers, to save space. But remember this is a simple DB-trick, and be careful about leading zeroes.

但是对于存储多少文件在种子中?整数或字符串?

But what about for storing how many files are in a torrent? Integer or string?

这显然是一个整数.

这篇关于数据库中的整数与字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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