Create a db for safe testing:
create schema Hybreeder;
use Hybreeder;
Schema:
CREATE TABLE `temp` (
`Id` int(11) AUTO_INCREMENT PRIMARY KEY,
`Receipt_no` int(11) NOT NULL,
`Name` varchar(20) NOT NULL,
UNIQUE KEY `unq_Receipt_no` (`Receipt_no`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- The following table would be useful system wide for all
-- your special incrementor needs whatever they may be
-- Especially great for those looking for Prefixes to
-- add to PK's like an id such as ABC00001
create table sequences
( id int auto_increment primary key,
sectionType varchar(200) not null,
nextSequence int not null,
unique key(sectionType)
) ENGINE=InnoDB;
-- Prime it with some "sections" (we had to call them something)
insert sequences (sectionType,nextSequence) values
('Chassis',1),('Engine Block',1),('Carburetor',1),('Receipt',1001);
Stored Proc:
DROP PROCEDURE if exists getNextSequence;
DELIMITER $$ -- Note: delete this line for PHPMyAdmin (you don't need it)
CREATE PROCEDURE getNextSequence(p_sectionType varchar(200))
BEGIN
-- pass in as a parameter the "section" for next inc, such as "Chassis"
START TRANSACTION;
SELECT nextSequence into @mine_to_use from sequences where sectionType=p_sectionType FOR UPDATE;
UPDATE sequences set nextSequence=nextSequence+1 where sectionType=p_sectionType;
COMMIT; -- get and release INTENTION LOCK ASAP
SELECT @mine_to_use as yourSeqNum; -- return as a 1 column, 1 row resultset
END;
$$ -- Note: delete this line for PHPMyAdmin (you don't need it)
DELIMITER ; -- Note: delete this line for PHPMyAdmin (you don't need it)
Your client program will call the stored proc and process the result set getting the next num to use such as:
call getNextSequence("Receipt");
+------------+
| yourSeqNum |
+------------+
| 1001 |
+------------+
call it again for the heck of it:
call getNextSequence("Receipt");
+------------+
| yourSeqNum |
+------------+
| 1002 |
+------------+
It now has a 1 row 1 column result set with the column name yourSeqNum
.
Let's pseudocode call that NNNNN
as a variable.
INSERT temp(`Receipt_no`,`Name`) VALUES (NNNNN,'Fred'); -- again this is pseudocode
Id
is the AUTO_INCREMENT
column so we skip it in the column list above. It gets dealt with automatically by MySQL.
Why is it pseudocode? Because there is no talk here about what your front-end language is such as PHP, Python, Java, to know how you processed that result set to get the variable NNNNN
. And I am not writing everything!
Your task for all that follow is to merely tweek the part above that gets that sequence number into a variable and use it in the INSERT
statement.
Cleanup:
DROP SCHEMA Hybreeder;
Now there are those that would say this whole thing looks silly, for NNNNN
is always 1000 greater than Id
so what is the point? If you had multiple consumers of the sequence table for the receipt section, say other processes or other companies, then that would not be the case.
Please follow along the narrative over Here for more technical aspects that I glossed over above.