1 The Relational Data Model Tables Schemas Conversion from E/R to Relations
2 A Relation is a Table namemanf Corvette G.M. MaximaNissan cars Attributes (column headers) Tuples (rows)
3 Schemas uRelation schema = relation name and attribute list. wOptionally: types of attributes. wExample: cars(name, manf) or cars(name: string, manf: string) uDatabase = collection of relations. uDatabase schema = set of all relation schemas in the database.
4 Why Relations? uVery simple model. uOften matches how we think about data. uAbstract model that underlies SQL, the most important database language today.
5 From E/R Diagrams to Relations uEntity set -> relation. wAttributes -> attributes. uRelationships -> relations whose attributes are only: wThe keys of the connected entity sets. wAttributes of the relationship itself.
6 Entity Set -> Relation Relation: cars(model, manf) cars model manf
7 Relationship -> Relation driverscars Likes Likes(driver, car) Favorite Favorite(driver, car) Married husband wife Married(husband, wife) name addr name manf Mustangdies 1 2 Mustangdies(name1, name2)
8 Combining Relations uOK to combine into one relation: 1.The relation for an entity-set E 2.The relations for many-one relationships of which E is the “many.” uExample: drivers(name, addr) and Favorite(driver, car) combine to make driver1(name, addr, favcar).
9 Risk with Many-Many Relationships uCombining drivers with Likes would be a mistake. It leads to redundancy, as: name addr car Sally 123 Maple Mustang Sally 123 Maple Maxima Redundancy
10 Handling Weak Entity Sets uRelation 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. uA supporting relationship is redundant and yields no relation (unless it has attributes).
11 Example LoginsHostsAt name Hosts(hostName, location) Logins(loginName, hostName, billTo) At(loginName, hostName, hostName2) Must be the same billTo At becomes part of Logins location
12 Subclasses: Three Approaches 1.Object-oriented : One relation per subset of subclasses, with all relevant attributes. 2.Use nulls : One relation; entities have NULL in attributes that don’t belong to them. 3.E/R style : One relation for each subclass: wKey attribute(s). wAttributes of that subclass.
13 Example cars Sports Car isa model manf RLR
14 Object-Oriented namemanf MustangNissan cars name manfRLR Corvette G.M. None Sports Good for queries like “find the RLR of Sports Car made by G.M..”
15 E/R Style namemanf Mustang Nissan Corvette G.M. Cars name RLR Corvette none Sports Cars Good for queries like “find all cars (including Sports Cars) made by G.M..”
16 Using Nulls namemanf RLR Mustang Nissan NULL Corvette G.M. none cars Saves space unless there are lots of attributes that are usually NULL.