Generating Custom Auto Generated ID


Can anyone give show a simple tutorial (step-by-step) to generate custom customer_id in a database table? For example, I have a table named Customer and would like to generate customer_id like the following example:

Table structure:
customer_id (auto-increment), fullname, address, contact, email

Example of customer_id as: 2011-AB001, 2011-CA001, 2012-DA001, 2005-FR001
20 as current year, 11 as current month, AB / CA / DA / FR as first 2 characters of customer name and 001 as increment number.


A field auto-increment only accept numbers. so you need create other field for that.
for ex: customer_code varchar(16)
and you need have a table for yours autoincrements for each 2 characters.

1 Like