I'm designing a schema for invoicing and I'm trying to decide on the best practice for carrying out the following requirements. There are multiple companies which have multiple branches. Each branch will want its own auto-increment number independent of the other branches (and companies). On top of that, there are different classes of invoices. I have set up the following table structure
tbl_invoices
|company_id|branch_id|class_id|invoice_id|...other invoice data columns...|
tbl_branches
|branch_id|branch_prefix|
tbl_classes
|class_id|class_prefix|
I am strongly favouring INNODB because of FKs and transactions. However, I have a problem with auto-increment (MyISAM would auto-increment multiple column primary keys easily). Another issue I have is having to join multiple tables all the time when I pull the invoice data because I need the full invoice_id that concats the different prefixes. I've also ran into the issue of what happens when the prefix strings change in the future. It's probably unwarranted for the past issued invoices to also have their prefixes change. I'm thinking I should add an additional prefix column for tbl_invoices which will store the full concat prefix on insert. I'm not too sure if this is the right practice? I've considered storing the entire invoice_id in full concat form in the column but I wouldn't be able to enforce uniqueness for INV-0001 from INV-00001.
This would still mean I have difficulty with the auto-increment.