日期时间与日期和时间Mysql [英] Datetime vs Date and Time Mysql
问题描述
我通常使用datetime字段存储应用程序中数据的created_time更新时间.
I generally use datetime field to store created_time updated time of data within an application.
但是现在我遇到了一个数据库表,在那里他们将日期和时间分别放在表中.
But now i have come across a database table where they have kept date and time separate fields in table.
- 那么应该使用其中两个的模式是什么?为什么?
- 使用两个优点和缺点是什么?
推荐答案
我倾向于认为将日期和时间存储在单独的字段中基本上没有任何好处. MySQL提供了非常方便的功能来提取datetime
值的日期和时间部分.
I tend to think there are basically no advantages to storing the date and time in separate fields. MySQL offers very convenient functions for extracting the date and time parts of a datetime
value.
好的.可能有一些效率方面的原因.在MySQL中,您可以在字段上放置单独的索引.因此,例如,如果您要搜索特定的时间,则按一天中的小时计数(例如)的查询可以使用time
字段上的索引.在这种情况下,将不使用datetime
字段上的索引.单独的date
字段可能使编写使用date
索引的查询更容易,但是严格来讲,datetime
也应该起作用.
Okay. There can be some efficiency reasons. In MySQL, you can put separate indexes on the fields. So, if you want to search for particular times, for instance, then a query that counts by hours of the day (for instance) can use an index on the time
field. An index on a datetime
field would not be used in this case. A separate date
field might make it easier to write a query that will use the date
index, but, strictly speaking, a datetime
should also work.
我看到日期和时间分别存储的一次是在交易系统中.在这种情况下,交易具有评估日期.评估时间类似于纽约开盘价"或伦敦收盘价"之类的东西-这不是实时值.它是对一天中用于评估的时间的描述.
The one time where I've seen dates and times stored separately is in a trading system. In this case, the trade has a valuation date. The valuation time is something like "NY Open" or "London Close" -- this is not a real time value. It is a description of the time of day used for valuation.
这篇关于日期时间与日期和时间Mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!