简体   繁体   English

什么是最好的方式来存储生日(生日)字段与可选的年份

[英]What would be the best way to store birthdate(birthday) field with optional year

I have mysql database, with birthdate column as date. 我有mysql数据库,以birthdate列为日期。

What would be the best way to save into this column, with optional year. 什么是保存到此列中的最佳方式(可选年份)。 Should I just put some random year on it? 我应该随便放些年份吗? what is the best practice to save birthday in database. 在数据库中保存生日的最佳实践是什么。

I create 3 text field in cakephp, day, month, and year.. but not sure how to approach this, to save in database, since if I randomly put the year (maybe 0001, since 0000 is not accepted), when I pull it back, it will show the year. 我在cakephp,日,月和年中创建了3个文本字段。.但不确定如何将其保存在数据库中,因为如果我随机输入年份(也许是0001,因为不接受0000),则将其拉出回来,它将显示年份。

Might it be best to use three columns, Day, Month, and Year, and treat Year as optional? 最好使用三列,即日,月和年,并将年作为可选项吗?

Edit: Although using two might be cleaner. 编辑:虽然使用两个可能更清洁。 one DATE column and just put a dummy year in (0000) and one YEAR column. 一个DATE列,然后在(0000)中放置一个虚拟年份,然后在YEAR列中放置一个。 Then assemble the two as you use them. 然后在使用它们时组装两者。 That way you get all the formatting, sorting and data validation that the data types provide, but you can still ignore the year when you don't have it 这样,您可以获得数据类型提供的所有格式,排序和数据验证,但是当您没有年份时,仍然可以忽略年份

Just an opinion, but if the year is optional (some will have it; some won't) I'd use a DATE type and set the year to zero. 只是一种意见,但是如果年份是可选的(某些年份将具有它;有些则不会),我将使用DATE类型并将年份设置为零。 You can check for birthdays without years like this: 您可以像这样检查没有年份的生日:

SELECT * FROM myTable WHERE YEAR(birthdate) = 0

Or if the birthdate column is indexed use this instead because it'll be optimizable: 或者,如果将birthdate列编入索引,请改用它,因为它将被优化:

SELECT * FROM myTable WHERE birthdate <= '0000-12-31'

If the year is forbidden for all values - in other words you'll never store the year - I'd recommend separate month and day columns. 如果所有值均禁止使用年份(换句话说,您将永远不会存储年份),则建议您使用单独的月份和日期列。

The best approach is to have two fields: 最好的方法是具有两个字段:

  1. birthdate which is a date birthdate是一个日期
  2. BirthdayHasYear which is a flag (such as a tinyint) BirthdayHasYear ,它是一个标志(例如tinyint)

The reason for not depending on a rule like "when the year is zero then it is unknown" is that the data structure is very hard to understand -- in the future or if someone else looks at it. 不依赖诸如“当年为零时,则未知”之类的规则的原因是,数据结构很难理解-将来或是否有人看它。 I would then be inclined to add a constraint that said "if BirthDayHasYear is true then the year on birthdate is 0 (or whatever). 然后,我倾向于添加一个约束条件,说“如果BirthDayHasYear为true,则birthdate为0(或其他任何年份)。

Alternatively, I would have one field of birthdate and then only access the table through a view where such fields as: 或者,我将有一个birthdate字段,然后通过以下字段访问该表:

  • BirthdayHasYear BirthdayHasYear
  • Age 年龄

are defined as additional fields in the view. 在视图中定义为其他字段。 In other databases, I would use this approach with computed columns. 在其他数据库中,我将这种方法用于计算列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM