4
It is possible to know which was the last row inserted in a Mysql table where the primary key is not sequential?
I have a table where the primary key consists of two columns that are FK, so they don’t follow a sequence.
When executed the query select last_insert_id();
is returned 0 (makes sense, why no ID was entered even).
The best output is to put an ID column even?
I could be wrong but I think the ID seems more appropriate.
– Maniero
If this ID will not impact the use of your PK, it is the best idea. Another possibility would be to include a field of type Datetime with DEFAULT CURRENT_TIMESTAMP (to get the last one is only to do the descending ordering) but this is not the best solution, mainly from the point of view of query performance.
– Bruno Bermann
Yes, I believe that a self-supporting column is the best solution msm thinking about ease to solve the problem and performance. Thank you!
– Raphael Castro Martin
If it is to avoid the ID, even worse by automatic date. Only comes out more "expensive", and depends on the machine clock, and does not support competition. ID does not have these problems, and just see the biggest.
– Bacco