8

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

2
  • Please provide the reference about SQL Server. I am very dubious.
    – Rick James
    Mar 31, 2017 at 3:48
  • 1
    I would not trust even microsecond-resolution to be unique. Might work on a single machine today, but what about tomorrow's machine(s), etc.
    – Rick James
    Mar 31, 2017 at 3:51

1 Answer 1

5

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).

4
  • are you considering the timestamp data type in mysql/mariadb right? timestamp is a poor choice is also my answer too thanks for enlightening me,
    – david
    Mar 28, 2017 at 9:05
  • Yes, it was the timestamp I had in mind. In MySQL/MariaDB timestamp is not the same type as in other DBMS:s, it deviates from the standard in several aspects. Choosing another datetime type does not help either since the maximum resolution is second there as well. Mar 28, 2017 at 10:14
  • MariaDB allows users to specify microsecond precision on creating a table . And it would be good for OP to describe the use cases to see whether the TIMESTAMP data type can fit with application requirement , e.g. is the timestamp the only column in the p.k. or it is part of a composite key ? does the application require the precision ? ... etc
    – Ham
    Jan 11 at 4:55
  • @Ham, I have little hope that the OP will clarify anything 6 years after the question was asked:-) Jan 11 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.