2011-03-17 63 views
1

是否有可能有一個列說在表中的ORDER自動增量只有當插入一個新的ID?所以說我有以下的列:MySQL的自動增量綁定屬性

ID ORDER 
1 0 
1 1 
1 2 
1 3 
1 4 

當我插入另一ID順序遞增到5,當我插入2作爲新的ID順序從0

+0

你需要一個觸發器 – Dalen 2011-03-17 00:10:59

+0

如果這只是爲了排序爲什麼不只是一個標準的自動增量列?在查看一個組時,您在「訂單」字段中會有空白,但組內的訂單仍然是正確的。 – 2011-03-17 00:22:25

+0

好吧,如果是這樣的話,我只是在浪費一堆空間? – aherlambang 2011-03-17 00:31:20

回答

3

本示例啓動使用組合索引而編號從1開始

create table test (
    id smallint not null, 
    norder int unsigned not null auto_increment, 
    primary key (id,norder) 
) engine=myisam; 

insert into test(id) values (1),(1),(1),(2),(3),(1),(1),(4),(5),(1) 

如果你想從零開始,你需要一個觸發器,如前所述。

+1

實際上它有可能從0開始從你的方法作爲解釋mysql文檔:http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html#sqlmode_no_auto_value_on_zero,無論如何它不是從0開始的建議練習 – Dalen 2011-03-17 00:31:45

+0

我不知道。謝謝Dalen :) – 2011-03-17 00:37:39

+0

所以你只是設置ID和沒有訂單作爲主鍵沒有訂單作爲auto_increment,這自動工作?不知道.. .. – aherlambang 2011-03-17 01:07:29