Presentation is loading. Please wait.

Presentation is loading. Please wait.

RELATIONAL DATA MANIPULATION

Similar presentations


Presentation on theme: "RELATIONAL DATA MANIPULATION"— Presentation transcript:

1 RELATIONAL DATA MANIPULATION
Week 5a RELATIONAL DATA MANIPULATION

2 READINGS NOTE:for this module ...
anytime you see this fellow on a slide, you can link to a detailed example. Click on the button; to return, click on the button in the upper right corner of the example. READINGS Chapter 9

3 Four strategies for relational data manipulation
relational algebra relational calculus transform-oriented languages Query-by-Example (QBE)

4 RELATIONAL ALGEBRA NOTE: (We’ll return shortly to relational algebra)
UNION A + B DIFFERENCE A - B INTERSECTION PRODUCT A x B PROJECTION SELECTION NOTE: (We’ll return shortly to relational algebra) JOIN DIVISION A / B

5 RELATIONAL CALCULUS derived from predicate calculus the use of the range (tuple) variable
RANGE of PX is S RETRIEVE (PX.P#) where PX.P# = “542-56” RANGE of SP isX GET W (S.SNAME: X(X.S#=S.S#) X(X.P#=“P2”) NOTE: (We’ll not concern ourselves any further with relational calculus)

6 TRANSFORM ORIENTED LANGUAGES
QUERY PROCESSOR DBMS ENGINE USER DATABASE Query Statements DBMS Commands Data ONE SUCH EXAMPLE IS

7 SQL Structured Query Language NOTE: We’ll devote time to learning the
SELECT Course#,Section FROM COURSE WHERE Course# = “483” QUERY PROCEDURE: DO COURSELIST FOR QTR = “199601” NOTE: We’ll devote time to learning the fundamentals of SQL and if you use ACCESS you’ll use a derivative of SQL

8 QUERY BY EXAMPLE (QBE) NOTE: if you use ACCESS
Presents to the user a graphical display of the query -- the user fills in the example and the database constructs the appropriate query to accomplish it. NOTE: if you use ACCESS you’ll can experiment with QBE

9 APPLICATION PROGRAM INTERFACES EMBEDDED DML
DBMS Pre-compiler OBJECT PROGRAM PROGRAM/DML Language Compiler NOTE: sorry .. not in this course

10 FUNDAMENTAL OPERATIONS OF RELATIONAL ALGEBRA

11 UNION Formed by adding the tuples from one relation to
those of a second relation to produce a third relation.

12 For a union to occur, The relations must be union compatible:
the relations must have the same number of attributes the attributes in corresponding columns must come from the same domain

13 DIFFERENCE Formed from tuples that occur in one union compatible
relation but not another

14 INTERSECTION Formed from tuples that occur in both of two relations

15 PRODUCT Formed from the concatenation of every tuple of one relation
with every tuple of another relation

16 PROJECTION A specified set of attributes of a single relation;
duplicate tuples are removed.

17 SELECTION A specified set of tuples of a single relation

18 JOIN Formed from some combination of the product
selection and projection operators NOTE: the attributes over which the join is done must be drawn from the same domain

19 DIVISION The resulting relation R consists of a set of tuples such that for every specified value, a, in the “divisor” relation (A) that is paired with a given value, b, in the “dividend” relation (B) b appears in the resulting relation.

20 The formed relation can be either:
an equijoin (a join over an equality) a natural join (an equijoin with the duplicate attribute projected out)

21 SOME USEFUL EXTENSIONS
LEFT OUTER JOIN RIGHT OUTER JOIN FULL OUTER JOIN OUTER UNION aggregate functions recursive closure operations

22 LEFT OUTER JOIN A JOIN operation between A and B, but the resulting relation contains every tuple in the first (“leftmost”) relation, A: tuples in B that do not match are included in the result but non-matching attributes are padded with null values.

23 RIGHT OUTER JOIN A JOIN operation between A and B, but the resulting relation contains every tuple in the second (“rightmost”) relation, B: tuples in B that do not match are included in the result but non-matching attributes are padded with null values .

24 TWO ADDITIONAL OPERATIONS
FULL OUTER JOIN A JOIN operation between A and B, but the resulting relation contains every tuple in the both relations: tuples in either that do not match are included in the result but non-matching attributes are padded with null values OUTER UNION A UNION operation between A and B which are not union compatible but partially compatible (only some of its attributes are union compatible): attributes that are not union compatible are included in the result but attributes that have no values for these tuples are padded with null values.

25 Aggregate functions mathematical functions that can applied to collections of values from the database COUNT SUM AVERAGE MAXIMUM MINIMUM

26 Recursive Closure Operations
Operations that allow recursive operations on tuples of the same type

27 INFIELD UNION OUTFIELD starting infielders and
LN FN B T POS INFIELD Baker Julie Carr Betty Davis Jean Farris Amy Sands Mae Ubel Jane R L 3B SS 1B 2B C P OUTFIELD LN FN B T POS Jones Julie R R LF Pope Joan R R CF Frank Ali L L RF SCRATCH command INFIELD UNION OUTFIELD LN FN B T POS Baker Julie R R 3B Note: both relations must be union compatible; duplicates will be eliminated Carr Betty R R SS Davis Jean L L 1B Farris Amy L R 2B Sands Mae R R C LN = last name FN = first name B = bats T = throws POS = position Ubel Jane R R P Jones Julie Pope Joan Frank Ali R L LF C RF Make a relation of starting infielders and outfielders

28 LN FN B T POS INFIELD Julie Carr Betty Davis Jean Farris Amy Sands Mae
Baker Julie Carr Betty Davis Jean Farris Amy Sands Mae Ubel Jane R L 3B SS 1B 2B C P Baker Julie Farris Amy R L 3B 2B BACKUP-PITCHERS LN FN B T POS Keck Mari INF Yaro Sands Mae C SCRATCH command INFIELD INTERSECT BACKUP-PITCHERS LN = last name FN = first name B = bats T = throws POS = position What starting infielders are also backup pitchers? Baker Julie Farris Amy Sands Mae R L 3B 2B C LN FN B T POS

29 INFIELD MINUS BACKUP-PITCHERS
LN FN B T POS INFIELD Baker Julie Carr Betty Davis Jean Farris Amy Sands Mae Ubel Jane R L 3B SS 1B 2B C P Baker Julie Farris Amy R L 3B 2B BACKUP-PITCHERS LN FN B T POS Keck Mari INF Yaro Sands Mae C SCRATCH command INFIELD MINUS BACKUP-PITCHERS LN = last name FN = first name B = bats T = throws POS = position What starting infielders are NOT backup pitchers? LN FN B T POS Carr Betty R R SS Davis Jean L L 1B Ubel Jane R R P

30 Pair the team’s base coaches with every starting player.
LN FN B T POS INFIELD Baker Julie Carr Betty Davis Jean Farris Amy Sands Mae Ubel Jane R L 3B SS 1B 2B C P LNP FNP B T POS Baker Julie Carr Betty Davis Jean Farris Amy Sands Mae Ubel Jane R L 3B SS 1B 2B C P LNC FNC BASE YRS Rena Harris Jo First Third 2 Fahr 4 SCRATCH command INFIELD TIMES COACHES LN Rena Harris Jo COACHES FN BASE YRS First Third 2 4 Fahr LN = last name FN = first name B = bats T = throws POS = position BASE = base YRS = years Pair the team’s base coaches with every starting player.

31 PROJECT TEAM OVER B, WLK TEAM List the player’s walks
LNP FNP B T POS BA SO WLK B WLK .433 20 21 Baker Julie R R 3B R 21 Breen Marie R 3B .383 23 26 R R 26 Carr R SS .280 18 Betty R 24 R 24 Church Mary SS .305 22 23 R R R 23 24 SCRATCH command Davis Jean L L 1B .250 29 L 29 Dove Fran PROJECT TEAM OVER B, WLK L L 1B .260 26 34 L 34 Farris Amy L R 2B .340 23 31 L 31 Green Gina L R 2B .290 28 21 L 21 C .291 30 Sands Mae R R 18 R 18 Terry Racheal R R C .240 22 22 R 22 Ubel Jane R R P .230 16 22 L 26 Waters Kathy L L P .310 19 26 TEAM List the player’s walks along with the whether the player bats left handed or right handed. LNP = last name FNP = first name B = bats T = throws POS = position BA = batting avg SO = #strike outs WLK = #walks

32 List the players who have a batting average greater than .300.
LNP FNP B T POS Baker Julie Davis Jean Farris Amy Sands Mae Ubel Jane R L 3B SS 1B 2B C P Carr Betty BA SO WLK 20 .383 23 21 26 .433 18 .305 22 24 .280 .260 29 34 .250 .290 28 31 .340 30 .240 .291 16 .310 19 .230 Breen Church Dove Green Terry Waters Marie Mary Fran Gina Racheal Kathy List the players who have a batting average greater than .300. SCRATCH command SELECT TEAM WHERE BA > .300 LNP FNP B T POS Baker Julie Farris Amy R L 3B SS 2B P BA SO WLK 20 .383 23 21 26 .433 .305 22 31 .340 .310 19 Breen Church Waters Marie Mary Kathy TEAM LNP = last name FNP = first name B = bats T = throws POS = position BA = batting avg SO = #strike outs WLK = #walks

33 TEAM Note: an equijoin! NUMBER POS 3B SS 1B 2B C P NO 22 13 14 9 23 31
LF CF RF 43 47 44 LNP FNP B T POS BA SO BB LNP = lastname FNP = firstname B = bats T = throws POS = position BA = batting avg SO = #strike outs WLK = #walks NO = assigned uniform number 3B .433 20 21 Baker Julie R R Breen Marie R R 3B .383 23 26 Carr Betty R R SS .280 18 24 Church Mary SS .305 22 23 R R 24 Davis Jean L L 1B .250 29 Dove Fran L L 1B .260 26 34 Farris Amy L R 2B .340 23 31 List Mary Church’s team information and uniform number. Green Gina L R 2B .290 28 21 C .291 30 Sands Mae R R 18 SCRATCH command Terry Racheal R R C .240 22 22 JOIN TEAM AND NUMBER WHERE TEAM.POS = NUMBER.POS AND TEAM.LNP = “Church” Ubel Jane R R P .230 16 22 Waters Kathy L L P .310 19 26 TEAM LNP FNP B T POS BA SO BB POS NO Note: an equijoin! Church Mary R R SS .305 22 23 SS 13

34 players that throw both DIVIDE (PROJECT TEAM OVER T,POS)
LNP FNP B T POS Baker Julie Davis Jean Farris Amy Sands Mae Ubel Jane R L 3B SS 1B 2B C P Carr Betty BA SO BB 20 .383 23 21 26 .433 18 .305 22 24 .280 .260 29 34 .250 .290 28 31 .340 30 .240 .291 16 .310 19 .230 Breen Church Dove Green Terry Waters Marie Mary Fran Gina Racheal Kathy HANDEDNESS T POS T L R R 3B R SS L 1B R 2B 2 L 2B DIVIDE ( .. ) BY HANDEDNESS R C R P L P POS 2B P 1 (PROJECT TEAM OVER T,POS) Two steps here ... TEAM SCRATCH command What positions have players that throw both left and right handed? LNP = last name FNP = first name B = bats T = throws POS = position BA = batting avg SO = #strike outs WLK = #walks DIVIDE (PROJECT TEAM OVER T,POS) BY HANDEDNESS

35 LEFT OUTER JOIN LINEUP AND WHERE LINEUP.POS = INandOUTFIELD.POS
3B 1B 2B C P LUP 3 5 7 6 2 LINEUP LF CF RF 1 4 9 SS 8 POS 3B SS 1B 2B NAME Baker Carr Davis Green LF CF RF Jones Pope Frank INandOUTFIELD What positions in the lineup are filled by infield and outfield players? SCRATCH command LEFT OUTER JOIN LINEUP AND INandOUTFIELD WHERE LINEUP.POS = INandOUTFIELD.POS NAME = last name POS = position LUP = position in the batting lineuup

36 numbers are worn by the infield players? C 23 P 31 LF 43 CF 47 RF 44
NAME POS INFIELD NUMBER POS NO Baker 3B 3B 22 Carr SS SS 13 Davis 1B 14 1B 2B 9 Green 2B What uniform numbers are worn by the infield players? C 23 P 31 LF 43 CF 47 RF 44 SCRATCH command RIGHT OUTER JOIN INFIELD AND NUMBER WHERE INFIELD.POS = NUMBER.POS NAME = last name POS = position NO = uniform number


Download ppt "RELATIONAL DATA MANIPULATION"

Similar presentations


Ads by Google