Generating sequential IDs for each user in the same MySQL table

0

I have always used the primary key itself as the default ID for each record in a database table. Once the table has been used by several users, each one has its own registered products, it will no longer be possible to maintain that standard sequence (one by one) for each user, since the code maintains the sequence of the primary key. In this way, I would like to check a more optimized and practical way to generate sequential code for each user. When creating a new column for each user's sequential code, I thought about the following possibilities:

  • When saving the product of each user, check your generated code (starting with 1, obviously) and then store in a user parameter field the next code to be used by it.

  • Or, when recording the product, look for the code of the last product recorded in the table, and then increment + 1 for the new product. (I do not find a proper way because by deleting products, it could reuse deleted code).

What do you recommend?

    
asked by anonymous 28.11.2018 / 14:20

2 answers

1

Boy, I think better the first option as the friend @Nicolas Pereira said before, I already made an application with his second option but was at the request of the client, just not to ask the sequence generated.     

28.11.2018 / 14:58
1

Would you like to create a sequence for each user? I believe you could make the first choice. create a table helps with the ID_USER and the LastCodigo of it and at the time of giving the insert you search what was the LastCodigo and enter +1 for the code!

The option of deleting a product would have no problem with the auxiliary table, and it would be nice for you to know how many products it has in total (excluded or not)

    
28.11.2018 / 14:47