ra examples. let’s build a family db erd person bday byear gender name

25
RA examples

Upload: gary-brodell

Post on 14-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

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

RA examples

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

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);

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

ERD

Person

Bday Byear GenderName

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

ERD

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

Parent Role

Person

Bday Byear GenderName

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

ERD

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

Parent RoleParent Role

Person

Bday Byear GenderName

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

Building the basic family tree

• Finding a person’s parents:•

• Finding a person’s children:•

(basically the same)

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

Building the basic family tree

• Finding a person’s sibling:• )

• And if we only wanted brothers?

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

Grandparents:

• How do you find a person grandparents:•

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

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

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

Grandparents:

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

•))

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

:

• 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!

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

:

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

)

• The length of the query depends on K

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

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)

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

Cousins

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

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

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

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)

• (

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

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)

• (

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

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.

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

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:

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

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):

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

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

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

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– …

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

In-laws• :=

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

Question

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

• (clue: use division)

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

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:

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

Answer

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

• Compute the final result using division: