dbTalk Databases Forums  

Determining Subtype from Supertype

comp.databases comp.databases


Discuss Determining Subtype from Supertype in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #11  
Old   
eKo1
 
Posts: n/a

Default Re: Determining Subtype from Supertype - 09-17-2006 , 09:30 AM






I think you've hit the nail on the head. The only part I don't like
about this approach is that sub.type_id will always contain the same
value. I will evaluate this scheme in detail as I implement it and
report back if I find any anomalies.

--
Bernd


Lennart wrote:
Quote:
eKo1 wrote:
Free wrote:
Use the classifying attribute, e.g. location_type_cd

The problem with that approach is that I have to be very careful with
the data. For example, one could easily add a tuple to the department
relation that references a location whose location_type_cd value
represents a municipality.

I could make (location_id, location_type_cd) the primary key of the
location relation and refernce these attributes in the other relations
but then the value of location_type_cd will be the same in the subtype
relations.

I think you should keep location_id as the primary key (otherwise you
could end up with several rows containing the same loction_id). In
addition to the primary key, add a unique constraint for
(location_id, location_type_cd). Your subtables can reference the
unique constraint instead of the primary key. Example

create table super (
super_id ... not null primary key
type_id ... not null
...
);

alter table super add constraint ak1_super unique (super_id, type_id);
alter table super add constraint c1_super check (type_id in (...));

create table sub (
super_id ... not null primary key
type_id ... not null
...
)

alter table sub add constraint fk1_sub foreign key (super_id, type_id)
references super (super_id, type_id);

alter table sub add constraint c1_sub check (type_id = ...);

HTH
/Lennart


Reply With Quote
  #12  
Old   
eKo1
 
Posts: n/a

Default Re: Determining Subtype from Supertype - 09-17-2006 , 02:10 PM






I spoke to soon. One problem remains in this design: I could have a
record in the supertype table that is not referenced by any of the
records in the subtype tables.

What can I do in this situation? Formulating it in a different manner:
Suppose I have two entities, A and B. An instance of A implies and
instance of B and vice versa. How would I express this contraint with
the relational model?

eKo1 wrote:
Quote:
I think you've hit the nail on the head. The only part I don't like
about this approach is that sub.type_id will always contain the same
value. I will evaluate this scheme in detail as I implement it and
report back if I find any anomalies.

--
Bernd


Lennart wrote:
eKo1 wrote:
Free wrote:
Use the classifying attribute, e.g. location_type_cd

The problem with that approach is that I have to be very careful with
the data. For example, one could easily add a tuple to the department
relation that references a location whose location_type_cd value
represents a municipality.

I could make (location_id, location_type_cd) the primary key of the
location relation and refernce these attributes in the other relations
but then the value of location_type_cd will be the same in the subtype
relations.

I think you should keep location_id as the primary key (otherwise you
could end up with several rows containing the same loction_id). In
addition to the primary key, add a unique constraint for
(location_id, location_type_cd). Your subtables can reference the
unique constraint instead of the primary key. Example

create table super (
super_id ... not null primary key
type_id ... not null
...
);

alter table super add constraint ak1_super unique (super_id, type_id);
alter table super add constraint c1_super check (type_id in (...));

create table sub (
super_id ... not null primary key
type_id ... not null
...
)

alter table sub add constraint fk1_sub foreign key (super_id, type_id)
references super (super_id, type_id);

alter table sub add constraint c1_sub check (type_id = ...);

HTH
/Lennart


Reply With Quote
  #13  
Old   
Lennart
 
Posts: n/a

Default Re: Determining Subtype from Supertype - 09-18-2006 , 12:00 PM




eKo1 wrote:
Quote:
I spoke to soon. One problem remains in this design: I could have a
record in the supertype table that is not referenced by any of the
records in the subtype tables.

True

Quote:
What can I do in this situation? Formulating it in a different manner:
Suppose I have two entities, A and B. An instance of A implies and
instance of B and vice versa. How would I express this contraint with
the relational model?

