开发者

MySQL autoincrement tied with an attribute

开发者 https://www.devze.com 2023-02-17 12:43 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消