问题描述
类似的问题:
我们正在设计一个新功能:我们将存储人们生日的月份和日子,但不是一年。所以圣诞节诞生的用户将有生日12/25。问题是,我们如何最好地在postgres中代表这一点?
We're designing a new feature: we'll be storing the month and day parts of people's birthdays, but not the year. So a user born on christmas will have the birthday "12/25". The question is, how can we best represent this in postgres?
-
date
专栏需要一年的时间,并且也禁止非法治疗中的闰年。我们可以将任何闰年的所有生日存储在一起。'1972-12-25'
,但这将是一个kludge。 - 我们可以使用两个
int
列,一个月,一个为一年,但是我们将完全失去pg的内置日期检查,您可以存储日期'02 -31'
。 - 我们可以使用一个
文本
列,但是查询这样做不会很漂亮。 / li>
- The
date
column requires a year, and also rejects leapdays in non-leapyears. We could store all birthdays with an arbitrary leap year, e.g.'1972-12-25'
, but that'd be a bit of a kludge. - We could use two
int
columns, one for month and one for year, but we'd lose pg's built in date checking entirely, and you could store the date'02-31'
. - We could use a single
text
column, but querying against this would not be pretty.
有没有我们缺少的选项?我们目前正在倾斜一对整数列。
Are there any options we're missing? We're currently leaning toward a pair of integer columns.
编辑:
隐私 - 这是一个可选功能,我们不想要求更多的个人信息。
Privacy -- this is an optional feature, and we don't want to ask for more personal information than needed.
推荐答案
只是存储日期与任意leapyear和格式化它需要在SELECTs。
我有一些情况下,我做到这一点。
Just store the date with an arbitrary leapyear and format it as need in SELECTs.
I have a number of cases where I do exactly that. It's so much easier than all the other ideas.
如果出生年份为可选,那么一个日期有额外的优势,你可以存储年份,如果你有的话 - 在同一个4字节的日期列需要。使用任意一个不用一年的日子是不可能的。像2400或1804。
If the year of birth is optional then a date has the additional advantage that you can store the year if you have it - in the same 4 bytes a date column needs. Use an arbitrary leapyear that is impossible otherwise for dates without a year. Like 2400 or 1804.
这篇关于如何保存一年没有一年的生日?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!