|Subject||Re: Auto increment|
--- In email@example.com, "Martijn Tonies"
> > Yes you are right, I made a mistake, I meant I think that a trigger is
> > needed for this. The problem is I don't know how to write such a
> >Yes this looks like what I need. I will try it.
> > Can this be done with a trigger?
> That depends. What do you want done when inserting a new row?
> Set the <field2> value based on <field1>?
> If so, you could try:
> BEFORE INSERT trigger:
> declare variable max_field2 integer;
> select max(field2) from mytable where field1 = new.field1
> into max_field2;
> if (max_field2 is null)
> then max_field2 = 0; /* zero rows found */
> new.field2 = max_field2 + 1;