As far as sql (supported by db2) goes I dont see how this can be
implemented. In the relational model I dont know, assertions at db
level?


/Lennart



Reply With Quote
  #14  
Old   
strawberry
 
Posts: n/a

Default Re: Determining Subtype from Supertype - 09-18-2006 , 12:20 PM




Lennart wrote:
Quote:
eKo1 wrote:
I spoke to soon. One problem remains in this design: I could have a
record in the supertype table that is not referenced by any of the
records in the subtype tables.


True

What can I do in this situation? Formulating it in a different manner:
Suppose I have two entities, A and B. An instance of A implies and
instance of B and vice versa. How would I express this contraint with
the relational model?


As far as sql (supported by db2) goes I dont see how this can be
implemented. In the relational model I dont know, assertions at db
level?


You know, typically newsgroups (like this one) have just one table -
something like this:

messages(message_id*,parent_id,message,date_posted )

new topics, then, are simply messages in which the parent_id is NULL

maybe it's an inappropriate model for your db but I thought I'd mention
it just in case.



Reply With Quote
  #15  
Old   
Ed Prochak
 
Posts: n/a

Default Re: Determining Subtype from Supertype - 09-19-2006 , 07:30 AM




eKo1 wrote:
Quote:
eKo1 wrote:
Doesn't my example constitute a good business example? I just don't
see how I can have super/sub-types without running into the
aformetioned problems.

Another problem: How can I enforce referential integrity between the
supertype and the subtype? I could easily have a tuple in the location
relation that isn't referenced by any of the subtype relations.

The relational model is really limited to these types of situations.
I still think you are mixing physical model with logical model too
soon. You keep talking about ID values and type fields, but you have
never defined exactly what constitutes a LOCATION. What attributes does
it have? What attributes does a department have that make it a location
but make it different from a sector? What attributes make them the
same so that you pick it as a location?

Forget the IDs and types and define your attributes. So far all you
have mentioned is that each has a name. Your problem stems from this
basic issue. Before figuring out whether to fit things in square or
round holes, you need to determine whether they are even pegs instead
of chairs and apples.

HTH,
ed



Reply With Quote
  #16  
Old   
Damien
 
Posts: n/a

Default Re: Determining Subtype from Supertype - 09-19-2006 , 07:50 AM



eKo1 wrote:
Quote:
eKo1 wrote:
I think you've hit the nail on the head. The only part I don't like
about this approach is that sub.type_id will always contain the same
value. I will evaluate this scheme in detail as I implement it and
report back if I find any anomalies.
But having that duplication is a good thing! You can immediately spot a
check constraint you can apply on the table which will reject duff data
even before it has to check the foreign key constraint.

Quote:
I spoke to soon. One problem remains in this design: I could have a
record in the supertype table that is not referenced by any of the
records in the subtype tables.

What can I do in this situation? Formulating it in a different manner:
Suppose I have two entities, A and B. An instance of A implies and
instance of B and vice versa. How would I express this contraint with
the relational model?

You're not going to be able to do that (with most SQL products I'm
aware of), since you're going to have to insert into one of the tables
before you insert into the other, so there must be some period of time
when you cannot tie the relationship in both directions.

Even in those products which allow you to defer constraint checking
until the end of the transaction, they don't tend to have the only type
of constraint you'd be able to use in this case - a database
constraint. A foreign key constraint is insufficient, since the foreign
table could be one of many different tables.

If I were you, I'd try re-reading some of Eds replies and try to
understand what he's getting at.

Damien



Reply With Quote
  #17  
Old   
eKo1
 
Posts: n/a

Default Re: Determining Subtype from Supertype - 09-19-2006 , 11:56 AM




