No idea why you need two columns auto incrementing values, there is no point... but if you insist -
You can accomplish it in a UDF or SP this way you have multiple columns auto incrementing a value.
EXAMPLE #1: STORED PROCEDURE (SP)
Table
CREATE TABLE tests ( test_id INT(10) NOT NULL PRIMARY KEY AUTO_INCREMENT, test_num INT(10) NULL, test_name VARCHAR(10) NOT NULL );
Stored Procedure
DELIMITER $$ CREATE PROCEDURE autoInc (name VARCHAR(10)) BEGIN DECLARE getCount INT(10); SET getCount = ( SELECT COUNT(test_num) FROM tests) + 1; INSERT INTO tests (test_num, test_name) VALUES (getCount, name); END$$ DELIMITER ;
Call the SP
CALL autoInc('one'); CALL autoInc('two'); CALL autoInc('three');
Look up the table
SELECT * FROM tests; +---------+----------+-----------+ | test_id | test_num | test_name | +---------+----------+-----------+ | 1 | 1 | one | | 2 | 2 | two | | 3 | 3 | three | +---------+----------+-----------+
EXAMPLE #2: USER-DEFINED FUNCTION (UDF) Table CREATE TABLE tests ( test_id INT(10) NOT NULL PRIMARY KEY AUTO_INCREMENT, test_num INT(10) NULL, test_name VARCHAR(10) NOT NULL );
User-defined Function
DELIMITER $$ CREATE FUNCTION autoInc () RETURNS INT(10) BEGIN DECLARE getCount INT(10); SET getCount = ( SELECT COUNT(test_num) FROM tests) + 1; RETURN getCount; END$$ DELIMITER ;
Insert using the UDF
INSERT INTO tests (test_num, test_name) VALUES (autoInc(), 'one'); INSERT INTO tests (test_num, test_name) VALUES (autoInc(), 'two'); INSERT INTO tests (test_num, test_name) VALUES (autoInc(), 'three');
Look up the table
SELECT * FROM tests; +---------+----------+-----------+ | test_id | test_num | test_name | +---------+----------+-----------+ | 1 | 1 | one | | 2 | 2 | two | | 3 | 3 | three | +---------+----------+-----------+
These have been tested and verified. I'd personally use the function, it's more flexible.