Subject | Re: Auto increment |
---|---|
Author | a_broekhuis |
Post date | 2004-11-05T14:01:25Z |
--- In firebird-support@yahoogroups.com, "Martijn Tonies"
<m.tonies@u...> wrote:
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
<m.tonies@u...> wrote:
>trigger
> > > > > Yes you are right, I made a mistake, I meant I think that a
> isshould
> > > > > 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
> berest of
> > mentioned (although, Martijn, I do know that you consider Dutch people
> like
> > yourself and Alexander (presumably) somewhat more clever than the
> us -or PK
> > 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
> Constraint.That warning wasn't necessary ;). Field 1 + Field 2 is the PK of that
> </standard warning>
>
> :-)
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