1
I have a table called "users" in the database. See below the table structure:
I would like to enter a field called "matricula" varchar (50) there after the "id" field. However I would like this "matricula" field to be auto_increment and Unic. (so far so good, I can do).
The problem is that I would like this "license plate" field to always receive the value (automatically) of: current year + the field "id".
For example, if entering a new user’s id was 741, then its "enroll" field would automatically receive the value of "2020741".
OBS: Remembering that this would be done in an existing table and with data.
OBS2: I know I could do this when entering the data with the programming language (php in my case), but I would like to do this directly with the Mysql.
Thanks for your attention. Thank you !
For the existing data, you will need to do some update, for the new data, ever thought to work with some Rigger?
– Daniel Mendes
Yes, a Trigger would be a good option. But this would not be to be set in the field creation ?
– Gato de Schrödinger
There is the default value for the fields, but as it will be something more dynamic, which will require even a date treatment to get the year, I do not know if it is possible, I have never tried, but it is worth searching, if accept a function for example, you could create this function in Mysql and associate to the default field.
– Daniel Mendes
Opa, it seems that it is possible to use an expression: https://stackoverflow.com/questions/270309/can-i-use-a-function-for-a-default-value-in-mysql
– Daniel Mendes
Take a look at this post: https://answall.com/questions/52492/como-personalizar-a-numera%C3%A7%C3%A3o-do-auto-increment-no-mysql
– Rodrigo Zem
I’ll take a look, Daniel and Rodrigo. Return to you if it served me. Thank you !
– Gato de Schrödinger
Might not be able to set the default as the current year + the id field ?
– Gato de Schrödinger