Ed Prochak wrote:
Quote:
I still think you are mixing physical model with logical model too
soon. You keep talking about ID values and type fields, but you have
never defined exactly what constitutes a LOCATION. What attributes does
it have? What attributes does a department have that make it a location
but make it different from a sector? What attributes make them the
same so that you pick it as a location?
Well, as I said, I have three entities: department, municipality and
sector. These are political divisions. Each department has one or more
municipalities and each municipality has one or more sectors. A
location can reference an instance of one of these political divisions.

Each political division has a name so department, municipality and
sector have name as an attribute.

Is that a good enough description?



Reply With Quote
  #18  
Old   
Ed Prochak
 
Posts: n/a

Default Re: Determining Subtype from Supertype - 09-19-2006 , 04:03 PM




eKo1 wrote:
Quote:
Ed Prochak wrote:
I still think you are mixing physical model with logical model too
soon. You keep talking about ID values and type fields, but you have
never defined exactly what constitutes a LOCATION. What attributes does
it have? What attributes does a department have that make it a location
but make it different from a sector? What attributes make them the
same so that you pick it as a location?

Well, as I said, I have three entities: department, municipality and
sector. These are political divisions. Each department has one or more
municipalities and each municipality has one or more sectors. A
location can reference an instance of one of these political divisions.

Each political division has a name so department, municipality and
sector have name as an attribute.

Is that a good enough description?
Actually, no. You still keep mentioning only ONE attribute, name. There
has to be more.

Also at the logical level you must ask questions like
What is a location? How is it used? (say it in a plain english
sentence)
Note that nothing in these kinds of questions mentions anything about
an ID.

For example, if I was creating a scheduling application and it could
refer to locations such as offices, conference rooms and auditoriums.
Each location has a name (Ed's office, Conf Room B, the Famous Person
Auditorium), but it also has attributes like seating capacity, and
physical address (e.g. Conf Room B is first floor, West Building, 123
This Street, Oak, Ohio)

Now your Location entity is less about physical position (I think), but
it also seems to have the added information that the locations are
hierarchically related. That doesn't affect the location type though.
But it should give you more attributes and relations to consider.

Note your description above also mentions the existence of another
entity: Political Division. What are its attributes? What makes a
sector a political division?

Have you tried drawing an ERD? That would be your best step before
thinking about tables.
Ed



Reply With Quote
  #19  
Old   
eKo1
 
Posts: n/a

Default Re: Determining Subtype from Supertype - 09-19-2006 , 08:51 PM




Ed Prochak wrote:
Quote:
Actually, no. You still keep mentioning only ONE attribute, name. There
has to be more.
Why? For my application, I only care about the name. I don't need any
other information.

Quote:
Also at the logical level you must ask questions like
What is a location? How is it used? (say it in a plain english
sentence)
Note that nothing in these kinds of questions mentions anything about
an ID.
How is it used? Well, I have another entity called "telephone number
range". Each instance of this entity must have a location attribute
that specifies where the range of telephone numbers operate. For
example, the range 123000 - 123999 are telephone numbers in the
department of Foo. The location attribute, therefore, contains the
department Foo. You see, a telephone number range may be assigned
department-wise, municipality-wise or sector-wise.

Quote:
Now your Location entity is less about physical position (I think), but
it also seems to have the added information that the locations are
hierarchically related. That doesn't affect the location type though.
But it should give you more attributes and relations to consider.
Yes, the hierarchy department->municipality->sector is not expressed in
the location entity.

Quote:
Note your description above also mentions the existence of another
entity: Political Division. What are its attributes? What makes a
sector a political division?
The only attribute this would have is name. There would be three
instances of such an entity: one named Department, one named
Municipality and one named Sector. They are political divisions because
that is the way things are divided here politically.

Quote:
Have you tried drawing an ERD? That would be your best step before
thinking about tables.
I did draw one (in my mind since it is simple) and I noticed that since
the attribute name is common to all subtypes, then that attribute
should be in the supertype, i.e. location. But then Department won't
have any attributes. Municipality would have on attribute, namely
department and sector would also have one attribute, namely
municipality.



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.