Domanda

With in my ERD I have a disjoint mandatory relationship. See the following example.

enter image description here

** Assume "Teen" and "Adult" entities have their own unique attributes.

How to map this kind of a relation ship. And how to implement this with in tables.

[As I know you have to create two tables called "Teen" and "Adult". Both have the same primary key "st_id". But the problem is, in both tables the "st_id" has to be unique since the relationship is mandatory. How to implement that?]

Example:

**Teen**                               
st_id | name
001     AA
002     BB

**Adult**
st_id | name
(as the st_id you can't have the value 001 again since it is already in the Teen table)
È stato utile?

Soluzione

What is stopping you from having three tables:

STUDENT [PK: st_id] (optionally also an attribute of type BOOLEAN or similar, "is_teen" or "is_adult")
TEEN    [PK: st_id]
ADULT   [PK: st_id]

We will add one row into STUDENT for every new student; each time you add a student, then create a TEEN row if the new student is a teen, otherwise create a new ADULT row.
In either case, the st_id to use will be that of the new student. The st_ids in TEEN and STUDENT will be mutually exclusive, but all st_ids will point back to the appropriate STUDENT row.

Your ERD snippet appears to be for a Logical Data Model; I have copied that into the physical implementation directly. In the real world, often we would "roll up" (denormalize) the TEEN and ADULT attributes into the STUDENT entity to limit the amount of joining needed to get to all the info. available for a given student.

Optionally, you may choose to define a pair of foreign key relationships between STUDENT and TEEN and between STUDENT and ADULT.

Altri suggerimenti

Why not use 1 table for teen and adult like this

person
------
id         int
name       varchar
is_adult   bit

study
-------
id         int
person_id  int
...
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top