Download presentation
Presentation is loading. Please wait.
Published byJessica Franklin Modified over 9 years ago
1
Databases : Relational Model 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes via his course web page (http://infolab.stanford.edu/~ullman/dscb/gslides.html)http://infolab.stanford.edu/~ullman/dscb/gslides.html
2
STEMPNU 2 A Relation is a Table NameNameF HiteJinro CassOB Attributes (column headers) Tuples (rows) Beers (Name, NameF)
3
STEMPNU 3 Schemas Relation schema = relation name attributes, in order (+ types of attributes). Example: Beers(name, manf) or Beers(name: string, manf: string) key definition Database = collection of relations. Database schema = set of all relation schemas in the database.
4
STEMPNU 4 Why Relations? Very simple model. Often matches how we think about data. Abstract model that underlies SQL, the most important database language today. But SQL uses bags, while the relational model is a set-based model. Bag allows duplications like {1,2,1} Set does NOT allow duplications {1,2} Algebraic Background Relational Algebra
5
STEMPNU 5 From E/R Diagrams to Relations Entity sets become relations with the same set of attributes. Relationships become relations whose attributes are only: The keys of the connected entity sets. Attributes of the relationship itself.
6
STEMPNU 6 Entity Set -> Relation Relation: Beers(name, manf) Beers name manf
7
STEMPNU 7 Relationship -> Relation DrinkersBeers Likes Likes(drinker, beer) Favorite Favorite(drinker, beer) Married husband wife Married(husband, wife) name addr name manf Buddies 1 2 Buddies(name1, name2)
8
STEMPNU 8 Combining Relations It is OK to combine the relation for an entity-set E with the relation R for a many-one relationship from E to another entity set. E 1 (A 11, A 12, A 13 ), E 2 (A 21, A 22, A 23 ) R(A 11, A 21 ): many-one E ’ 1 (A 11, A 12, A 13, A 21 ) R(A 11, A 21 ): one-one E ’ 1 (A 11, A 12, A 13, A 21 ) ? R(A 11, A 21 ): one-many E ’ 1 (A 11, A 12, A 13, A 21 ) ? Example: Drinkers(name, addr) and Favorite(drinker, beer) combine to make Drinker1(name, addr, favBeer).
9
STEMPNU 9 Risk with Many-Many Relationships Combining Drinkers with Likes would be a mistake. It leads to redundancy, as: name addr beer Sally 123 Maple Bud Sally 123 Maple Miller Redundancy
10
STEMPNU 10 Handling Weak Entity Sets Relation for a weak entity set must include attributes for its complete key (including those belonging to other entity sets), as well as its own, nonkey attributes A supporting (double-diamond) relationship is redundant and yields no relation.
11
STEMPNU 11 Example LoginsHostsAt name Hosts(hostName) Logins(loginName, hostName, time) At(loginName, hostName) time At becomes part of Logins
12
STEMPNU 12 Entity Sets With Subclasses Three approaches: 1. Object-oriented: each entity belongs to exactly one class; create a relation for each class, with all its attributes. 2. E/R style: create one relation for each subclass, with only the key attribute(s) and attributes attached to that E.S.; entity represented in all relations to whose subclass/E.S. it belongs. 3. Use nulls: create one relation; entities have null in attributes that don ’ t belong to them.
13
STEMPNU 13 Example Beers Ales isa name manf color
14
STEMPNU 14 Object-Oriented namemanf BudAnheuser-Busch Beers name manfcolor Summerbrew Pete’sdark Ales
15
STEMPNU 15 E/R Style namemanf Bud Anheuser-Busch Summerbrew Pete’s Beers name color Summerbrew dark Ales
16
STEMPNU 16 Using Nulls namemanf color Bud Anheuser-Busch NULL Summerbrew Pete’s dark Beers
17
STEMPNU 17 Comparisons O-O approach good for queries like “ find the color of ales made by Pete ’ s. ” Just look in Ales relation. E/R approach good for queries like “ find all beers (including ales) made by Pete ’ s. ” Just look in Beers relation. Using nulls saves space unless there are lots of attributes that are usually null.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.