Ini adalah pertanyaan yang menarik karena database yang berbeda memiliki pendekatan unik untuk menyediakan auto_increment.
MySQL : Hanya satu kunci auto_increment yang dihasilkan untuk mengidentifikasi baris dalam tabel secara unik. Tidak banyak penjelasan di balik alasannya, tetapi hanya implementasi. Bergantung pada tipe data, nilai auto_increment ditetapkan oleh panjang tipe data dalam byte:
- Max TINYINT adalah 127
- TINTINT UNSIGNED Max adalah 255
- Max INT adalah 2147483647
- Max UNSIGNED INT adalah 4294967295
PostgreSQL
Seri datatype internal digunakan untuk kenaikan otomatis dari 1 menjadi 2.147.483.647. Rentang yang lebih besar diizinkan menggunakan bigserial.
Oracle : Objek skema yang disebut SEQUENCE dapat membuat angka baru dengan hanya memanggil fungsi nextval. PostgreSQL juga memiliki mekanisme seperti itu.
Berikut adalah URL yang bagus yang menyediakan bagaimana DB lain menentukannya: http://www.w3schools.com/sql/sql_autoincrement.asp
Sekarang mengenai pertanyaan Anda, jika Anda benar-benar ingin memiliki beberapa kolom auto_increment dalam satu tabel, Anda harus meniru itu.
Dua alasan mengapa Anda harus meniru ini:
- MySQL hanya mengakomodasi satu kolom kenaikan per tabel seperti halnya PostgreSQL, Oracle, SQL Server, dan MS Access.
- MySQL tidak memiliki objek skema SEQUENCE seperti Oracle dan PostgreSQL.
Bagaimana Anda meniru itu ???
Menggunakan beberapa tabel yang hanya memiliki satu kolom auto_increment dan memetakannya ke kolom yang diinginkan di tabel target. Berikut ini sebuah contoh:
Salin dan Tempel Contoh Ini:
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;
Ini akan membuat tabel kuis pop untuk guru. Saya juga membuat lima emulator urutan, satu untuk setiap hari dalam minggu sekolah. Setiap emulator urutan bekerja dengan memasukkan nilai 0 di kolom val. Jika urutan emulator kosong, itu dimulai dengan val 0, nextval 1. Jika tidak, kolom nextval bertambah. Anda kemudian dapat mengambil kolom nextval dari emulator urutan.
Berikut adalah contoh hasil dari contoh:
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>
Jika Anda benar-benar membutuhkan beberapa nilai kenaikan otomatis di MySQL, ini adalah cara terdekat untuk menirunya.
Cobalah !!!
UPDATE 2011-06-23 21:05
Saya baru saja perhatikan pada contoh saya, saya tidak menggunakan nilai @pop.
Kali ini saya mengganti 'pop_tue = pop_tue +1' dengan 'pop_tue = @pop' dan mencoba kembali contohnya:
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>