Presentation is loading. Please wait.

Presentation is loading. Please wait.

© D. Wong 2003 1 Ch. 3 (part 1)  Relational Model basics  From E/R diagram to Relations.

Similar presentations


Presentation on theme: "© D. Wong 2003 1 Ch. 3 (part 1)  Relational Model basics  From E/R diagram to Relations."— Presentation transcript:

1 © D. Wong 2003 1 Ch. 3 (part 1)  Relational Model basics  From E/R diagram to Relations

2 © D. Wong 2003 2 Relational Model  Based on mathematically defined relations of entities  Consists of: –Attributes (fields) –Domain legitimate values of attributes (data range) –Views of data presented in table format –Create new views by projections of the database –Records are n-tuples, where n = # of attributes (arity)  Use relational algebra operations to compute data  Result of the operations are also relations

3 © D. Wong 2003 3Relations  Sets of tuples presented in a table (order of tuples immaterial)  Attributes are the column header  Schema of a relation: name and the set of attributes. E.g. Movies (title, year, length, filmType)  Database schema – the set of schemas for the relations in a design  Domain – elementary data type of each attribute  Relation instances titleyearlengthfilmType Star Wars 1977124color Mighty Ducks 1991104color Wayne’s World 199295color Movies

4 © D. Wong 2003 4 E/R Diagram to Relational Designs 1. Turn each entity set into a relation with the same set of attributes. E.g. Star (name, address) 2. Represent relationships by relations 3. Combining relations when appropriate 4. Handle weak entity sets 5. Convert subclass structures to relations

5 © D. Wong 2003 5 E/R Relationship to Relations Replace a relationship by a relation (say R): 1.For each entity set involved in relationship R, take its key attribute(s) as part of the schema of the relation for R. 2.If the relationship has attributes, add to R’s attribute set 3.If one entity set has multiple roles in a relationship, then its key attributes appear as many times as there are roles. Need to rename the attributes to avoid name duplication. e.g. Contracts (starName, title, year, studioOfStar, producingStudio) Ref. Fig. 3.6

6 © D. Wong 2003 6 Discovering Keys for Relations If relation R is constructed from:  Entity set – key attributes of the entity set  Relationship R, key of R is : –many-many : composed of keys of both connected entity sets –many-one from E1 to E2 : consisted of key of E1 –one-one between E1 and E2: either the key of E1 or E2

7 © D. Wong 2003 7 Combining Relations  In general, if 2 or more relations has exactly the same keys, combine them.  For many-one relationship: –E (ea1, ea2) –F (fa1, fa2, fa3) –R (ea1, fa2, ra1) -- key is from the many relation –Result: E(ea1, ea2, fa2, ra1) E F R

8 © D. Wong 2003 8 Handling weak entity set 1. The relation for the weak entity set includes it’s own attributes and the attritubes of the other entity sets that help form it’s key  e.g. 1: Contracts (starName, studioName, title, year, salary)  e.g. 2: Crew (number, studioName) 2. The relation for any relationship create as usual (rename attributes to avoid confusion) 3. Don’t need to create a relation for supporting relationships

9 © D. Wong 2003 9 Weak entity set handling example  Studios(name, addr)  Crews(number, studioName)  Unit-of(number, studioName, name) – but not needed because it’s a supporting relationship Unit-of Crew Studios number name addr

10 © D. Wong 2003 10 Converting Subclass Structures to Relations Strategies: 1. E/R approach 2. OO approach 3. Use null values

11 © D. Wong 2003 11 E/R approach  For each entity set E in the hierarchy, create a relation that includes the key attributes from the root and any attributes belonging to E  Do not create a relation for isa relationship  Example: –Movies(title, year, length, filmType) –MurderMysteris(title, year, weapon) –Cartoons(title, year)

12 © D. Wong 2003 12 OO approach  Treat entities as objects belonging to a single class – for each possible subtree including the root, create one relation, whose schema includes all the attributes of all the entity sets in the subtree  Example: –Movies(title, year, length, filmType) –MoviesC(title, year, length, filmType) –MoviesMM( title, year, length, filmType, weapon) –MoviesCMM(title, year, length, filmType, weapon) –Voices(title, year, starName)  Then combine relations that have the same set of attributes.

13 © D. Wong 2003 13 Use null values  Create one relation with all the attributes of all the entity sets in the hierarchy. Each entity is represented by one tuple, and that tuple has a null value for whatever attributes the entity does not have.  Example: –Movie(title, year, length, filmType, weapon)

