Answer the question
In order to leave comments, you need to log in
What is the best way to generate a unique number in the database, in addition to the auto-increment key?
There is a database like:
группа
id name code ...
1 1b2 111
студент
id group_id name contract_number
1 1 Иванов ?
Answer the question
In order to leave comments, you need to log in
You are aware that a unique index can be hung on a set of fields, and not on one field? Keep all these fields in different columns of the table, and combine them into one unique index for integrity control.
do not mix technical information with applied
unique key - technical, unique contract number - applied,
in short, natural keys - evil, abstract - good,
if the customer certainly wants to stuff something into the contract number
(and guaranteed to want more in a year)
then
1) store this something as separate search fields
2) do not use the contract number for links in any case
3) do not store the number but collect it with a request only for output (if you prohibit editing the original fields)
4) uniqueness to the contract number will be added by a timestamp
Make the contract a separate entity and store it in a separate table. One day you will inevitably have students with multiple contracts.
To select a unique number, fix the full prefix and look for the maximum number with this prefix
contract_number like '111090621%'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question