I am storing some records in a database. A record has a 'last visited' field which is a timestamp. If a record has not been visited yet, the timestamp is invalid. I am currently storing a future date e.g. '2101-01-01 00:00:00' in the 'last visited' field, to denote an invalid date.
我将一些记录存储在数据库中。记录具有“上次访问”字段,该字段是时间戳。如果尚未访问记录,则时间戳无效。我目前正在存储未来日期,例如“上次访问”字段中的“2101-01-01 00:00:00”表示无效日期。
Is there a better way to indicate an 'invalid' date. What is the recommended 'best practise' for doing this?
是否有更好的方法来表示“无效”日期。这样做的推荐“最佳实践”是什么?
I am using MySQL, but ideally, the recommendation should be db agnostic
我正在使用MySQL,但理想情况下,建议应该是db不可知的
4 个解决方案
#1
6
Store a NULL value instead. MySQL timestamps are pretty screwed up, so you might need to change your table schema to encourage it to let you put a NULL in there; I use DATETIME instead, it's a little less weird than TIMESTAMPs in MySQL.
而是存储NULL值。 MySQL时间戳非常糟糕,因此您可能需要更改表模式以鼓励它在那里放置NULL;我使用DATETIME,它比MySQL中的TIMESTAMP更奇怪。
#2
3
The appropriate method of storing something of no value is to simply provide no value. That is to say, a null. Storing anything else and treating it as a magical value is often problematic. There are times when the magical value can be a valid value, and now you have no method of distinguishing the two.
存储没有价值的东西的适当方法是简单地不提供任何价值。也就是说,null。存储任何其他东西并将其视为一种神奇的价值通常是有问题的。有时候魔法值可以是有效值,现在你没有区分这两者的方法。
#3
1
I would just leave the field null.
我只是将该字段留空。
If they've never visited the record, it shouldn't have a timestamp at all.
如果他们从未访问过该记录,则根本不应该有时间戳。
#4
0
Is there any reason you wouldn't use NULL
for this?
你有什么理由不为此使用NULL吗?
#1
6
Store a NULL value instead. MySQL timestamps are pretty screwed up, so you might need to change your table schema to encourage it to let you put a NULL in there; I use DATETIME instead, it's a little less weird than TIMESTAMPs in MySQL.
而是存储NULL值。 MySQL时间戳非常糟糕,因此您可能需要更改表模式以鼓励它在那里放置NULL;我使用DATETIME,它比MySQL中的TIMESTAMP更奇怪。
#2
3
The appropriate method of storing something of no value is to simply provide no value. That is to say, a null. Storing anything else and treating it as a magical value is often problematic. There are times when the magical value can be a valid value, and now you have no method of distinguishing the two.
存储没有价值的东西的适当方法是简单地不提供任何价值。也就是说,null。存储任何其他东西并将其视为一种神奇的价值通常是有问题的。有时候魔法值可以是有效值,现在你没有区分这两者的方法。
#3
1
I would just leave the field null.
我只是将该字段留空。
If they've never visited the record, it shouldn't have a timestamp at all.
如果他们从未访问过该记录,则根本不应该有时间戳。
#4
0
Is there any reason you wouldn't use NULL
for this?
你有什么理由不为此使用NULL吗?