Subject Re: Auto increment
Author a_broekhuis
--- In firebird-support@yahoogroups.com, "Martijn Tonies"
<m.tonies@u...> wrote:
>
> > > > > 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
> > > > > trigger. 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;
> > > > begin
> > > > 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;
> > > > end;
> > >
> > > Yes this looks like what I need. I will try it.
> >
> > Hmm, looks like a newbie to me. If so, I think a warning about the
> potential
> > problems with concurrency and making these fields a composite key
should
> be
> > mentioned (although, Martijn, I do know that you consider Dutch people
> like
> > yourself and Alexander (presumably) somewhat more clever than the
rest of
> us -
> > and as such cannot imagine any of you stumbling at such an easy hurdle
> ;o).
>
> Alright alright...
>
> <standard warning>
> This isn't multi user safe unless you make Field1 + Field2 a Unique
or PK
> Constraint.
> </standard warning>
>
> :-)

That warning wasn't necessary ;). Field 1 + Field 2 is the PK of that
table, so everything is ok :).

But thx anyway for your concers :). And yes I think I'm a newbie, but
I do know some stuff and pitfalls about databases.

Alexander