I have a Table Employee(id,name,dept_name).I want the id will alphanumeric [dddddaaaaa] with first 5 digit will be auto increment id and rest 4 char will be the first 4 char of employee name.
For example , for the first employee name=John Todd ,the auto incremented part of the Id will be 00001. And so the Id will be 00001JOHN.
Is it possible to set a default expression to the column Id=(concat(autoincrement,substring(name,4)).
I was also thinking if I Can create a trigger on after insert Employee and the trigger will update the Employee.Id. But MySql does not allow to update the same table from trigger for which trigger got fired.
Please Help.
What about a schema like
That’ll let you perform lookups fairly quickly using your natural primary key, while giving you an artificial primary key and not forcing to denormalize.
Of course since the 0001 part of the primary key is unique enough to identify the user you need not query the name at all.
If you insist on precalculating this should work
Testing it:
This is kind of a hack, and information_schema won’t be available on some DBs where permissions aren’t under your control.