简体   繁体   中英

Storing 'invalid' date in a database

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 eg '2101-01-01 00:00:00' in the 'last visited' field, to denote an invalid date.

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

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.

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.

I would just leave the field null.

If they've never visited the record, it shouldn't have a timestamp at all.

你有什么理由不为此使用NULL吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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