Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 implementing generalization hierarchy

Author  Topic 

Tim
Starting Member

392 Posts

Posted - 2003-04-28 : 10:42:27
super type is "Vehicle"
sub types are "Car" and "Motorcycle"

Each vehicle must be either car OR motorcycle.


create table Vehicles
(
vid integer identity not null ,
TopSpeed integer not null
)
create table VehiclesCars
(
vid integer not null ,
NumberOfDoors integer not null
)
create table VehiclesMotorcycles
(
vid integer not null
)



Q1) how to implement the disjointness constraint so that each vehicle is one and only one subtype, and in a way that elegantly accomodates many more subtypes ("Truck","Tractor","Pope Mobile"... etc) ?

Q2) how to implement the completness constraint when a row must exist in the vehicle table and in one of the subtype tables. ie, How to escape the deadlock of each table requiring a row in the other?

thanks for any help

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-04-28 : 10:53:08
I actually give an example of doing this in this thread:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=25220

(read where I post the DDL and then examples of inserting data and what gets accepted and rejected).


- Jeff
Go to Top of Page

Tim
Starting Member

392 Posts

Posted - 2003-04-28 : 11:20:12
damn i searched before I posted too :)

So I read that thread.

Jay's is effectively using a view for the supertype and putting all of it's attributes in every subtype.

My example is obviously a contrived one for clarity, but in the actual case, there are many attributes in the supertype and many subtypes, so Jay's answer to it is still messy for me. Also, if every vehicle has a unique registration code, then it becomes messy to enforce it without the supertype table. But as soon as I create that supertype table, I have the deadlock problem if I want to implement the RI.

AndJeff your solution didn't seem to cover the completness constraint since it would be possible to add a row to the supertype without the possibility of there being no corresponding subtype row.

arrgghh


Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-04-28 : 11:26:51
One table can be dependant on a row in another table, but it can't be both ways ... that is not possible. It'd be a chicken and egg thing.

In any RDMS, you can have a table of Orders and a table of OrderDetails --- but there is no way (that I know of!) to say an Order MUST have rows in Order details. Of course, it is trivial to say the rows in the details table must match up with an Order.

If there's a way around this, I'd sure like to know -- but I really don't think it's possible.

- Jeff
Go to Top of Page

Tim
Starting Member

392 Posts

Posted - 2003-04-28 : 11:29:42
yeah that's why it bugs me.

I can model this logically and then can't implement it physically.



Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-04-28 : 12:07:11
quote:

yeah that's why it bugs me.

I can model this logically and then can't implement it physically.



Ummmm...How?

Did use any Modeling tools like ERWin?

How can you model a fact that a Parent must have a child?

I imagine you could probably manage this with a trigger, but I would also imaging that you'd have to keep your hands off those tables a well, or risk a syncronization problem later.



Brett

8-)
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-04-28 : 12:18:56
quote:

Ummmm...How?

Did use any Modeling tools like ERWin?



You can use notepad to model something logically if you want, Brett.

"All Veichles must have 1 corresponding record in the viechle-sub-types table."

Done!


- Jeff
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-04-28 : 12:42:30
...yes, but it's the "in 1 table but not the other table" that defies data modelling techniques....never seen any notation that does that.

I guess you can call it a business rule...but not a relational; model rule (or one I've never encountered).



Brett

8-)

EDIT: OOOPPPPS I take it back, ERWin has a SubType Category relationship....but that's only in the logical model....



Edited by - x002548 on 04/28/2003 12:50:59
Go to Top of Page

Tim
Starting Member

392 Posts

Posted - 2003-04-28 : 22:22:03
Seems I will have to live with the fact that I can't enforce this rule "declaritively" (if that's even a word) and in a way that is database independent.

Edited by - tim on 04/28/2003 22:26:02
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2003-04-28 : 22:33:20
I suppose you can write a trigger that forces an insert into the appropriate sub-table(s) whenever a certain super-type entity is created, and also have it cascade or rollback the deletion of a supertype to the subtype (and vice-versa). You would, by necessity, have to supply some default values for the initial creation of the sub-element and then update them afterwards, or, write a stored procedure to accept all of the relevant attributes and insert them correctly.

However, I think you're trying to force an object-oriented structure into a relational database, and it really just doesn't work, and I don't think it's worth the effort to force it to. Apples are apples, oranges are oranges: neither is better than the other, nor are they both appropriate or interchangeable in the same dessert recipes. If you can't get the perfect tool for the job you have, you need to find some compromise that works with the tools you do have.

Go to Top of Page

byrmol
Shed Building SQL Farmer

1591 Posts

Posted - 2003-04-28 : 22:46:52
You can overcome the completeness constraint by using Views to insert your data.

Create Views that join your "supertype" to your "subtype" and then write an instead of trigger for each. Disallow direct insertion into the supertype table....

DavidM

"SQL-3 is an abomination.."
Go to Top of Page

aiken
Aged Yak Warrior

525 Posts

Posted - 2003-04-29 : 00:58:25
I may be slow, but I'm not sure I understand the intent.

From where I sit, this should also work if rather than creating a seperate table for each subtype, you do:
create table vehicles (vid int identity not null, topspeed int not null, i_types int not null)

create table VehicleTypes (i int identity not null, typename varchar(30))


...using seperate tables for each vehicle type rather than a simple join table is why this problem looks more complicated than it is, I think.

Cheers
-b

Go to Top of Page

Tim
Starting Member

392 Posts

Posted - 2003-04-29 : 19:23:22
aiken, where woul dyou put all the subtype columns? They are different for every subtype. I only showed two subtypes but there may be many more with many columns each...



Go to Top of Page
   

- Advertisement -