0

Suppose the ASCII tree below is a UML generalized concept model. To represent these entities/classes in UML, lets assume [mammal] a super class, and the level 1 classes, [wild] and [domestic] are sub classes in UML. In other words, class [wild] inherits all attributes from [mammal], and [domestic] inherits all attributes from [mammal] as well. Furthermore, [raccoon] and [white tail deer] inherit all attributes from [wild].

I want to convert the UML model to a relational database model.

Notice how [raccoon] extends both [wild] and [domestic]. In other words, it has two parents. I would normally make the primary key of all descendant tables the same as their parent primary key. In addition, I'd make the foreign key in the descendant table the same as the primary key in its parent table. Because [raccoon] has two parents, how would I build that table?

mammal
 |
 +- wild
 | |
 | +- raccoon
 | |
 | +- white tail deer
 |
 +- domestic
 |
 +- cat
 | |
 | +- siberian
 |
 +- dog
 | |
 | +- golden retriever
 | |
 | +- lab
 |
 +- raccoon 
asked Nov 20, 2013 at 5:09
3
  • 1
    I think having 2 FKs from racoon(pk), one referencing wild(pk) and the other referencing domestic(pk) would solve this. Right? Commented Nov 20, 2013 at 9:25
  • That's what I thought too. Would there also be a PK for the sub class (child table) in addition to these two FKs? I can't make it a composite PK because both would have to exist (NOT NULL). Commented Nov 20, 2013 at 15:53
  • 1
    I don't see why you would need a composite key here or why that would be impossible. Commented Nov 20, 2013 at 16:00

1 Answer 1

3

You can have two (or more) foreign keys in a table. They can be both regarding the same attribute and referencing different tables. So you could have 2 foreign keys from racoon(pk), one referencing wild(pk) and the other referencing domestic(pk). Something like:

CREATE TABLE raccon
( mammal_id INT PRIMARY KEY
, FOREIGN KEY (mammal_id)
 REFERENCES domestic (mammal_id)
, FOREIGN KEY (mammal_id)
 REFERENCES wild (mammal_id)
) ;

(assuming all tables have mammal_id as the primary key)

answered Nov 20, 2013 at 15:59
3
  • Is that Oracle? Can you add SQL Server to your answer so I'm doing it correctly? I forgot to add the tag. Commented Nov 20, 2013 at 16:34
  • 1
    The syntax is valid for both Oracle and SQL-Server: SQL-Fiddle Commented Nov 20, 2013 at 16:37
  • Learn something new every day Commented Nov 20, 2013 at 16:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.