Mysql
如何在 MySQL phpmyadmin 中使用 2 個自動增量列
是否可以使用 2 個自動增量值?
- 一個從0開始
- 另一個從 4000400 開始
請幫我
不,你不能。不是開箱即用。可能的解決方法:
- 觸發器(
AFTER INSERT
觸發器)。缺點:
單純的恐怖。帶有觸發器的所有其他缺點,例如維護和調試噩夢。優點:
您可以有
FOREIGN KEY
引用此列的約束。您可以單獨更新第一個和第二個 id 列。
- 意見。如果你只有一個總是
+4000400
第一個自動遞增值的值,你可以使用視圖,所以基本上根本不儲存這個值,在需要的時候計算它:CREATE VIEW tablex_with_2nd_AI AS SELECT tablex_id, colA, -- other columns -- -- you need tablex_ix + 4000400 AS second_id FROM tablex ;
缺點:
您不能有
FOREIGN KEY
引用此(虛擬)列的約束。優點:節省一些空間。插入速度稍快。
- **不要那樣做,**根本沒有第二個自動遞增的列。你真的需要第二個自動遞增值嗎?做什麼的?對您嘗試解決的實際問題的描述將有助於其他人更好地幫助您。我認為您在這裡只告訴了您如何嘗試解決問題,而不是實際問題是什麼。
我有好消息和壞消息
好消息
您可以使用多個 auto_increment 值
壞消息
兩件事情:
- 它僅適用於 MyISAM 儲存引擎
- 只允許使用一個數字 auto_increment 值。每個 auto_increment 必須具有關聯的列,以定義同一個表中其他 auto_increment 值的唯一性。
我以前討論過這個
Jun 10, 2012
: MySQL 在沒有自動增量的情況下獲取下一個唯一值Apr 21, 2012
:你怎麼能在一張表中有兩個自動增量列?這是一個範例表
USE test DROP TABLE IF EXISTS stores; CREATE TABLE stores ( store_type int not null, id int not null auto_increment, store_name varchar(128) not null, PRIMARY KEY (store_type,id) ) ENGINE=MyISAM;
這是範例數據
INSERT INTO stores (store_type,store_name) VALUES (1,'Red Lobster'),(1,'Olive Garden'), (2,'ShopRite'),(2,'PathMark'),(2,'Wegman''s'), (3,'McDonald''s'),(3,'Wendy''s'),(3,'Burger King'), (1,'Ruby Tuesdays'),(1,'TGI Fridays'), (4,'BJs'),(4,'Costco'),(1,'Bennigan''s');
讓我們載入它
mysql> USE test Database changed mysql> DROP TABLE IF EXISTS stores; Query OK, 0 rows affected (0.00 sec) mysql> CREATE TABLE stores -> ( -> store_type int not null, -> id int not null auto_increment, -> store_name varchar(128) not null, -> PRIMARY KEY (store_type,id) -> ) ENGINE=MyISAM; Query OK, 0 rows affected (0.08 sec) mysql> INSERT INTO stores (store_type,store_name) VALUES -> (1,'Red Lobster'),(1,'Olive Garden'), -> (2,'ShopRite'),(2,'PathMark'),(2,'Wegman''s'), -> (3,'McDonald''s'),(3,'Wendy''s'),(3,'Burger King'), -> (1,'Ruby Tuesdays'),(1,'TGI Fridays'), -> (4,'BJs'),(4,'Costco'),(1,'Bennigan''s'); Query OK, 13 rows affected (0.00 sec) Records: 13 Duplicates: 0 Warnings: 0 mysql>
現在,看看數據:
mysql> SELECT * FROM stores; +------------+----+---------------+ | store_type | id | store_name | +------------+----+---------------+ | 1 | 1 | Red Lobster | | 1 | 2 | Olive Garden | | 2 | 1 | ShopRite | | 2 | 2 | PathMark | | 2 | 3 | Wegman's | | 3 | 1 | McDonald's | | 3 | 2 | Wendy's | | 3 | 3 | Burger King | | 1 | 3 | Ruby Tuesdays | | 1 | 4 | TGI Fridays | | 4 | 1 | BJs | | 4 | 2 | Costco | | 1 | 5 | Bennigan's | +------------+----+---------------+ 13 rows in set (0.00 sec) mysql>
現在,查看按 PRIMARY KEY 列排序的數據
mysql> SELECT * FROM stores ORDER BY store_type,id; +------------+----+---------------+ | store_type | id | store_name | +------------+----+---------------+ | 1 | 1 | Red Lobster | | 1 | 2 | Olive Garden | | 1 | 3 | Ruby Tuesdays | | 1 | 4 | TGI Fridays | | 1 | 5 | Bennigan's | | 2 | 1 | ShopRite | | 2 | 2 | PathMark | | 2 | 3 | Wegman's | | 3 | 1 | McDonald's | | 3 | 2 | Wendy's | | 3 | 3 | Burger King | | 4 | 1 | BJs | | 4 | 2 | Costco | +------------+----+---------------+ 13 rows in set (0.00 sec) mysql>
試一試 !!!
更新 2013-02-26 12:00 EST
讓我們通過將 4000400 添加到
id
值來增加 SuperMarkets (store_type 2)mysql> UPDATE stores SET id = id + 4000400 WHERE store_type = 2; Query OK, 3 rows affected (0.02 sec) Rows matched: 3 Changed: 3 Warnings: 0 mysql> SELECT * FROM stores ORDER BY store_type,id; +------------+---------+---------------+ | store_type | id | store_name | +------------+---------+---------------+ | 1 | 1 | Red Lobster | | 1 | 2 | Olive Garden | | 1 | 3 | Ruby Tuesdays | | 1 | 4 | TGI Fridays | | 1 | 5 | Bennigan's | | 2 | 4000401 | ShopRite | | 2 | 4000402 | PathMark | | 2 | 4000403 | Wegman's | | 3 | 1 | McDonald's | | 3 | 2 | Wendy's | | 3 | 3 | Burger King | | 4 | 1 | BJs | | 4 | 2 | Costco | +------------+---------+---------------+ 13 rows in set (0.00 sec) mysql>
讓我們添加
A & P
為另一個超市mysql> INSERT INTO stores (store_type,store_name) VALUES (2,'A & P'); Query OK, 1 row affected (0.00 sec) mysql> SELECT * FROM stores ORDER BY store_type,id; +------------+---------+---------------+ | store_type | id | store_name | +------------+---------+---------------+ | 1 | 1 | Red Lobster | | 1 | 2 | Olive Garden | | 1 | 3 | Ruby Tuesdays | | 1 | 4 | TGI Fridays | | 1 | 5 | Bennigan's | | 2 | 4000401 | ShopRite | | 2 | 4000402 | PathMark | | 2 | 4000403 | Wegman's | | 2 | 4000404 | A & P | | 3 | 1 | McDonald's | | 3 | 2 | Wendy's | | 3 | 3 | Burger King | | 4 | 1 | BJs | | 4 | 2 | Costco | +------------+---------+---------------+ 14 rows in set (0.00 sec) mysql>
看起來它可以為你工作!