日期时间与时间戳字段 [英] Datetime vs Timestamp field

查看:75
本文介绍了日期时间与时间戳字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 MySQL 数据库的新手.

I'm new in MySQL database.

您是否建议在表创建中使用日期时间或时间戳字段以及为什么.我使用的是 MySQL 5.7 和 innodb 引擎.

Would you recommend using a datetime or a timestamp field in table creation and why. I'm using MySQL 5.7 and innodb engine.

谢谢

推荐答案

我会使用 TIMESTAMP 来处理任何需要自动管理的东西,因为它支持诸如 ON UPDATE CURRENT_TIMESTAMP 或将 CURRENT_TIMESTAMP 作为默认值.

I would use TIMESTAMP for anything that needs to be managed automatically since it supports stuff like ON UPDATE CURRENT_TIMESTAMP or having CURRENT_TIMESTAMP as the default value.

这在 DATETIME 中是不可能的,但我更喜欢这种格式,因为你不需要转换它.所以我会把它用于其他所有事情.

This isn't possible with DATETIME but I do like the format better since you don't need to convert it. So I'd use that for everything else.

如果您需要一种可以自动生成的灵活格式,那么您可能必须使用 TIMESTAMP 并在需要时进行转换.

If you need a flexible format that can be autogenerated, then you'd probably have to go with TIMESTAMP and convert when needed.

这篇关于日期时间与时间戳字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