HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Is using timestamp as primary key good idea - mysql/mariadb?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
ideaprimarytimestampmariadbmysqlusinggoodkey

Problem

We just argued that storing timestamp in sqlserver in microsoft does guarantee uniqueness, but im a little bit doubful about mysql/mariadb, because we are using parallel execution when inserting to the mysql/mariadb database.

So, Is using timestamp as primary key good idea - mysql/mariadb?

PS: There is always an PK and status but its important if we are using timestamp as the where clause in getting the actual remaining records to be archived/removed (so that it is efficient in querying the records).So its important its unique

Solution

IMO it's bad because of several reasons. First, blindly assigning a generated p.k. for every relation is bad practice. At least try to identify what candidate keys there are in each relation. A surregate p.k. may be the smartest choice in some situations, but it is definitely not so in all possible situations. Good properties for candidate keys are:

  • Unique



  • Irreducible



  • Non-complex



  • Stable



  • Familiar



Often there is a conflict between these criteria and you have to sacrifice one for another.

Second, if the best choice is a surrogate identifier I'll say that timestamp is a poor choice. Especially in MySQL/MariaDB before (MySQL) 5.6.4 where the resolution is seconds. Using a timestamp as a p.k in these DBMS means that you cannot insert more than one row / second (as long as the timestamp should reflect the time of insert).

Context

StackExchange Database Administrators Q#168356, answer score: 5

Revisions (0)

No revisions yet.