This is an interesting question because different databases have unique approaches for providing auto_increment.
MySQL : Only one auto_increment key is generated to uniquely identify a row in a table. There is not a lot of explanation behind why, but just implementation. Depending on datatype, auto_increment values are fixed by the length of datatype in bytes:
- Max TINYINT is 127
- Max UNSIGNED TINTINT is 255
- Max INT is 2147483647
- Max UNSIGNED INT is 4294967295
PostgreSQL
The internal datatype serial is used for auto increment from 1 to 2,147,483,647. Larger ranges are allowed using bigserial.
Oracle : The schema object called SEQUENCE can create new numbers by simply summoning the nextval function. PostgreSQL also has such a mechanism.
Here is a nice URL that provides how other DBs specify them : http://www.w3schools.com/sql/sql_autoincrement.asp
Now concerning your question, if you really want to have multiple auto_increment columns in a single table, you will have to emulate that.
Two reasons why you must emulate this:
- MySQL accommodates only one increment column per table as does PostgreSQL, Oracle, SQL Server, and MS Access.
- MySQL does not have a SEQUENCE schema object like Oracle and PostgreSQL.
How would you emulate it ???
Using multiple tables that have only one auto_increment column and mapping them to the desired columns in target tables. Here is an example:
Copy and Paste This Example:
use test
DROP TABLE IF EXISTS teacher_popquizzes;
CREATE TABLE teacher_popquizzes
(
teacher varchar(20) not null,
class varchar(20) not null,
pop_mon INT NOT NULL DEFAULT 0,
pop_tue INT NOT NULL DEFAULT 0,
pop_wed INT NOT NULL DEFAULT 0,
pop_thu INT NOT NULL DEFAULT 0,
pop_fri INT NOT NULL DEFAULT 0,
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
);
INSERT INTO teacher_popquizzes (teacher,class) VALUES
('mr jackson','literature'),
('mrs andrews','history'),
('miss carroll','spelling');
DROP TABLE IF EXISTS mon_seq;
DROP TABLE IF EXISTS tue_seq;
DROP TABLE IF EXISTS wed_seq;
DROP TABLE IF EXISTS thu_seq;
DROP TABLE IF EXISTS fri_seq;
CREATE TABLE mon_seq
(
val INT NOT NULL DEFAULT 0,
nextval INT NOT NULL DEFAULT 1,
PRIMARY KEY (val)
);
CREATE TABLE tue_seq LIKE mon_seq;
CREATE TABLE wed_seq LIKE mon_seq;
CREATE TABLE thu_seq LIKE mon_seq;
CREATE TABLE fri_seq LIKE mon_seq;
BEGIN;
INSERT INTO tue_seq (val) VALUES (0) ON DUPLICATE KEY UPDATE nextval = nextval + 1;
SELECT nextval INTO @pop FROM mon_seq;
UPDATE teacher_popquizzes SET pop_tue = pop_tue + 1 WHERE id = 2;
COMMIT;
BEGIN;
INSERT INTO tue_seq (val) VALUES (0) ON DUPLICATE KEY UPDATE nextval = nextval + 1;
SELECT nextval INTO @pop FROM tue_seq;
UPDATE teacher_popquizzes SET pop_tue = pop_tue + 1 WHERE id = 1;
COMMIT;
BEGIN;
INSERT INTO wed_seq (val) VALUES (0) ON DUPLICATE KEY UPDATE nextval = nextval + 1;
SELECT nextval INTO @pop FROM wed_seq;
UPDATE teacher_popquizzes SET pop_wed = pop_wed + 1 WHERE id = 2;
COMMIT;
SELECT * FROM teacher_popquizzes;
This will create a table of pop quizzes for teachers. I also created five sequence emulators, one for each day of the school week. Each sequence emulator works by inserting the value 0 in the val column. If the sequence emulator is empty, it starts with val 0, nextval 1. If not, the nextval column is incremented. You can then fetch the nextval column from the sequence emulator.
Here are the sample results from the example:
mysql> CREATE TABLE teacher_popquizzes
-> (
-> teacher varchar(20) not null,
-> class varchar(20) not null,
-> pop_mon INT NOT NULL DEFAULT 0,
-> pop_tue INT NOT NULL DEFAULT 0,
-> pop_wed INT NOT NULL DEFAULT 0,
-> pop_thu INT NOT NULL DEFAULT 0,
-> pop_fri INT NOT NULL DEFAULT 0,
-> id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
-> );
Query OK, 0 rows affected (0.11 sec)
mysql> INSERT INTO teacher_popquizzes (teacher,class) VALUES
-> ('mr jackson','literature'),
-> ('mrs andrews','history'),
-> ('miss carroll','spelling');
Query OK, 3 rows affected (0.03 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> DROP TABLE IF EXISTS mon_seq;
Query OK, 0 rows affected (0.06 sec)
mysql> DROP TABLE IF EXISTS tue_seq;
Query OK, 0 rows affected (0.03 sec)
mysql> DROP TABLE IF EXISTS wed_seq;
Query OK, 0 rows affected (0.03 sec)
mysql> DROP TABLE IF EXISTS thu_seq;
Query OK, 0 rows affected (0.05 sec)
mysql> DROP TABLE IF EXISTS fri_seq;
Query OK, 0 rows affected (0.07 sec)
mysql> CREATE TABLE mon_seq
-> (
-> val INT NOT NULL DEFAULT 0,
-> nextval INT NOT NULL DEFAULT 1,
-> PRIMARY KEY (val)
-> );
Query OK, 0 rows affected (0.12 sec)
mysql> CREATE TABLE tue_seq LIKE mon_seq;
Query OK, 0 rows affected (0.09 sec)
mysql> CREATE TABLE wed_seq LIKE mon_seq;
Query OK, 0 rows affected (0.08 sec)
mysql> CREATE TABLE thu_seq LIKE mon_seq;
Query OK, 0 rows affected (0.07 sec)
mysql> CREATE TABLE fri_seq LIKE mon_seq;
Query OK, 0 rows affected (0.14 sec)
mysql> BEGIN;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO tue_seq (val) VALUES (0) ON DUPLICATE KEY UPDATE nextval = nextval + 1;
Query OK, 1 row affected (0.00 sec)
mysql> SELECT nextval INTO @pop FROM mon_seq;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> UPDATE teacher_popquizzes SET pop_tue = pop_tue + 1 WHERE id = 2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> COMMIT;
Query OK, 0 rows affected (0.03 sec)
mysql> BEGIN;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO tue_seq (val) VALUES (0) ON DUPLICATE KEY UPDATE nextval = nextval + 1;
Query OK, 2 rows affected (0.00 sec)
mysql> SELECT nextval INTO @pop FROM tue_seq;
Query OK, 1 row affected (0.00 sec)
mysql> UPDATE teacher_popquizzes SET pop_tue = pop_tue + 1 WHERE id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> COMMIT;
Query OK, 0 rows affected (0.03 sec)
mysql> BEGIN;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO wed_seq (val) VALUES (0) ON DUPLICATE KEY UPDATE nextval = nextval + 1;
Query OK, 1 row affected (0.00 sec)
mysql> SELECT nextval INTO @pop FROM wed_seq;
Query OK, 1 row affected (0.00 sec)
mysql> UPDATE teacher_popquizzes SET pop_wed = pop_wed + 1 WHERE id = 2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> COMMIT;
Query OK, 0 rows affected (0.03 sec)
mysql> SELECT * FROM teacher_popquizzes;
+--------------+------------+---------+---------+---------+---------+---------+----+
| teacher | class | pop_mon | pop_tue | pop_wed | pop_thu | pop_fri | id |
+--------------+------------+---------+---------+---------+---------+---------+----+
| mr jackson | literature | 0 | 1 | 0 | 0 | 0 | 1 |
| mrs andrews | history | 0 | 1 | 1 | 0 | 0 | 2 |
| miss carroll | spelling | 0 | 0 | 0 | 0 | 0 | 3 |
+--------------+------------+---------+---------+---------+---------+---------+----+
3 rows in set (0.00 sec)
mysql>
If you really need multiple auto increment values in MySQL, this is the closest way to emulate it.
Give it a Try !!!
UPDATE 2011-06-23 21:05
I just noticed in my example I dont use the @pop value.
This time I replaced 'pop_tue = pop_tue + 1' with 'pop_tue = @pop' and retried the example:
mysql> use test
Database changed
mysql> DROP TABLE IF EXISTS teacher_popquizzes;
Query OK, 0 rows affected (0.05 sec)
mysql> CREATE TABLE teacher_popquizzes
-> (
-> teacher varchar(20) not null,
-> class varchar(20) not null,
-> pop_mon INT NOT NULL DEFAULT 0,
-> pop_tue INT NOT NULL DEFAULT 0,
-> pop_wed INT NOT NULL DEFAULT 0,
-> pop_thu INT NOT NULL DEFAULT 0,
-> pop_fri INT NOT NULL DEFAULT 0,
-> id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
-> );
Query OK, 0 rows affected (0.06 sec)
mysql> INSERT INTO teacher_popquizzes (teacher,class) VALUES
-> ('mr jackson','literature'),
-> ('mrs andrews','history'),
-> ('miss carroll','spelling');
Query OK, 3 rows affected (0.03 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> DROP TABLE IF EXISTS mon_seq;
Query OK, 0 rows affected (0.03 sec)
mysql> DROP TABLE IF EXISTS tue_seq;
Query OK, 0 rows affected (0.03 sec)
mysql> DROP TABLE IF EXISTS wed_seq;
Query OK, 0 rows affected (0.03 sec)
mysql> DROP TABLE IF EXISTS thu_seq;
Query OK, 0 rows affected (0.01 sec)
mysql> DROP TABLE IF EXISTS fri_seq;
Query OK, 0 rows affected (0.03 sec)
mysql> CREATE TABLE mon_seq
-> (
-> val INT NOT NULL DEFAULT 0,
-> nextval INT NOT NULL DEFAULT 1,
-> PRIMARY KEY (val)
-> );
Query OK, 0 rows affected (0.08 sec)
mysql> CREATE TABLE tue_seq LIKE mon_seq;
Query OK, 0 rows affected (0.09 sec)
mysql> CREATE TABLE wed_seq LIKE mon_seq;
Query OK, 0 rows affected (0.13 sec)
mysql> CREATE TABLE thu_seq LIKE mon_seq;
Query OK, 0 rows affected (0.11 sec)
mysql> CREATE TABLE fri_seq LIKE mon_seq;
Query OK, 0 rows affected (0.08 sec)
mysql> BEGIN;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO tue_seq (val) VALUES (0) ON DUPLICATE KEY UPDATE nextval = nextval + 1;
Query OK, 1 row affected (0.01 sec)
mysql> SELECT nextval INTO @pop FROM tue_seq;
Query OK, 1 row affected (0.00 sec)
mysql> UPDATE teacher_popquizzes SET pop_tue = @pop WHERE id = 2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> COMMIT;
Query OK, 0 rows affected (0.03 sec)
mysql> BEGIN;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO tue_seq (val) VALUES (0) ON DUPLICATE KEY UPDATE nextval = nextval + 1;
Query OK, 2 rows affected (0.00 sec)
mysql> SELECT nextval INTO @pop FROM tue_seq;
Query OK, 1 row affected (0.00 sec)
mysql> UPDATE teacher_popquizzes SET pop_tue = @pop WHERE id = 1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> COMMIT;
Query OK, 0 rows affected (0.03 sec)
mysql> BEGIN;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO wed_seq (val) VALUES (0) ON DUPLICATE KEY UPDATE nextval = nextval + 1;
Query OK, 1 row affected (0.01 sec)
mysql> SELECT nextval INTO @pop FROM wed_seq;
Query OK, 1 row affected (0.00 sec)
mysql> UPDATE teacher_popquizzes SET pop_wed = @pop WHERE id = 2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> COMMIT;
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT * FROM teacher_popquizzes;
+--------------+------------+---------+---------+---------+---------+---------+----+
| teacher | class | pop_mon | pop_tue | pop_wed | pop_thu | pop_fri | id |
+--------------+------------+---------+---------+---------+---------+---------+----+
| mr jackson | literature | 0 | 2 | 0 | 0 | 0 | 1 |
| mrs andrews | history | 0 | 1 | 1 | 0 | 0 | 2 |
| miss carroll | spelling | 0 | 0 | 0 | 0 | 0 | 3 |
+--------------+------------+---------+---------+---------+---------+---------+----+
3 rows in set (0.00 sec)
mysql>