Subject Re: preventing orphaned rows
Author dtrudgett
Duilio Foschi wrote:

> >Have you thought of inserting a detail row from within an AFTER INSERT
> >trigger on the master table?
>
> >Alternatively, insert both the master row
> >and the first detail row using a stored procedure that first does the
> >master, then the detail.
>
> cool, but both solutions look difficult to implement with IBO, that I am
> using.

Perhaps... I suppose it may depend on the approach you're taking with
it. You might want to pursue it on the IBO list.


> >Similarly, you could use a BEFORE DELETE trigger on the detail table
> >to make sure at least one detail row remains for the master row.
>
> there is already a foreign key in the detail table that prevents the master
> to be deleted first.

Yes, but my suggestion was for the reverse case (to prevent the last
detail row from being deleted, so that there will always be at least
one detail row).


>
> If I apply the control you suggest, it seems that nobody will be able to
> delete a whole (master-detail) record any more...

Quite true. That could be handy in some situations, but I assume
that's not what you want :-).

You could also arrange the database security so that only a particular
stored procedure has the required permissions to delete from the
detail table. That procedure would make any necessary checks before
doing the actual deletion.

The other consideration is to ask yourself why you need one or more
detail rows instead of zero or more. It might be because you have
things in the header that should be in a detail row or vice versa.


>
> >Come va il progetto?
>
> spero di consegnare gran parte della nuova versione gia' lunedi'.

E poi avrai la possibilita' di dormire? ;-)

Ciao,

David Trudgett