MySQL autoincrement tied with an attribute
Is it possible to have a column say ORDER in a table to autoincrement only when inserti开发者_开发百科ng a new ID? So say I have the following column:
ID ORDER
1 0
1 1
1 2
1 3
1 4
When I insert another ID order increments to 5, when I insert 2 as a new ID order starts from 0.
This example uses a composite index but numbering starts from 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)
If you want that it starts from zero you need a trigger as already said.
精彩评论