14 © D. Wong 2003 14 Comparison of the 3 approaches  Pros and Cons in each approach. Factors to consider: –Number of relations involved in a query (depends on the query of interest) –Total number of relations in the schema –Space usage

15 © D. Wong 2003 15 Operations in the Relational Data Model  Algebraic notation (Relational Algebra)  sets  bags  Constraints on relations  Extensions to the relational model

16 © D. Wong 2003 16 Ch. 5 Relational Algebra  Relational Algebra expressions are composed of: –Operands - relations represented by:  relation’s name or  the list of tuples in a relation –Operators  Results of the operations are also relations  Build complex expressions from simple ones  Queries – relational algebra expressions

17 © D. Wong 2003 17 Classes of Relational Algebra Operations Classes of relational algebra operations: 1. Set operations: union, difference, intersection 2. Remove parts of a relation: –Selection – eliminates rows (tuples) –Projection – eliminates columns 3. Combine tuples of two relations : cartesian product and joins 4. Renaming: changes relation schema (i.e. relation name, and/or attribute names)

18 © D. Wong 2003 18 Example relations 1 ABC abc daf cbd R ABC bga daf S

19 © D. Wong 2003 19 Set Operations  Union, Difference, Intersection   Given relations R and S: – –R and S have schemas with identical set of attributes, and domain types for each attributes (=> same arity) – –Columns of R and S must be ordered so that the order of attributes is the same for both relations

20 © D. Wong 2003 20 Union  R  S –Result: a set of tuples that are in R or S or both. –A tuple appears only once in the result even if it is present in both R and S –Reminder: R and S has same arity  Example: ABC abc daf cbd bga

21 © D. Wong 2003 21 Difference  R – S –Result: set of tuples in R but not in S –Reminder: Not commutative: R – S ≠ S – R –Reminder: R and S has same arity  Example: ABC abc cbd

22 © D. Wong 2003 22 Intersection  R  S –Result: set of tuples in both R and S, i.e. R-(R-S) –Reminder: R and S has same arity  Example: ABC daf

23 © D. Wong 2003 23Projection    …, An ( R )  Result: –a new relation that has only the columns for attributes A 1, A 2, … A n of R –Schema of the new relation is {A 1, A 2, … A n }  Example 1:  A,  C ( R )  Example 2:  B ( R ) B b a ACac df cd

24 © D. Wong 2003 24 Selection   c ( R ), where C is a conditional expression: –operands are constants or attributes of R –Operators are :, , , , and, or, not  Result: –a new relation with a subset of R’s tuples that satisfy condition C –Schema is the same as R’s schema  Example:  B=b (R) ABC abc cbd

25 © D. Wong 2003 25 Cartesian Product  R x S  Let arity of R = k 1 and arity of S = k 2  Result: the set of all possible (k 1 + k 2 )-tuples whose first k 1 components form a tuple in R, and whose last k 2 components form a tuple in S  Example: R.AR.BR.CS.AS.BS.C abcbga abcdaf dafbga dafdaf cbdbga cbddaf

26 © D. Wong 2003 26 Example Relation 2 RABCSDE 12331 45662 789

27 © D. Wong 2003 27 Theta-Joins  R C S where C is an arbitrary condition  Result: tuples in R x S such that C is satisfied, i.e.  C (R x S)  To construct the result: 1.Take the product of R and S 2.Select from the product only those tuples that satisfy the condition C  Arity of result = arity of R + arity of S  Example: R C S where C is B < D

28 © D. Wong 2003 28 Example Relation 3 ABC abc dbc bbf cad BCD bcd bce adb R S

29 © D. Wong 2003 29 Natural Joins  R S  Result:  i1, i2, …, im  R.A1=S.A1  …  R.Ak=S.Ak (R x S) where i1, i2, …, im is the list of all components of R x S in order, except the components S.A 1, … S.A k  Example: ABCD abcd abce dbcd dbce cadb

30 © D. Wong 2003 30 Renaming   (T) or  (T)   S(A1, A2, …, An) (T) or  S (T)  Result: renames relation T to S  Example: R x  (S )  Example: R x  S(X, Y, Z) (S ) ABCXYZ abcbga abcdaf dafbga dafdaf cbdbga cbddaf


Download ppt "© D. Wong 2003 1 Ch. 3 (part 1)  Relational Model basics  From E/R diagram to Relations."

Similar presentations


Ads by Google