If I had to do this (continue using the MyISAM AUTO_INCREMENT behavior but using InnoDB tables)
One option I would consider is continuing to use MyISAM tables to generate the AUTO_INCREMENT values, exercising that functionality from a BEFORE INSERT trigger on the InnoDB table.
First, I'd remove the AUTO_INCREMENT attribute from the invoiceID column on the new InnoDB table ...
CREATE TABLE invoice
( invoiceID INT
, businessunitID INT(2)
, date DATETIME
, PRIMARY KEY (businessunitID, invoiceID)
) ENGINE=InnoDB;
Next, create a MyISAM table that has the AUTO_INCREMENT property. We only need the two columns in the primary key, we don't need any of the other columns.
CREATE TABLE invoice_autoincrement
( invoiceID INT AUTO_INCREMENT
, businessunitID INT(2)
, PRIMARY KEY (businessunitID, invoiceID)
) ENGINE=MyISAM;
Then we can create a BEFORE INSERT trigger. As a rudimentary starting point, something like this:
DELIMITER $$
CREATE TRIGGER invoice_bi
BEFORE INSERT ON invoice
FOR EACH ROW
BEGIN
DECLARE li_invoiceid INT DEFAULT NULL;
IF NEW.invoiceID IS NULL OR NEW.invoiceID = 0 THEN
INSERT INTO invoice_autoincrement
( invoiceID
, businessunitID
) VALUES
( NEW.invoiceID
, NEW.businessunitID
);
SELECT LAST_INSERT_ID() INTO li_invoiceid ;
SET NEW.invoiceID = li_invoiceid ;
END IF;
END$$
DELIMITER ;
There's likely more that needs to be added to the trigger to appropriately handle error conditions; if sql_mode doesn't includes NO_AUTO_VALUE_ON_ZERO
, then we only need to handle the NULL value and not the zero value...
IF NEW.invoiceID IS NULL THEN
We also need to consider that the INSERT into the MyISAM table won't be rolled back if the transaction rolls back.
We don't necessarily need to keep every row we insert into the MyISAM table; we only really need the row with the highest (maximum) invoiceID for each businessunitID.
As batch maintenance approach to keeping the MyISAM table trimmed...
DELETE t.*
FROM invoice_autoincrement t
-- anti-join exclude
LEFT
JOIN ( SELECT r.businessunitID
, MAX(r.invoiceid) AS max_invoiceID
FROM invoice_autoincrement r
GROUP BY r.businessunitID
) s
ON s.businessunitID = t.businessunitID
AND s.max_invoiceID = t.invoiceID
WHERE s.businessunitID IS NULL
If we want to keep the MyISAM table trimmed from within BEFORE INSERT trigger, following a successful insert to the MyISAM table, and after we've retrieve the LAST_INCREMENT_ID() value... we can execute a DELETE including predicates on businesunitID.
We could repeat the same batch logic, for just the NEW.businessunitID
DELETE t.*
FROM invoice_autoincrement t
-- anti-join exclude
LEFT
JOIN ( SELECT r.businessunitID
, MAX(r.invoiceid) AS max_invoiceID
FROM invoice_autoincrement r
WHERE r.businessunitID = NEW.businessunitID
GROUP BY r.businessunitID
) s
ON s.businessunitID = t.businessunitID
AND s.max_invoiceID = t.invoiceID
WHERE s.businessunitID IS NULL
AND t.businessunitID = NEW.businessunitID
;
Or we can do a simpler DELETE, removing rows for that one businessunitID that have invoiceID less than the auto_increment value we just retrieved...
DELETE q.*
FROM invoice_autoincrement q
WHERE q.businessunitID = NEW.businessunitID
AND q.invoiceID < NEW.invoiceID
;
That's an alternative I would consider.
auto_increment
to produce separate sequences for each distinct value ofbusinessunitID
. As in: insert a record for business 1, and it's invoice 1; insert another record for business 1 and it's invoice 2; then insert a record for business 2 and it's invoice 1, not 3. Is that a correct description of what you want?auto_increment
) aren't supposed to be meaningful at all. If you have specific business requirements for how your invoices are numbered, you should probably assign the numbers explicitly in your application code.