![]() | |
#11
| |||
| |||
|
|
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 |
#12
| |||
| |||
|
|
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 |
#13
| |||
| |||
|
|
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? |
#14
| |||
| |||
|
|
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? |
#15
| |||
| |||
|
|
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. |
#16
| |||
| |||
|
|
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 |
|
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 |
#17
| |||
| |||
|
|
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? |
#18
| |||
| |||
|
|
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? |
#19
| |||||
| |||||
|
|
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. |
|
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. |
![]() |
| Thread Tools | |
| Display Modes | |
| |