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
  #1  
Old   
eKo1
 
Posts: n/a

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






I have four entities: location, department, municipality and sector.
municipality is a subtype of department, sector is a subtype of
municipality, and department, municipality and sector are subtypes of
location. Here are their relation schemas:

location(id)
department(id, name)
municipality(id, dept_id, name)
sector(id, municip_id, name)

department.id, municipality.id and sector.id reference location.id.
dept_id references department.id and municip_id references
municipality.id. The *.id attributes are all primary keys.

Given a value of location.id, how would I determine what subtype it
belongs to? The only way I see of doing this is by querying each
instance of department, municipality and sector individually. I was
thinking of adding an extra attribuite to location called div_type
whose value would tell me whether location.id points to a department,
municipality or sector but this is not very elegent since one could
have a location.id value that references a sector but the div_type says
it is a department.

--
Bernd


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

Default Re: Determining Subtype from Supertype - 09-05-2006 , 06:21 PM







eKo1 wrote:
Quote:
I have four entities: location, department, municipality and sector.
municipality is a subtype of department, sector is a subtype of
municipality, and department, municipality and sector are subtypes of
location. Here are their relation schemas:

location(id)
department(id, name)
municipality(id, dept_id, name)
sector(id, municip_id, name)

department.id, municipality.id and sector.id reference location.id.
dept_id references department.id and municip_id references
municipality.id. The *.id attributes are all primary keys.

Given a value of location.id, how would I determine what subtype it
belongs to? The only way I see of doing this is by querying each
instance of department, municipality and sector individually. I was
thinking of adding an extra attribuite to location called div_type
whose value would tell me whether location.id points to a department,
municipality or sector but this is not very elegent since one could
have a location.id value that references a sector but the div_type says
it is a department.

--
Bernd
I guess you could use aliases as in (untested):

SELECT
d.id AS department_location_id,
m.id AS municipality_location_id,
s.id AS sector_location_id
FROM
department d,
municipality m,
sector s
WHERE
d.id = x
OR m.id = x
OR s.id = x



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

Default Re: Determining Subtype from Supertype - 09-06-2006 , 09:57 AM




eKo1 wrote:
Quote:
I have four entities: location, department, municipality and sector.
municipality is a subtype of department, sector is a subtype of
municipality, and department, municipality and sector are subtypes of
location. Here are their relation schemas:

location(id)
department(id, name)
municipality(id, dept_id, name)
sector(id, municip_id, name)

department.id, municipality.id and sector.id reference location.id.
dept_id references department.id and municip_id references
municipality.id. The *.id attributes are all primary keys.

Given a value of location.id, how would I determine what subtype it
belongs to? The only way I see of doing this is by querying each
instance of department, municipality and sector individually. I was
thinking of adding an extra attribuite to location called div_type
whose value would tell me whether location.id points to a department,
municipality or sector but this is not very elegent since one could
have a location.id value that references a sector but the div_type says
it is a department.

--
Bernd
to solve these types of problems, I find it best to leave the physical
model behind (IDs) and try to define the problem in a descriptive form.
IOW, could you describe your problem at a data model (logical) level?
(Sometimes just rephrasing the question reveals the answer.) If
location has no data other than ID, then how would a user ever select
it? (Implied in that question is a hint at the guideline, never reveal
pseudoKey values to the end user.)

Ed



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

Default Re: Determining Subtype from Supertype - 09-06-2006 , 02:45 PM



strawberry wrote:
Quote:
I guess you could use aliases as in (untested):

SELECT
d.id AS department_location_id,
m.id AS municipality_location_id,
s.id AS sector_location_id
FROM
department d,
municipality m,
sector s
WHERE
d.id = x
OR m.id = x
OR s.id = x
I would still need to look at each of d.id, m.id and s.id and check
which one is not null. This is not very elegant.



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

Default Re: Determining Subtype from Supertype - 09-06-2006 , 02:52 PM



Ed Prochak wrote:
Quote:
to solve these types of problems, I find it best to leave the physical
model behind (IDs) and try to define the problem in a descriptive form.
IOW, could you describe your problem at a data model (logical) level?
(Sometimes just rephrasing the question reveals the answer.) If
location has no data other than ID, then how would a user ever select
it? (Implied in that question is a hint at the guideline, never reveal
pseudoKey values to the end user.)
Rephrasing: I have an entity called location which can be one of either
a department, municipality or sector. Given an instance of a location
entity, how can I determine whether it is a department, municipality or
sector?



Reply With Quote
  #6  
Old   
Free Data Advice
 
Posts: n/a

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



Quote:
Ed Prochak wrote:
to solve these types of problems, I find it best to
leave the physical model behind (IDs) and try to define
the problem in a descriptive form. IOW, could you
describe your problem at a data model (logical) level?
(Sometimes just rephrasing the question reveals the
answer.) If location has no data other than ID, then
how would a user ever select it? (Implied in that
question is a hint at the guideline, never reveal
pseudoKey values to the end user.)
Rephrasing: I have an entity called location which can be
one of either a department, municipality or sector. Given
an instance of a location entity, how can I determine
whether it is a department, municipality or sector?

Use the classifying attribute, e.g. location_type_cd


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

Default Re: Determining Subtype from Supertype - 09-08-2006 , 05:00 PM



Free wrote:
Quote:
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.



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

Default Re: Determining Subtype from Supertype - 09-08-2006 , 11:32 PM



Free wrote:
Quote:
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.
All super/sub type data structures require a classifying
attribute to identify which subtype applies for the instance
of the super type. This is due to the mutual exclusivity of
the sub types. The data structure allows for more than one
subtype to be applicable for a given super type, but I've
yet to find a good business example. So if you don't have a
classifying attribute, you don't have a super/sub type.
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.



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

Default Re: Determining Subtype from Supertype - 09-14-2006 , 04:54 PM



eKo1 wrote:
Quote:
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.



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

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




eKo1 wrote:
Quote:
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
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.