Guarantee the completeness-constraint of super-sub-entity-typs

Guarantee the completeness-constraint of super-sub-entity-typs

Post by Dominik Rappapor » Fri, 07 May 1999 04:00:00



Hello out there,

I hope somebody can help me to find a solution to following problem:

Assume we have following entity-relationship-diagram:

     --------
     |person|
     --------
        |
       / \ (1,1)
      /   \
  ------------
  |          |
------    -------
|man |    |woman|
------    -------

As a result we have 3 tables:
* person with the primary key p# and the attributes which have man and
woman in common
* man and woman which have each p# as primary key and as foreign key to
person too, and the
  attributes which are specific for man and woman

The ER-diagramm tells us also that:
* a person can only be a man OR a woman. It can't be a man and a woman
simultaneously.
  -> disjointness-constraint
* a person MUST be a man or a woman. It is forbidden that lines in the
person table exists
  which are not referenced by a line in the man- or the woman table.
  -> completeness constraint

The implementation of the disjointness constraint is easy. We just have
to create two triggers, one for the man and one for the woman table
which are fired by an insert in the corresponding table. The triggers
has to look if the primary key still exists in the other table and if
yes to rollback the transaction.
In contrast the implementation of the completeness constraint is much
more difficult. In theory the insert operation into the person-table and
i.e. the man table should be one (atomic) operation. But the insert
statement in SQL doesn't support an insert into two table
simultaneously. So a user has two make two separate inserts. But nobody
can guarantee the he or she makes the second. So it could be, that we
have a line in person which is never referenced by a line in man or
woman - in fact a person with no sex. My first idea to fix the problem
was to write a stored procedure. But I can't force the user to call that
procedure instead of making the inserts manual. My second idea was to
create a view which results of an inner join of the person and i.e. the
man table over the primary key. Next I could prevent the user to insert
directly into the person table with the grant command and as a result
the user can only insert over the view were he or she must use a
complete list of all attributes (those of person and those of man). But
usually views which results of a join operation can't be updated. So I
had to revoke that idea too. My last one was to add a (boolean) field to
the person table which determines the sex of the person (i.e. 'M' and
'F'). Then an insert trigger on the person table inspect the boolean
value and make the corresponding insert operation in the man or in the
woman table. The disadvantage of that method is that all
man-specific-attributes can only be set to their default values or to
NULL. But the completeness would be guaranteed. A second disadvantage is
that if we have more than two sub-entity-typs the insert trigger must
implement a huge and complex switch-case-statement. And the user must
make an update statement to set the sub-type specific attributes.

I know that this example could be done much more easier if we include
all man and woman specific attributes in the person table. But in real
life that may be impossible for example if we have the super-typ
business contact and customer and supplier with lots of specific
attributes as sub-types.

I hope some of you can help me.

Greetings

Dominik

 
 
 

Guarantee the completeness-constraint of super-sub-entity-typs

Post by Ruud de Kote » Tue, 11 May 1999 04:00:00


Hi Dominik,

It is a bit difficult to suggest a solution to your problem, since you
do not indicate
the environment you are working on. As far as I am concerned, I can give
you a
suggestion that works in an Oracle environment, but I am not into other
database
systems...

In an Oracle environment, you might get a fair part of what you want to
achieve
using stored procedures. The access rights for those happen to be
independent of
the access rights on the underlying database objects. That is: you do
not need to
have access rights on the tables to be able to execute a procedure
working on those
tables. I think this has been introduced exactly for the kind of problem
you are
mentioning: you 'd like to be able to assure users will access the
tables by means of
the stored procedure!

The idea would then be to have one database account that is the owner of
all database
objects in your application, both tables and procedures (and views, and
indexes, and...).
Other user(s) will then be defined for day-to-day access on these
objects, where these
users are only granted the privileges they need form a business
perspective. In your
example, they 'd get privileges on the procedure assuring both tables
are filled and not
on the tables themselves.

Hope you can use this to advantage.

Regards,

Ruud de Koter

--------------------------------------------------------------------------------------

Ruud de Koter                             HP OpenView Software Business
Unit
Senior Software Engineer              IT Service Management Operation
Telephone: +31 (20) 514 15 89    Van Diemenstraat 200
Telefax  : +31 (2) 514 15 90         PO Box 831
Telnet   : 547 - 1589                     1000 AV  Amsterdam, the
Netherlands

internet: http://www.openview.hp.com/itsm
             http://www.openview.hp.com/assetview
intranet: http://ovweb.bbn.hp.com/itservicemanager
--------------------------------------------------------------------------------------


>  Hello out there,

> I hope somebody can help me to find a solution to following problem:

> Assume we have following entity-relationship-diagram:

>      --------
>      |person|
>      --------
>         |
>        / \ (1,1)
>       /   \
>   ------------
>   |          |
> ------    -------
> |man |    |woman|
> ------    -------

> As a result we have 3 tables:
> * person with the primary key p# and the attributes which have man and
> woman in common
> * man and woman which have each p# as primary key and as foreign key
> to person too, and the
>   attributes which are specific for man and woman

> The ER-diagramm tells us also that:
> * a person can only be a man OR a woman. It can't be a man and a woman
> simultaneously.
>   -> disjointness-constraint
> * a person MUST be a man or a woman. It is forbidden that lines in the
> person table exists
>   which are not referenced by a line in the man- or the woman table.
>   -> completeness constraint

> The implementation of the disjointness constraint is easy. We just
> have to create two triggers, one for the man and one for the woman
> table which are fired by an insert in the corresponding table. The
> triggers has to look if the primary key still exists in the other
> table and if yes to rollback the transaction.
> In contrast the implementation of the completeness constraint is much
> more difficult. In theory the insert operation into the person-table
> and i.e. the man table should be one (atomic) operation. But the
> insert statement in SQL doesn't support an insert into two table
> simultaneously. So a user has two make two separate inserts. But
> nobody can guarantee the he or she makes the second. So it could be,
> that we have a line in person which is never referenced by a line in
> man or woman - in fact a person with no sex. My first idea to fix the
> problem was to write a stored procedure. But I can't force the user to
> call that procedure instead of making the inserts manual. My second
> idea was to create a view which results of an inner join of the person
> and i.e. the man table over the primary key. Next I could prevent the
> user to insert directly into the person table with the grant command
> and as a result the user can only insert over the view were he or she
> must use a complete list of all attributes (those of person and those
> of man). But usually views which results of a join operation can't be
> updated. So I had to revoke that idea too. My last one was to add a
> (boolean) field to the person table which determines the sex of the
> person (i.e. 'M' and 'F'). Then an insert trigger on the person table
> inspect the boolean value and make the corresponding insert operation
> in the man or in the woman table. The disadvantage of that method is
> that all man-specific-attributes can only be set to their default
> values or to NULL. But the completeness would be guaranteed. A second
> disadvantage is that if we have more than two sub-entity-typs the
> insert trigger must implement a huge and complex
> switch-case-statement. And the user must make an update statement to
> set the sub-type specific attributes.

> I know that this example could be done much more easier if we include
> all man and woman specific attributes in the person table. But in real
> life that may be impossible for example if we have the super-typ
> business contact and customer and supplier with lots of specific
> attributes as sub-types.

> I hope some of you can help me.

> Greetings

> Dominik