main concepts of relational model the relational model was proposed in 1970 by edgar codd the...

31
LECTURE 2 Main concepts of relational model

Upload: jadon-wilding

Post on 14-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

  • Slide 1

Slide 2 Main concepts of relational model Slide 3 The relational model was proposed in 1970 by Edgar Codd The relational model assumes that data is stored in two-dimensional tables called relations As an example, information about company cars (vehicle identification number, number plate, mark and year) can be stored in the following relation: VINNUMBER_PLATEMARKYEAR GTRE545WRTH256452EPA60PLFord Fusion I2003 THER186ACVG636853EL432PLOpel Corsa II2005 WDH144TETU063632EZG42PLCitroen C3 III2011 Slide 4 Definition (Relation schema): Let R be a finite set of elements A 1,A 2,...,A n called attributes. Each A i (i=1..n) is assigned to the set of values D(A i ) called the domain of A i. The set R is called a relation schema. Example 1 (Relation schema): STUDENT:={INDEX_NO, FIRST_NAME, LAST_NAME, YEAR, FACULTY} D(INDEX_NO):= {927502, 138571, 714072, 841053, 595723, 965024}, D(FIRST_NAME):= {David, Agnes, Andrew, Charles, Eva, Clara}, D(LAST_NAME):= {Holl, Lewis, Harris, Klark, Barker, Morgan}, D(YEAR):= {1, 2, 3}, D(FACULTY):= {Mathematics, Computer Science, Management, Law, Economics, Physics, Chemistry, Biology}. Slide 5 Definition (Tuple): Consider the relation schema R:={A 1,A 2,...,A n } and the set D(R) := D(A 1 ) D(A 2 ) D(A n ). Let f: R D(R) be a function such that f(A i ) D(A i ) for each i=1..n. The function f is called a tuple. Tuples defined on R will be denoted by r, r 1, r 2, s, t,.... Table representation of tuples Let r be a tuple defined on R={A 1,A 2,...,A n }. r is a function so it can be expressed as r = {(A 1,r(A 1 )), (A 2,r(A 2 )),...,(A n,r(A n ))} where r(A i ) D(A i ), i=1,...,n. This notation implies the following table representation of r: A1A1 A2A2... AnAn r(A 1 )r(A 2 )... r(A n ) Slide 6 Definition (Relation): Let T(R) denotes the set of all tuples defined on the set R. A finite subset of the set T(R) is a called a relation with the schema R. This subset is also called an instance of the schema R. Relations with the schema R (instances of R) will be denoted by I, J, K,.... Table representation of relations Let I be a relation with the schema R={A 1,A 2,...,A n }. According to the definition I:={r 1, r 2,...,r m }, where r i : R D(R), r i (A j ) D(A j ), i=1,...,m, j=1,...,n. Since each r i function has the same set of arguments, therefore relation I can be described by the following table: A1A1 A2A2... AnAn r 1 (A 1 )r 1 (A 2 )... r 1 (A n ) r 2 (A 1 )r 2 (A 2 )... r 2 (A n )... r m (A 1 )r m (A 2 )... r m (A n ) Slide 7 Example 1 (Relation) I(STUDENT):={ {138571, Eva, Klark, 2, Law}, {927502, David, Holl, 1, Biology}, {714072, Andrew, Lewis, 3, Computer Science}, {965024, Charles, Barker, 1, Mathematics} } is a relation with the STUDENT schema (I(STUDENT) is an instance of the STUDENT schema). Slide 12 Example 2 Consider the following instances I and J of PHONE:={PHONE_ID, NUMBER, MODEL} : R = {A 1,A 2,,A 3 } (R = PHONE, A 1 = PHONE_ID A 2 = NUMBER, A 3 = MODEL) D(A 1 )=D(PHONE_ID)={6,3,9,11}, D(A 2 )=D(NUMBER)={426235020, 491503592, 525246252, 426548716}, D(A 3 )=D(MODEL)={Siemens Gigaset W341, Sony Xperia WE, Nokia Lumia 329, Panasonic E425}, r 1 := {(A 1,r(A 1 )), (A 2,r(A 2 )),(A 3,r(A 3 ))}, r 1 := {(PHONE_ID,6), (NUMBER, 426235020),(MODEL, Siemens Gigaset W341)}, r 2 := {(A 1,r(A 1 )), (A 2,r(A 2 )),(A 3,r(A 3 ))}, r 2 := {(PHONE_ID,3), (NUMBER, 491503592),(MODEL, Sony Xperia WE)}, r 3 := {(A 1,r(A 1 )), (A 2,r(A 2 )),(A 3,r(A 3 ))}, r 3 := {(PHONE_ID,9), (NUMBER, 525246252),(MODEL, Nokia Lumia 329)}, r 4 := {(A 1,r(A 1 )), (A 2,r(A 2 )),(A 3,r(A 3 ))}, r 4 := {(PHONE_ID,11), (NUMBER, 426548716),(MODEL, Panasonic E425)}, I:={r 1,r 2,r 3 }, I(PHONE)={6, 426235020, Siemens Gigaset W341}, {3, 491503592, Sony Xperia WE}, {9, 525246252, Nokia Lumia 329}, {11, 426548716, Panasonic E425 } Slide 13 I(PHONE) J(PHONE) I(PHONE) J(PHONE) I(R) J(R) := {rT(R); rI(R) rJ(R)} PHONE_IDNUMBERMODEL 6426235020Siemens Gigaset W341 3491503592Sony Xperia WE 9525246252Nokia Lumia 329 11426548716Panasonic E425 PHONE_IDNUMBERMODEL 1506923054Nokia Lumia 6556 3491503592Sony Xperia WE 4543012943Samsung Galaxy A45 PHONE_IDNUMBERMODEL 1506923054Nokia Lumia 6556 3491503592Sony Xperia WE 4543012943Samsung Galaxy A45 6426235020Siemens Gigaset W341 9525246252Nokia Lumia 329 11426548716Panasonic E425 Slide 14 I(PHONE) J(PHONE) I(PHONE) J(PHONE) I(R) J(R) := {r T(R); r I(R r J(R)} PHONE_IDNUMBERMODEL 6426235020Siemens Gigaset W341 3491503592Sony Xperia WE 9525246252Nokia Lumia 329 11426548716Panasonic E425 PHONE_IDNUMBERMODEL 1506923054Nokia Lumia 6556 3491503592Sony Xperia WE 4543012943Samsung Galaxy A45 PHONE_IDNUMBERMODEL 3491503592Sony Xperia WE Slide 15 I(PHONE) J(PHONE) I(PHONE) \ J(PHONE) I(R)\J(R) := {r T(R); r I(R r J(R)} PHONE_IDNUMBERMODEL 6426235020Siemens Gigaset W341 3491503592Sony Xperia WE 9525246252Nokia Lumia 329 11426548716Panasonic E425 PHONE_IDNUMBERMODEL 1506923054Nokia Lumia 6556 3491503592Sony Xperia WE 4543012943Samsung Galaxy A45 PHONE_IDNUMBERMODEL 6426235020Siemens Gigaset W341 9525246252Nokia Lumia 329 11426548716Panasonic E425 Slide 16 The projection operator chooses a set of specified attributes from a given relation. Let R={A 1,A 2,...,A n } be relation schema and let P {A 1,A 2,...,A n }. Definition 2.4 (Tuple restriction) : A tuple s T(P) is called a restriction of r T(R) to P if and only if s(A i )=r(A i ) for each A i P. The tuple restriction is denoted by r[P]. Slide 18 Definition (Projection) Given a relation I(R) and the set P R, the set P (I(R)) := {r T(P); (s I(R)) (r = s[P])} is called the projection of I(R) on P. Example 1 (cont. Projection) Let STUDENT:={INDEX_NO, FIRST_NAME, LAST_NAME, YEAR, FACULTY} be relation schema and let I(STUDENT):={ {138571, Eva, Klark, 2, Law}, {927502, David, Holl, 1, Biology}, {714072, Andrew, Lewis, 3, Computer Science}, {965024, Charles, Barker, 1, Mathematics} } be a realtion, next let P {INDEX_NO, LAST_NAME, FACULTY}, then P ( STUDENT) ={{138571, Klark, Law},{927502, Holl, Biology}, {714072, Lewis, Computer Science}, {965024, Barker, Mathematics}} Slide 19 INDEX_NOFIRST_NAMELAST_NAMEYEARFACULTY 138571EvaKlark 2 Law 927502DavidHoll 1 Biology 714072AndrewLewis 3 Computer Science 965024CharlesBarker 1 Mathematics INDEX_NOLAST_NAMEFACULTY 138571KlarkLaw 927502HollBiology 714072LewisComputer Science 965024BarkerMathematics Slide 20 The selection operator is used to produce a horizontal subset of a given relation by selecting only the tuples which meet the specified selection condition. Definition (Selection): Let I(R) be an instance of relation schema R={A 1,A 2,...,A n }, SC a selection condition (a boolean expression involving terms connected by logical connectives). The set SC (I(R)) := {r I(R); SC(r)=true} is called selection of I(R) on the basis of the SC condition. Slide 21 Consider the following instance I(R) of R={A 1, A 2, A 3, A 4 }. I(R) The following instance is the selection of I(R) on the basis of the condition SC: (A 1 >A 2 and A 3 =A 4 ) {A1>A2 and A3=A4} (I(R)) A1A1 A2A2 A3A3 A4A4 52ax 119bb 63cy 2017dd A1A1 A2A2 A3A3 A4A4 119bb 2017dd Slide 22 The join operator is applied to combine tuples from two or more relations. Definition (Joins): Let R and S be relation schemas such that [R S . The join of two relations I(R) and J(S) is defined as the ralation I(R) BC J(S):= {r T(R S); r[R] I(R) r[S] J(S)} If R=S then I(R) ) BC J(S) is equivalent to I(R) J(S). Slide 23 Example (Join) Consider the following relation instances I(R) and J(S) of the schemas R={A,B,C,D,E} and S={A,B,F,G,H}, respectively. I(R) J(S) I(R) BC J(S) ABCDE 9hbxh 5oeko 12rimr 17tddt ABCDEFG 9hBxhnf tddtds ABCDEFG 9hbxhnf tddtds Slide 24 A functional dependency defines a relationship between attributes of a given relation schema. Definition (Functional dependency) Let R={A 1, A 2,...,A n } be a relation schema and let X and Y be any subsets of R. We say that an attribute Y is functionally dependent on attribute X (X Y) if and only if (r, s I(R)) (r[X] = s[X] r[Y] = s[Y]). for any instance I(R) of the R schema. In particular, an instance J(R) of R satisfies a functional dependency X Y (X,Y R) if for any two tuples t and u of J(R), t[X] = u[X] t[Y] = u[Y]. Assume that a set of attributes X 1, X 2,...,X n functionally determines several attributes Y 1, Y 2,...,Y m : X 1 X 2...X n Y 1 X 1 X 2...X n Y 2 X 1 X 2...X n Y m The above dependencies will be written as X 1 X 2...X n Y 1 Y 2...Y m.