MySQL – How to Create Unique Datetime Without Seconds

datetimeinnodbMySQL

I need to build a table accepting one row for each minute. If I set the type of primary key to datetime, 60 records can be added for each minute.

Is there any trick to design a table to keep 0000-00-00 00:00 unique?

Best Answer

Store CONCAT(LEFT(NOW(), 16), ':00') into the PRIMARY KEY that is a DATETIME.

mysql> SELECT NOW(), CONCAT(LEFT(NOW(), 16), ':00');
+---------------------+--------------------------------+
| NOW()               | CONCAT(LEFT(NOW(), 16), ':00') |
+---------------------+--------------------------------+
| 2015-12-11 21:37:38 | 2015-12-11 21:37:00            |
+---------------------+--------------------------------+