I have a table where the primary key is format 'BCB001'
, I would like to know if there is any way to format AUTO_INCREMENT
so that I can fill the field I need.
I have a table where the primary key is format 'BCB001'
, I would like to know if there is any way to format AUTO_INCREMENT
so that I can fill the field I need.
The auto-incremental field was created precisely not to be touched by the user, making it possible to generate changes and delete data from the DB without generating collateral damage. In addition to facilitating their referential integrity.
If all of this still didn't convince you the
AUTO-INCREMENT
is for int's, not varchar's attributes, so you wouldn't be able to do what you're wanting.I hope I've helped. Cheers
A solution would be to create a field
INT AUTO_INCREMENT
in your table apart from the code to save the desired format.Then create a Trigger to update the field
cod
before (before) , using the Concat function to concatenate the fixed format(BCB)
with what LPad returns (number, amount, charfill) filling with0
on the left to what the LAST_INSERT_ID +1 function returns (last inserted id) , not exceeding theLength
in this example is 3 (second parameter)To insert I would just do the
INSERT
classic.