RA examples. Let’s build a Family DB ERD Person Bday Byear Gender Name.

Post on 14-Dec-2015

216 views 0 download

Tags:

Transcript of RA examples. Let’s build a Family DB ERD Person Bday Byear Gender Name.

RA examples

Let’s build a Family DB

• Describe a person by it’s name, year and day of birth and gender:

• Person(Name, Byear, Bday, gender);

ERD

Person

Bday Byear GenderName

ERD

Let’s add parents:Parent(Cname, Cyear, Cday, Role, Pname, Pyear, Pday)(

Parent Role

Person

Bday Byear GenderName

ERD

Is this ERD perfect?Can you make it closer to the real world?

Parent RoleParent Role

Person

Bday Byear GenderName

Building the basic family tree

• Finding a person’s parents:•

• Finding a person’s children:•

(basically the same)

Building the basic family tree

• Finding a person’s sibling:• )

• And if we only wanted brothers?

Grandparents:

• How do you find a person grandparents:•

Grandparents:

• How would you change the previous Query if you only wanted grandmothers:

•))

• Or:•

))

• The 2 queries will return the same result if the DB is sane

Grandparents:

• How would you change the previous Query if you only wanted the grandparents on the mother’s side:

•))

:

• Definition: (by the recursion)

• is a person’s parent.• is a person’s grandparent• is a person’s great-grandparent• Etc…

• By this definition, what is a person’s ?– The person!

:

• How do you find a person’s ?(for a given k)

)

• The length of the query depends on K

Ancestors

• Definition:– A is an ancestor of B if there exist a K such that

• How would write a query to find all ancestors?– This seemingly cannot be done in RA!• (can be proved)

Cousins

• Definition:• two people are cousins if they share a common grandparent.

• How two find a person’s cousins:• (

K-Cousins

• Definition:• two people are k-cousins if they share a common .• 0-cousin = sibling• 1-cousin = cousin• 2-cousin = share a common great-grandparent

• How two find a person’s k-cousins:(for a given k)

• (

K-Cousins m-times removed

• Definition:• Person A and person B are k-cousins m-times removed if A’s

is B’s k-cousin.• k-cousin 0-times removed = k-cousin• k-cousin 1-times removed = parent is k-cousin

• How two find a person’s k-cousins m-times removed:(for a given k,m)

• (

First-Born children

• Definition:– A person is a first born if at least one of it’s

parents doesn’t have any children older than the person.

First-Born children

• Query for finding first-borns:• All pairs of siblings:

• All pairs of siblings in which the first sibling is younger than the second sibling:

• All people that have an older sibling:

First-Born children

• Query for finding first-borns: (cont.)• All people that have a sibling:

• All people that are an only-child:

• All people that don’t have an older sibling:

• All first-borh children (either oldest or only-child):

Let’s add spouses…

• We can make our DB a bit more complicated:

Spouse(Name, Byear, Bday, SpouseName, SpouseByear, SpouseBday)

Parent RoleParent Role

Person

Bday Byear GenderName

Spouse

In-laws

• Definition:– A person’s <relation>-in-laws our the person’s

spouse’s <relation>– Parent-in-law = spouse’s parent– Sibling-in-law = spouse’s sibling– …

In-laws• :=

Question

• Get the names of all the mothers that gave birth every year during the 80’s

• (clue: use division)

Answer

• Get all the parents and the years in which they add a child:

• Get all the females:

• Get all the mothers and the years in which they gave birth:

Answer

• We will define the set of years in the 80’s:

• Compute the final result using division: