Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datetime vs Timestamp field

Tags:

mysql

I'm new in MySQL database.

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

Thanks

like image 200
Damien Avatar asked May 16 '26 17:05

Damien


2 Answers

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.

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.

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

like image 120
Chris Avatar answered May 18 '26 09:05

Chris


For a creation field I would use a timestamp. You will not get into trouble with timezone stuff this way, which can be pretty tricky.

Timestamp is not usefull for, for instance, birthdays, as you have to deal with the EPOCH aka timestamp '0', which is 1 january 1970. But for creation-time it should not matter.

like image 20
Nanne Avatar answered May 18 '26 07:05

Nanne