数据库记录属性的整数与字符 [英] Integer vs char for DB record property

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

问题描述

说我有一张有房地产列表的表。每个列表可以是出售或出租。因此,我可以将For sale映射为0,For rent映射为1,并将其作为INT存储在数据库中。然而,如果我在CHAR类型的字段中将其存储为sale/rent,则会更具描述性。或者我可以将0和1映射到我的程序中的两个常量FOR_SALE和FOR_RENT。或者使用chars'S'和'R'。在数据库中存储这些属性的最佳做法是,一个属性的选项总数非常小。

Say I have a table with real estate listings. Every listing can be either 'For sale' or 'For rent'. Therefore, I can map 'For sale' to 0, 'For rent' to 1 and store it as an INT in the database. However, it would be much more descriptive if I store it as 'sale' / 'rent' in a field of type CHAR. Or I can map 0 and 1 to two constants FOR_SALE and FOR_RENT in my program. Or use chars 'S' and 'R'. What are the best practices for storing such properties in a database with a condition that the total number of options for one such property is very small.

推荐答案

您应该使用char(1)或int(取决于选项的数量),并将值映射到常量字符串,这样你就可以节省空间,字符串将很容易配置在未来:)

You should use a char(1) or an int (depending upon the number of options) and map the values to constant strings, this way you'll save space and the strings will be easily configurable in the future :)

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

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