Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spring 2003 ECE569 Lecture 02.1 ECE 569 Database System Engineering Spring 2003 Yanyong Zhang www.ece.rutgers.edu/~yyzhangwww.ece.rutgers.edu/~yyzhang.

Similar presentations


Presentation on theme: "Spring 2003 ECE569 Lecture 02.1 ECE 569 Database System Engineering Spring 2003 Yanyong Zhang www.ece.rutgers.edu/~yyzhangwww.ece.rutgers.edu/~yyzhang."— Presentation transcript:

1 Spring 2003 ECE569 Lecture 02.1 ECE 569 Database System Engineering Spring 2003 Yanyong Zhang www.ece.rutgers.edu/~yyzhangwww.ece.rutgers.edu/~yyzhang Course URL www.ece.rutgers.edu/~yyzhang/spring03www.ece.rutgers.edu/~yyzhang/spring03

2 Spring 2003 ECE569 Lecture 02.2 Warm up discussion  Compare and contrast database and file system l Similarity: the ability to manage persistent data l Difference: efficiency -DBMS provides index, navigation among relations, easier data access via query language (data manipulation language), transaction management,

3 Spring 2003 ECE569 Lecture 02.3 Today’s topic  Relational data model l Different data models exist: relational, network, hierarchical, object-oriented l Powerful, simple, declarative  Relational algebra

4 Spring 2003 ECE569 Lecture 02.4 Set-theoretic notion of a relation  A domain D is a set of values l colors = {red, blue, green} l age = set of positive integers less than 20 l last_name = {a-zA-Z} +  A relation over domains D1,D2,…,Dn is a subset of D1  D2  D3…  Dn.  Example R(colors, age)  {(red,1), (blue,1),(green,1), (red,2), (blue,2),(green,2), …}

5 Spring 2003 ECE569 Lecture 02.5 Set-theoretic notion of a relation (cont)  We are only interested in finite relations.  The members of a relation are called tuples.  Each relation that is a subset of the product of k domains is k-degree.  A relation can be represented as a table, where each row is a tuple and each column corresponds to one domain. PERSON (NAME, AGE) NameAge Zhang27 Merril2 Stewart40

6 Spring 2003 ECE569 Lecture 02.6 Set-theoretic notion of a relation (cont)  Because a relation is a set of tuples, the order of tuples in the table is insignificant (but the order of domains does matter). NameAge Zhang27 Merril2 Stewart40 NameAge Zhang27 Stewart40 Merril2  AgeName 27Zhang 2Merill 40Stewart 

7 Spring 2003 ECE569 Lecture 02.7 An alternative definition – set of mappings  A relation schema R = { A 1,A 2,…,A n } (no order imposed on attributes)  A relation instance of R is a finite set of mappings {  1,  2, …,  m } where  2 : R  D where D = null  dom(A 1 )  dom(A 2 )…  dom(A n ).  Each tuple i maps each attribute in tuple i to a value.  Note that attributes must be given names that are unique in a relation schema.  Under this definition, all three instances above are equivalent.

8 Spring 2003 ECE569 Lecture 02.8 Keys  A set S of attributes is a candidate key for R if l No semantically correct instance of R can include two tuples   such that  [s] = [s], and l No proper subset of S satisfies (a)  A set of attributes satisfying (a) but not necessarily (b) is a superkey.  A relation may have multiple candidate keys. One can be designated as the primary key.

