K
K
k491022019-01-20 17:24:02
MySQL
k49102, 2019-01-20 17:24:02

How to implement such autoincrement?

Good afternoon!
There is an application on laravel, MySQL database.
It is necessary to implement a table of products, where the id (or some additional id) of the product must be auto-incrementing for each user individually. For example, user 1's product id is 1, 2, 3, user 2's product id is 1, 2, 3, etc.
Autoincrement must be "fair", i.e. by deleting product 3 from user 1, the next product will have id - 4.
How can this be implemented?
The methods I found - a separate table with the counter value for each user, increasing through a trigger (similar question - https://stackoverflow.com/a/18949951 ).
Perhaps there is some more elegant way to solve this issue.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Antonio Solo, 2019-01-21
@solotony

Add a simple product counter to each user's profile, and increment it when adding a product for this user.

M
morricone85, 2019-01-20
@morricone85

Auto-increment must be "fair"

Is it you who invented such a new term?)
it's not clear what you want: after deleting number 3, you want a new product with number 4, but if there is already a product with this number, what then?

I
idShura, 2019-01-21
@idShura

You can make a table in which sequences will be stored ("honest" autoincrement :)) and write a function. The user id will be passed to the function input, and the output will be an additional id for this user.
Upd example
. It is not necessary to create a sequence table, as Antonio Solo wrote, you can store this information in the user profile.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question