Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get a unique TIMESTAMP for every record in MySQL

Is there a possibility of getting a unique timestamp value for for each record in MySQL??..

I created a sample table

CREATE TABLE t1 (id int primary key, name varchar(50), 
  ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP );

and ran some sample INSERTIONS and seems to be timestamp values are duplicated.

e.g  insert into t1(id,name) values(1,"test");
like image 584
NULL_USER Avatar asked Apr 25 '12 17:04

NULL_USER


2 Answers

Some day soon (5.6.4), MySQL will provide fractional seconds in TIMESTAMP columns, however, even fractional seconds aren't guaranteed to be unique. Though theoretically, they'd most often be unique, especially if you limited MySQL to a single thread.

You can use a UUID if you need a unique number that is ordered temporally.


SELECT UUID(); yields something like:

45f9b8d6-8f00-11e1-8920-842b2b55ce56

And some time later:

004b721a-8f01-11e1-8920-842b2b55ce56

The first three portions of a UUID consist of the time, however, they're in order from highest precision to least, so you'd need to reverse the first three portions using SUBSTR() and CONCAT() like this:

SELECT CONCAT(SUBSTR(UUID(), 16, 3), '-', SUBSTR(UUID(), 10, 4),
  '-', SUBSTR(UUID(), 1, 8))

Yields:

1e1-8f00-45f9b8d6

You obviously couldn't use a function like this as a default value, so you'd have to set it in code, but it's a guaranteed unique temporally ordered value. UUID() works at a much lower level than seconds (clock cycles), so it's guaranteed unique with each call and has low overhead (no locking like auto_increment).

Using the UUID() on the database server may be preferred to using a similar function, such as PHP's microtime() function on the application server because your database server is more centralized. You may have more than one application (web) server, which may generate colliding values, and microtime() still doesn't guarantee unique values.


Useful reading for understanding the components of UUID

  • Universally unique identifier (UUID)
  • Extracting timestamp and MAC address from UUIDs
like image 63
4 revs, 2 users 81% Avatar answered Sep 30 '22 02:09

4 revs, 2 users 81%


Yes if you don't do two or more inserts or edits during one second. Only problem is that a lot stuff can be done during a second, i.e. multiple inserts or automatic updates using a where clause. That rules out the simple solution to force unique timestamps: to add unique constraint into timestamp column.

Why should a timestamp be unique? Use auto increment or something else if you need unique index etc.

If you need more precise time values than timestamp, see:

  • http://dev.mysql.com/doc/refman/5.5/en/fractional-seconds.html (Note: fractional part is discarded during insert. Not helping...)
  • http://codeigniter.com/forums/viewthread/66849/ (Apparently double(13,3) makes it possible to add microtime into DB.)
  • MySQL greater than problem with microtime timestamp (int multiplied with 100 or 1000 could also work. Here decimal is preferred over double.)
  • Why doesn't MySQL support millisecond / microsecond precision?
like image 45
ZZ-bb Avatar answered Sep 30 '22 04:09

ZZ-bb