I am trying to implement a ticket system, the numbers generated would be sequential.
4000, 4001, 4002, 4003
My idea was to have a table that relates to current raffle, and the ticket_no column is an auto increment column that resets when the foreign key changes.
The composite keys are raf_id
and ticket_no
since the ticket no should not exist multiple times in same raffle
Is this possible?
|------------|-----------|---------|
| raf_id | ticket_no | user_id |
|------------|-----------|---------|
| 1 | 4000 | 1 |
| 1 | 4001 | 1 |
| 1 | 4002 | 2 |
| 1 | 4003 | 3 |
| 2 | 4000 | 4 |
| 2 | 4001 | 4 |
| 2 | 4002 | 5 |
| 2 | 4003 | 1 |
|------------|-----------|---------|
Best Answer
Automatic?
Unknown. Most likely, not. (as in: no out-of-the-box database features)
Manual?
Yes.
You need to keep track of "last number used" in a table. eg
From there, your process needs to go like this:
RAF_ID
RAF
table.COMMIT
the dataDepending on your RDBMS, some steps can be combined into a single step. The HOWTO do these things (eg Best practice for "grab a lock") can also be RDBMS dependent.
Notes
The "grab a lock" step will serialize your code. This is bad for databases. This requirement/process will limit how fast you can add new rows. I suggest you look at alternative methods (eg a
VIEW
that usesRANK()
).Can this be implemented in a
TRIGGER
? Maybe.