Can I get a unique TIMESTAMP for each record in MySQL

Is it possible to get a unique timestamp value for each record in MySQL? ..

I created an example table

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

and performed some selective insertions and seems to duplicate timestamp values.

e.g  insert into t1(id,name) values(1,"test");
+5
source share
2 answers

In the near future (5.6.4) MySQL will provide fractional seconds in TIMESTAMP columns, however even fractional seconds are not guaranteed to be unique, although theoretically they are most often unique, especially if you have limited MySQL to a single thread.

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

SELECT UUID() gives something like:

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

And after a while:

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

UUID , , , SUBSTR() CONCAT() :

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

:

11e1-8f00-45f9b8d6

, ​​ , , . UUID() , ( ), ( , auto_increment).

UUID() , PHP microtime() , . (web), , microtime() .

+6

, . , , where. : unique timestamp.

? auto increment - , ..

, timestamp, .

+2

All Articles