START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model.

Slides:



Advertisements
Similar presentations
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Advertisements

Chapter 3 : Relational Model
Relational Algebra Ch. 7.4 – 7.6 John Ortiz. Lecture 4Relational Algebra2 Relational Query Languages  Query languages: allow manipulation and retrieval.
Relational Algebra 1 Chapter 5.1 V3.0 Napier University Dr Gordon Russell.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
Discussion #22 1/10 Discussion #22 Relational Data Model.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Basic (Flat) Relational Model.
Fall 2001Arthur Keller – CS 1803–1 Schedule Today Oct. 2 (T) Relational Model. u Read Sections Assignment 1 due. Personal Letter of Introduction.
Discussion #23 1/32 Discussion #23 Relational Algebra.
Discussion #15 1/16 Discussion #15 Interpretations.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
Relation Decomposition A, A, … A 12n Given a relation R with attributes Create two relations R1 and R2 with attributes B, B, … B 12m C, C, … C 12l Such.
Relational Model Session 6 Course Name: Database System Year : 2012.
1 The Relational Data Model, Relational Constraints, and The Relational Algebra.
Relational Data Model. A Brief History of Data Models  1950s file systems, punched cards  1960s hierarchical  IMS  1970s network  CODASYL, IDMS 
Relational Algebra Instructor: Mohamed Eltabakh 1.
THE RELATIONAL DATA MODEL CHAPTER 3 (6/E) CHAPTER 5 (5/E) 1.
CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An.
10/9/20151 The Relational Data Model TCU Database Systems Last update: September 2004 Reference: Elmasri 4 th edition, chapter 5.
DBSQL 3-1 Copyright © Genetic Computer School 2009 Chapter 3 Relational Database Model.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
Chapter 2 Adapted from Silberschatz, et al. CHECK SLIDE 16.
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – Relational Model.
1 The Relational Database Model. 2 Learning Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical.
1 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An = {(x1, x2, x3, …, xn): xi  Ai} R  A1  A2.
9/7/2012ISC329 Isabelle Bichindaritz1 The Relational Database Model.
1 Relational Algebra. 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of data from a database. v Relational model supports.
Relational Data Model Ch. 7.1 – 7.3 John Ortiz Lecture 3Relational Data Model2 Why Study Relational Model?  Most widely used model.  Vendors: IBM,
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com ICOM 5016 – Introduction.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
1.1 CAS CS 460/660 Introduction to Database Systems Relational Algebra.
ICS 321 Fall 2011 The Relational Model of Data (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 8/29/20111Lipyeow.
Discrete Mathematics Relation.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Intro to Relational.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Relational model.
Chapter 2 Introduction to Relational Model. Example of a Relation attributes (or columns) tuples (or rows) Introduction to Relational Model 2.
Chapter 2: Intro to Relational Model. 2.2 Example of a Relation attributes (or columns) tuples (or rows)
1 CS 430 Database Theory Winter 2005 Lecture 4: Relational Model.
CSE314 Database Systems Lecture 3 The Relational Data Model and Relational Database Constraints Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Sets & Set Operations Tuples & Relations. Sets Sets are collections –The things in the collection are called elements or members –Sets have no duplicates.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
CMPT 258 Database Systems Relational Algebra (Chapter 4)
N-ary Relations & Their Applications. 2 n-ary Relations Let A 1, A 2, …, A n be sets. An n-ary relation on these sets is a subset of A 1 x A 2 x … x A.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Intro to Relational.
CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An.
Chapter 3 The Relational Model. Objectives u Terminology of relational model. u How tables are used to represent data. u Connection between mathematical.
Relational Algebra. What is an algebra? –a pair: (set of values, set of operations) –  ADT  type  Class  Object e.g., stack: (set of all stacks, {pop,
1 The Relational Data Model David J. Stucki. Relational Model Concepts 2 Fundamental concept: the relation  The Relational Model represents an entire.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 6 th Edition Chapter 8: Relational Algebra.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
Module 2: Intro to Relational Model
Relational Data Model.
Relational Algebra Chapter 4 1.
Chapter 6: Formal Relational Query Languages
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 6: Formal Relational Query Languages
Instructor: Mohamed Eltabakh
Relational Algebra Chapter 4, Sections 4.1 – 4.2
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 6: Formal Relational Query Languages
Example of a Relation attributes (or columns) tuples (or rows)
Chapter 2: Intro to Relational Model
Unit Relational Algebra 1
Presentation transcript:

START OF DAY 8 Reading: Chap. 2 & 9

Relational Data Model

Relational Database A persistent collection of relations Information about various kinds of objects (persons, places, things, events) Each relation holds information about various kinds of objects (persons, places, things, events, etc.) Each relation, or table, is characterized by a set of attributes or properties from a domain e.g.,person(SSN, Name, Address, Phone) Each individual object, or record, is a tuple of values e.g.,( , Pat Carter, 12 Main, ) Set of records make up the relation, i.e., subset of the cross- product of the attributes’ domains Assume DB exists – in reality, create tables, insert tuples, etc. DBMS – manage storage, integrity/security, crash recovery, etc.

StudentIDNameAddressPhone 12345C. Brown12 Apple St L. Van Pelt34 Pear Ave P. Patty56 Grape Blvd Snoopy12 Apple St CourseStudentIDGrade CS A CS B EE C EE B+ EE B CS A- PH C+ CourseDayHour CS101M9AM CS101W9AM CS101F9AM EE200Tu10AM EE200W1PM EE200Th10AM PH100Tu11AM CoursePrerequisite CS101CS100 EE200EE005 EE200CS100 CS120CS101 CS121CS120 CS205CS101 CS206CS121 CS206CS205 CourseRoom CS101Turing Aud. EE20025 Ohm Hall PH100Newton Lab. Relational Database Example snapcr cdhcsg cp

Relational Schemas Each table has a schema Name Set of attributes Domain for each attribute Example: Names: snap, cp, cdh, cr, csg Attributes:table headers Domains:studentID: integer all the rest are strings, but we could be more specific (e.g. time, day, grade)

Relational Tables Tables consist of n-tuples, where n is the arity or degree of the relation (i.e., the number of attributes) Each n-tuple t  D 1  D 2  …  D n, where the D i ’s are the domains e.g., a 3-tuple t of cdh is an element string  string  string or string  day  time or course  day  time depending on how specific we make our domains A table is a set of tuples, all with the same schema e.g., cdh  D course  D day  D hour

Tables & Keys Because a table is a set of tuples, there are no duplicates There is always a set of attributes whose values uniquely identify a tuple (even if it is all of them) A set of attributes whose values always uniquely identify a tuple constitutes a key Typically, one or two attributes make up a key Keys must be declared: we cannot assume uniqueness e.g., Name is not a key; there could be another C. Brown Some systems add a tuple identifier as the key

Keys  Examples TableKey snapStudentID Name, Address, Phone (possible key?) cpCourse Prerequisite cdhCourse Day Hour Can a course meet twice on the same day? If not:Course Day crCourse Room Does a course always meet in the same room? If so:Course csgCourse StudentID

Predicates and Tuples A table name for tuples of arity n is an n-place predicate cdh('CS101','M','9AM') Asserts that CS101 meets on Monday at 9:00 am Predicates give each tuple a meaning in the ordinary sense of predicates The subset of D 1  D 2  …  D n present in the database are those assigned T; all others are assigned F (CWA) Interpretation: Domain For each predicate and every substitution, T or F Every relation is a set, and every set is a predicate. Hence every relation is a predicate (and vice-versa)

Database Tuples Database tuples (strictly speaking) are not true subsets of D 1  D 2  …  D n because we can alter the column order if we do so “correctly” More properly defined, a tuple in a relation is a set of attribute-value pairs e.g. {(Course, 'CS101'), (Day, 'M'), (Hour, '9AM') } = {(Day, ' M ' ), (Course, ' CS101 ' ), (Hour, ' 9AM ' ) } Normally, we factor out the attribute and fix the order Implication: we can interchange columns cr = Course Room =RoomCourse CS101 Turing Aud.Turing Aud.CS101 EE Ohm Hall25 Ohm HallEE200 PH100 Newton Lab.Newton Lab.PH100

Relational Algebra

What is an algebra? a pair: (set of values, set of operations)  ADT  type  Class  Object e.g., stack: (set of all stacks, {pop, push, top, …}) integer: (set of all integers, {+, -, *,  }) What is relational algebra? (set of relations, set of relational operators) Operators: , , , , , , , |  |

Relational Algebra is Closed Closed: all operations produce values in the value set (reals, {+, *,  })  closed (reals, {+, *, ,  })  not closed (divide by 0) (reals, {+, *, >})  not closed (T/F not in value set) (computer reals, {+, *,  })  not closed (overflow, roundoff) (relations, relational operators)  closed Implication: we can always nest (or compose) relational operators; can’t for algebras that are not closed.

Set Operations: , , and  Relations are sets; thus set operations work. Examples: R = A B S = A B R  S = A B R  S = A B R  S = A B 1 2 S  R = A B

Set Operations (cont’d) Definition: schema(R) = {A, B} = AB, i.e. the set of attributes We sometimes write R(AB) to mean the relation R with schema AB. Definition: union compatible schema(R) = schema(S) required precondition for , ,  Definitions: R  S = { t | t  R  t  S} R  S = { t | t  R  t  S} R  S = { t | t  R  t  S}

Tuple Restriction: [X] Restriction is a tuple operator (not a relational operator). t[X] restricts tuple t to the attributes in X. A B C t = t[ A ] = (1) t[ AC ] = (1,3)

Renaming:   A  B R renames attribute A to be B in R A must be in schema(R) B must not be in schema(R) Example: let  C  B Q = A B R  C  B Q = A B R = A B Q = A C But with  : R  Q = ? Not union compatible

Renaming (cont’d) Q =  A  B R renames attribute A to B; the result is Q. Precondition: A  schema(R) B  schema(R) Postcondition: schema(Q) = (schema(R)  {A})  {B} Q = {t' |  t (t  R  t' = (t – {(A, t[A])})  {(B, t[A])})} R = {{(A,1), (C,2)} {(A,2), (C,2)}} Q =  A  B R = {{(B,1), (C,2)} {(B,2), (C,2)}}

Selection:  The selection operation selects the tuples that satisfy a condition.  A=1 R = A B 1 2  B=2 R = A B  A=2  B  2 R = A B  A=3 R = A B Note: empty, but still retains the schema  P R = { t | t  R  P(t) } Precondition: each attribute mentioned in P must be in schema(R). Postcondition:  P R = { t | t  R  P(t) } schema(  P R) = schema(R) Meaning: apply predicate P to tuple t by substituting into P appropriate t values. R = A B

Projection:  The projection operation restricts tuples in a relation to those designated in the operation. R = A B Q = A B C  A R = A 1 2  B R = B 2 3  BC Q = B C  AB R = R =  A,B R =  {A,B} R Precondition: X  schema(R) Postcondition:  X R = { t' |  t (t  R  t' = t[ X ]) } schema(  X R) = X

Practice Exercises PE1 Using the following database, compute:  Trail Name Characteristics  Activity2  Description=Waterfall Recreational features  Feature=Vista Characteristics  Trails Name=North Fork and Forest-ID=2 Trails

Cross Product:  Standard cartesian product adapted for relational algebra R = A B S = C D R  S = A B C D

Cross Product (cont’d) R = A B 1 2 = t' 2 2 S = C D = t'' Precondition: schema(R)  schema(S) =  Postcondition: R  S = { t |  t'  t''(t'  R  t''  S  t = t'  t'')} schema(R  S) = schema(R)  schema(S) t' = { (A,1), (B,2) } t'' = { (C,3), (D,3) } t'  t'' = { (A,1), (B,2), (C,3), (D,3) }

Cross Product (cont’d) R = A B S = C A What if R and S have the same attribute, e.g. A? Can’t do cross product Solution: Rename  A  A S R   A  A S = A B C A

Natural Join: |  | R = A B S = B C R |  | S = A B C (R  ) Cross Product A B R |  | S =  ABC Projection  B=B' Selection Renaming  B  B' S B' C

Natural Join (cont’d) In general, we can equate 0, 1, 2, or more attributes using |  |. A join is defined as: schema ( R |  | S ) = schema( R )  schema( S ) R |  | S = {t | t[schema( R) ]  R  t[schema( S) ]  S} There are no preconditions  join always works.

Natural Join (cont’d) R = A B S = C D R |  | S = A B C D R = A B S = B C R |  | S = A B C R = A B C S = A B D R |  | S = A B C D attributes in common (full cross product) 1 attribute in common 2 attributes in common

Practice Exercises PE1 Using the following database, compute:  Department COURSE|x|  Semester SECTION  Department=Math COURSE |x|  Instructor=Anderson SECTION  Credit_hours=4 COURSE|x|  Year=05 SECTION

END OF DAY 8 Homework: #12