9 Spring 2003 ECE569 Lecture 02.9 Example  Schema for a relation of degree four patient(name,address,balance_due,room#)  An instance of patient with three tuples that satisfies the key constraint (name is a candidate key) patientNameAddressBalance_dueRoom # Zhang1964 highland dr$5,230518 Stewart2342 K St.$25203 Brinkley2342 K St.$10,200203  No other candidate key is possible (assuming that this is a semantically correct instance of patient).

10 Spring 2003 ECE569 Lecture 02.10 Entity-Relationship model  To describe the conceptual scheme.  An entity is a thing that exists and is distinguishable.  Entity sets consist of a group of all “similar” entities.  Each entity has certain attributes.  A relationship among entity sets is an ordered list of entity sets.

11 Spring 2003 ECE569 Lecture 02.11 Example of entity-relationship model Billed AccountMade To Room# Name Address Balance Patient Payment Amount Date Diagnosed DiseaseNameTreatment From Vital Sign Pulse Blood Time

12 Spring 2003 ECE569 Lecture 02.12 Example – Healthcare DB patient(name, address, balance_due, room#) payments(name, amount, date) vital_signs(name, pulse, blood, date, time) diagnosis(patient_name, disease_name) disease(disease_name, treatment) treats(patient, doctor)

13 Spring 2003 ECE569 Lecture 02.13 Representing entity-relationship diagrams  An entity set E can be represented by a relation whose relation scheme consists of all the attributes of the entity set.  Each tuple of the relation represents one entity in the current instance of E.  A relationship R among E 1, E 2, …, E k is represented by a relation whose relation scheme consists of the attributes in the keys for each of E 1, E 2, …, E k.

14 Spring 2003 ECE569 Lecture 02.14 Relational Algebra  A set of five basic operations that map one or more relations to a new relation.  Union: R  S   t  t  R or t  S  l The set of tuples in R or S or both l The degrees of R and S must match ABC abc adc eab DE de bc FGH deb abc abb dbb R S T abc adc eab deb abb dbb R  T

15 Spring 2003 ECE569 Lecture 02.15 Relational Algebra (cont)  Difference: R - S   t  t  R and t  S  l The set of tuples in R, but not in S l The degrees of R and S must match ABC abc adc eab DE de bc FGH deb abc abb dbb R S T adc eab R - T

16 Spring 2003 ECE569 Lecture 02.16 Relational Algebra (cont)  Cartesian Product: R X S   (a 1, a 2, …, a r+s )  (a 1, a 2, …, a r )  R and (a r+1, a r+2, …, a r+s )  S  l R x S is of degree r+s where r=degree(R) and s=degree(s) l a tuple t is in R x S if its first r components match those of a tuple in R and its last s components from a tuple in S ABC abc adc eab DE de bc FGH deb abc abb dbb R S T ABCDE abcde abcbc adcde adcbc eabde eabbc R x S R.AR.BR.CS.DS.E

17 Spring 2003 ECE569 Lecture 02.17 Relational Algebra (cont)  Projection:  i1, i2, …, im (R)   (a i1, a i2, …, a im )  (a 1, a 2, …, a n )  R  l  i1, i2, …, im (R) is of degree m l A tuple t in the result is obtained by removing and/or rearranging the attributes of a tuple of R ABC abc adc eab DE de bc FGH deb abc abb dbb R S T GF ed ba bd  2,1 (T)

18 Spring 2003 ECE569 Lecture 02.18 Relational Algebra (cont)  Selection:  F (R)   t  t  R and F(t)  l  F (R) is of the same degree as R l All tuples in R that satisfy predicate F are included in result l F is a formula involving -Constants or components of the tuple – component i is denoted $I -Comparison operators,, , ,  -Logical operators and, or, not FGH deb abc abb dbb T FGH deb abb dbb  $1=d or $2=$3 (T)

19 Spring 2003 ECE569 Lecture 02.19 Examples  Find the names of all patients with a balance of more than $10,000.  Find the names of all patients that have a pulse less than 50 or have been diagnosed with hypertension.

20 Spring 2003 ECE569 Lecture 02.20 Examples (cont)  Find all patients whose treatment includes the “application of leeches”  Print the names of all pairs of patients that occupy the same hospital room and have been diagnosed with the same disease.

21 Spring 2003 ECE569 Lecture 02.21 Examples (cont)  Print the name of the patient with the highest pulse recorded today l Find all patients that had pulse measured today l Find the set of all ‘losers’, i.e., those whose pulse is less than or equal to that of some other patient l Subtract (b) from (a) and project out patient name

22 Spring 2003 ECE569 Lecture 02.22 Examples (cont)  List all patients that suffer from at least one illness that no other patient suffers from

23 Spring 2003 ECE569 Lecture 02.23 Additional Operations  Intersection: R  S   t  t  R and t  S  l The set of tuples in both R and S l The degrees of R and S must match l R  S = R – (R – S) S R R – ( R – S) R – S

24 Spring 2003 ECE569 Lecture 02.24 Additional Operations (cont)  Theta-join: R i  j S   $i  $r+j (R  S) where r = degree(R) and   , , ,   l When  is ‘=‘ operation is called equijoin.  Find patient with highest pulse

25 Spring 2003 ECE569 Lecture 02.25 Additional Operations (cont)  Natural Join: R S l An equijoin for all attributes that R and S have in common. l The shared columns originating in S are projected out l R S   i1, i2, …, im  R.A1 = S. A1 and … and R.Ak = S.Ak (R x S) where i 1, i 2, … i m is the list of all attributes of R x S, in order, except the attributes S.A 1, …, S.A k. l Example: Names of patients being treated with leeches.

26 Spring 2003 ECE569 Lecture 02.26 Sample Queries  Consider the following set of relations frequents (drinker, bar) serves (bar, beer) likes (drinker, beer)  List the drinkers that frequent at least one bar that serves a beer they like  Construct the relation should_visit(drinker,bar) consisting of all tuples where bar b serves a beer that drinker d likes.

27 Spring 2003 ECE569 Lecture 02.27 Sample Queries (cont)  List the drinkers that frequent only bars that serve some beer they like. (Assume every drinker frequents at least one bar.)  Print the drinkers that frequent no bar that serves a beer that they like

28 Spring 2003 ECE569 Lecture 02.28 Questions on Healthcare DB  Retrieve all pairs of patients that shared a room at the same time  List all patients that finished paying their bill before one of their roomates began paying theirs.  Print the names of all pairs of patients that occupy the same hospital room and have been diagnosed with the same disease.


Download ppt "Spring 2003 ECE569 Lecture 02.1 ECE 569 Database System Engineering Spring 2003 Yanyong Zhang www.ece.rutgers.edu/~yyzhangwww.ece.rutgers.edu/~yyzhang."

Similar presentations


Ads by Google