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

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

问题描述

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

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.

建立 其他 地址 < a href =http://www.peacecomputers.com/addressbook/step7.html =noreferrer> 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 / li>
  • 当您获取值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.


但是存储多少
文件是在torrent?整数或
字符串?

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

这显然是整数。

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

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