Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use a big INT or regular INT in MySQL to store a timestamp?

Should I be using a big integer or a regular integer in MySQL to store a timerstamp in? I plan on storing it in an INT and not the built in timestamp or datetime so which INT type should I use?

like image 994
JasonDavis Avatar asked Jan 08 '10 22:01

JasonDavis


People also ask

How is TIMESTAMP stored in MySQL?

MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME .) By default, the current time zone for each connection is the server's time.

What is difference between Int and Bigint in MySQL?

Remarks. The int data type is the primary integer data type in SQL Server. The bigint data type is intended for use when integer values might exceed the range that is supported by the int data type. bigint fits between smallmoney and int in the data type precedence chart.

Is a TIMESTAMP an int?

More precisely, the timestamp is an integer numeric value that expresses the number of seconds elapsed from an arbitrary date, ie midnight (UTC) of January 1, 1970, which is called epoch not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z).

Is it better to use TIMESTAMP or datetime?

Timestamps in MySQL are generally used to track changes to records, and are often updated every time the record is changed. If you want to store a specific value you should use a datetime field.


2 Answers

Int would roll over to a negative in 2038 (if you are using UNIX timestamp): http://en.wikipedia.org/wiki/2038_problem.

so BIGINT is probably the safest choice

like image 56
zmbush Avatar answered Sep 21 '22 15:09

zmbush


You should be storing it in a timestamp since that's most likely what the DBMS will optimize for timestamp data.

I'm curious as to why you would sacrifice all the hard work that the MySQL developers have put into making timestamps work the way they should, and replacing it with something that will almost certainly not work as well.

Since you don't state why you want to use an integer, I'm just going to assume it was temporary insanity and that you'll soon recover :-)

like image 32
paxdiablo Avatar answered Sep 18 '22 15:09

paxdiablo