I would like to create a unique billing identifier/number. I want it to be a combination of a constant string, year and an additional number. For example "ABC-2013-100001". How can I do that so I will be definitely sure that the billing identifier is unique? I was thinking about maintaing a billing_id like a primary key that would be unique and autoincrement. Then I would just select max(billing_id) to get the last one add 1 to it and the prefix "ABC-2013" and then insert it into the table. The problem is that this is a 3 step way. Select, do something, insert. I am afraid that it might happen something like, when 2 users will do this at the same time, every one selects the same number, so technically they insert the same identifier. Am I wrong and overreacting or is this kind of generating unique ids common? Should I take a different approach?
EDIT
Ok so I did something like this:
$data = array(
'billing_identifier' => 'law-'.$year."-".$this->db->insert_id()
);
// inserting into billing_identifiers
$this->db->insert('billing_identifiers', $data);
$bill_id = $this->db->insert_id();
but the $this->db->insert_id() is 0 at the beginning which makes sense and after calling the insert function it gets the last id. Isnt there a way I can do that in one shot?