The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing...
-
Upload
kathleen-alannah-pitts -
Category
Documents
-
view
213 -
download
0
Transcript of The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing...
![Page 1: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/1.jpg)
The Relational Model
Part III
![Page 2: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/2.jpg)
Remember:3 Aspects of the Model
• It concerns• 1) data objects
• storing it
• 2) data integrity• making sure it corresponds to reality
• 3) data manipulation• working with it
![Page 3: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/3.jpg)
Manipulating Data
The theory in the model
![Page 4: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/4.jpg)
Relational Algebra
• A set of operators • Take relations as operands• c.f. arithmetic operators
• 2 + 2 returns 4 • R1 op R2 returns R3
![Page 5: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/5.jpg)
Relational Closure
• The output of a relational operation is a relation
• Put simply, we always work with tables• The output of one operation can be the
input to the next • we are familiar with the concept in
arithmetic • 2 + (4 × 3) - (28/4)
• The great trick!
![Page 6: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/6.jpg)
Operators
• Any number could be defined • 8 originals
• 4 traditional set operations (modified) • union, intersection, difference, Cartesian
product
• 4 special relational operations• restrict, project, join and divide
• We will also look at 2 more • extend, summarize
![Page 7: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/7.jpg)
Type Compatibility
• Having the same set of attributes• with corresponding attributes defined on
the same domains
• Some operators require this• some do not
• adding apples and oranges
![Page 8: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/8.jpg)
The 4 set operators
1. Union 2. Intersection 3. Difference
• = Minus
4. (Cartesian) Product• = Times
![Page 9: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/9.jpg)
Union operator
• (Requires type compatibility)
• A UNION B • returns a relation with:
• the same heading as A or B
• the set of all tuples in A or B or both
![Page 10: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/10.jpg)
Union
Name Job Posting
Gordon Accountant London
George Salesman Washington
Vladimir Security Moscow
• Duplicates eliminated
Name Job Posting
Gordon Accountant London
George Salesman Washington
Name Job Posting
Gordon Accountant London
Vladimir Security MoscowUNION
RETURNS
![Page 11: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/11.jpg)
Intersection operator
• (Requires type compatibility)
• A INTERSECTION B • returns a relation with:
• the same heading as A or B
• the set of all tuples belonging to both A and B
![Page 12: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/12.jpg)
Intersection
Name Job Posting
Gordon Accountant London
Name Job Posting
Gordon Accountant London
George Salesman Washington
Name Job Posting
Gordon Accountant London
Vladimir Security Moscow
INTERSECTION
RETURNS
![Page 13: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/13.jpg)
Difference operator
• (Requires type compatibility)
• A DIFFERENCE B • returns a relation with:
• the same heading as A or B
• the set of all tuples belonging to A and not to B
![Page 14: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/14.jpg)
Difference
Name Job Posting
George Salesman Washington
• Directionality
Name Job Posting
Gordon Accountant London
George Salesman Washington
Name Job Posting
Gordon Accountant London
Vladimir Security Moscow
DIFFERENCE
RETURNS
![Page 15: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/15.jpg)
Product operator
• (Does not require type compatibility)
• A PRODUCT B• returns a relation with:
• a heading which is the union of the headings of A and B
• the set of tuples formed by coalescing all tuples from A with all tuples from B – all permutations
• Not typically of practical use • No extra information • Theoretical value
![Page 16: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/16.jpg)
Product
C
A
BPRODUCT RETURNS
N
1
2
3
C N
A 1
A 2
A 3
B 1
B 2
B 3
![Page 17: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/17.jpg)
Product operator - note
• If the headers have names in common • product would have duplicated attributes • not a well formed relation
• must rename one or both • R1 (a, b, c) Product R2 (c, d, e) • might be made to return
• R3 (a, b, c1, c2, d, e) or
• R3 (a, b, R1.c, R2.c, d, e)
![Page 18: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/18.jpg)
Operator Ordering
• Associative• Union, Intersection,
Product • but not Difference
• Commutative: • Union, Intersection,
Product • but not Difference
• Equivalent: • (A Union B) Union C • A Union (B Union C) • A Union B Union C
• Equivalent: • A Union B • B Union A
![Page 19: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/19.jpg)
The 4 relational operators
1. Restrict2. Project3. Join 4. Divide
![Page 20: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/20.jpg)
The Restrict Operation
• Based on:• one relation• scalar operator Θ• Θ could be
<, <=, =, <>, >=, > etc. • two attributes
• Often represented by the word where
• One attribute can be replaced by an expression
• Examples• A where X Θ Y• B where r > s• C where length < 42
• Selects tuples • Removes rows
![Page 21: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/21.jpg)
RESTRICT
Name Job Posting
George Salesman Washington
• people WHERE job = ‘Salesman’
Name Job Posting
Gordon Accountant London
George Salesman Washington
RETURNS
![Page 22: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/22.jpg)
Restrict Conditions (and/or)
• A where C1 and C2 ≡• (A where C1) INTERSECTION (A where C2)
• A where C1 or C2 ≡• (A where C1) UNION (A where C2)
• A where not C ≡• A DIFFERENCE (A where C)
• We can extend the WHERE clause with any arbitrary Boolean combination of comparisons • People WHERE height < 1.5 and age > 50
![Page 23: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/23.jpg)
Project
• Removes “columns” (attributes)• Written as:
• A [X, Y] • returns a relation with two named attributes
• Duplicate tuples eliminated • if the lost attributes distinguished them
• All attributes named - identity projection • No attributes named - nullary projection
![Page 24: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/24.jpg)
Join
• The output relation from A JOIN B has: • a heading consisting of:
• attributes found only in A • attributes found only in B • attributes found in both A and B (1 copy)
• tuples where values of identified attributes are the same in A and B
• Associative and commutative • Sometimes called the natural join
![Page 25: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/25.jpg)
JOIN
Weight Colour Length
Very heavy Red Very short
Very heavy Red Short
Heavy Red Very short
Heavy Red Short
Light Yellow Very long
Weight Colour
Very light Blue
Very heavy Red
Heavy Red
Light Yellow
Colour Length
Green Long
Red Very short
Red Short
Yellow Very long
JOIN
RETURNS
![Page 26: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/26.jpg)
Θ -Join
• Join is based on equality • Θ -join is based on any condition
• (A PRODUCT B) where X Θ Y
• if Θ is = we have an equijoin • X and Y attributes same in all tuples • eliminate one with projection -we have join
• Join is a projection of a restriction of a product• Crucial to understand and appreciate this
![Page 27: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/27.jpg)
The PRODUCT
Table3
weightcolour
verylight blue
veryheavy red
heavy red
light yellow
colour length
green long
red veryshort
red short
yellow verylong
weight Table3.colour Table4.colour length
verylight blue green long
veryheavy red green long
heavy red green long
light yellow green long
verylight blue red veryshort
veryheavy red red veryshort
heavy red red veryshort
light yellow red veryshort
verylight blue red short
veryheavy red red short
heavy red red short
light yellow red short
verylight blue yellow verylong
veryheavy red yellow verylong
heavy red yellow verylong
light yellow yellow verylong
PRODUCT How many tuples?
4 x 4 = 16
![Page 28: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/28.jpg)
Alphabetical Less Than Join
Table3
weight colour
verylight blue
veryheavy red
heavy red
light yellow
colour length
green long
red veryshort
red short
yellow verylong
weight Table3.colour Table4.colour length
verylight blue green long
verylight blue red veryshort
verylight blue red short
verylight blue yellow verylong
veryheavy red yellow verylong
heavy red yellow verylong
A < B
![Page 29: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/29.jpg)
Directional Joins
• a heading consisting of: • attributes found only in A • attributes found only in B • attributes found in both A and B (1 copy)
• all the tuples from one relation• only matching tuples from the other
• Left Join or Right Join• will result in blanks
![Page 30: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/30.jpg)
Left-Join
Table3
weight colour
verylight blue
veryheavy red
heavy red
light yellow
colour length
green long
red veryshort
red short
yellow verylong
Left Join
weight colour length
verylight blue
veryheavy red short
veryheavy red veryshort
heavy red short
heavy red veryshort
light yellow verylong
![Page 31: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/31.jpg)
Division
• given A{X, Y } and B{Y } • division returns a relation with
• heading X • tuples for which A has an {X, Y } for all Y in
B
• X and/or Y can be multiple attributes
![Page 32: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/32.jpg)
Division
Person
Jim
Person Sport
Jim Soccer
Paul Rugby
Mary Tennis
Paul Tennis
Mary Squash
Jim Tennis
Sally Soccer
Sport
Soccer
TennisDIVIDE
RETURNS
![Page 33: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/33.jpg)
2 additional operators
• Others have been proposed • and still are
• These 2 have widespread value and are illustrative• extend • summarize
![Page 34: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/34.jpg)
Extend
• Adds a new attribute calculated from one or more existing attributes
EXTEND relation ADD expression AS ATTRIBUTE
EXTEND item ADD (cost . 2.58) AS dollar
• the expression can involve constants, attributes and other relations
![Page 35: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/35.jpg)
Summarize
• Column-wise computations - grouping • c.f. row-wise in Extend
• e.g. SUMMARIZE R by A1 add sum A2 as Total
• Return a relation with • heading {A1 , Total} • a tuple for each distinct value of A1 in R
containing the total of A2 values over them
![Page 36: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/36.jpg)
Summarize - notes
• Can be “by” more than one attribute• projection plus one attribute
• Can be “by” no attribute • grand total (or other calculation)
![Page 37: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/37.jpg)
Relation assignment? • So far it has all been expressions
• need a syntax for storing the result • in named relations
• The existing heading and tuples in a relation will be “overwritten”
• e.g. • A = B UNION C • X = X UNION Y
• c.f. arithmetic • Not done like this
• Rarely store “answers”• We change tables
![Page 38: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/38.jpg)
Updating relations
• Could use assignment with destination relation in the expression • error conditions not then handled
• addition of duplicate tuple • deletion of non-existent tuple
• not efficient • not declarative
• Specific update operations handle this:• insert • update • delete
![Page 39: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/39.jpg)
Insert
• Source and target relations • must be type compatible
• All tuples of source inserted into target• set operation
• Source and target can be expressions
insert(A where x > 1 or y = 42) into B
![Page 40: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/40.jpg)
Update
• Change specified attribute values in specified tuples of a relation • expression to identify the restriction of a relation• assignments to set attributes
update (A where model = delux) colour = red trim = gold
• set of tuples changed• may be set of 1
![Page 41: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/41.jpg)
Delete
• Identified tuples from a relation• again, a set of tuples
DELETE A where length > 42
![Page 42: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/42.jpg)
What is the algebra for?
• Retrieval: as expected • Views: virtual relations (stored queries)• Update: what parts change • Security: define data under particular
authorisation control • Concurrency control: data to be
protected • Integrity rules: some parts of the data
which must obey certain rules
![Page 43: The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e845503460f94b85d95/html5/thumbnails/43.jpg)
Data Manipulation
End