Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Relation sID sName Major 1025 Alice PHY 1026 Bob CSC 1028

Similar presentations


Presentation on theme: "A Relation sID sName Major 1025 Alice PHY 1026 Bob CSC 1028"— Presentation transcript:

1 A Relation sID sName . . . Major 1025 Alice PHY 1026 Bob CSC 1028
degree attributes sID sName . . . Major 1025 Alice PHY 1026 Bob CSC 1028 Carol 1029 Dan EGR tuples Diagram from B. Signer

2 Table 3.3 - Relational Database Keys

3 Figure 3.2 - An Example of a Simple Relational Database

4

5 沒什麼, ничего, waxba, কিছু না,
NULL is … nothing, nada, nichts, 沒什麼, ничего, waxba, কিছু না, rud ar bith, 무, नथिंग , 何も , chochote, không, dim byd `

6 Figure 3.3 - An Illustration of Integrity Rules

7 Codd’s Original RA Operators
Set Operators Relation Operators Union Selection Difference − Projection Intersection Join Cartesian Product × Division ÷ Others have been added since Codd … e.g., Rename `

8 Soccer Recruitment DB College (cName, state, enrollment) Player (pID, pName, yellowCards, Highschool) Tryout (pID, cName, pPosition, decision) College Player Tryout cName state enr LSU LA 18000 ASU AZ 12000 OSU OK 22000 FSU FL pID pName yCard HS 10001 Andy no 1200 20002 Blake 1600 30003 Chuck 600 40004 Dave yes 50005 Ed 300 60006 Ford 250 pID cName pPos dec 10001 LSU goalie no ASU mid yes 20002 FSU strike 30003 OSU 40004 50005 Leave this slide up while writing all the examples on the board Layout idea from J. Widom

9 Selection: 𝝈 𝒆𝒏𝒓>𝟏𝟐𝟎𝟎𝟎 𝑪𝒐𝒍𝒍𝒆𝒈𝒆 𝝈 𝒚𝑪𝒂𝒓𝒅=𝒏𝒐 𝑷𝒍𝒂𝒚𝒆𝒓
Colleges with more than 12,000 students: Players with no Yellow Cards: Players trying out for goalie at ASU: 𝝈 𝒆𝒏𝒓>𝟏𝟐𝟎𝟎𝟎 𝑪𝒐𝒍𝒍𝒆𝒈𝒆 𝝈 𝒚𝑪𝒂𝒓𝒅=𝒏𝒐 𝑷𝒍𝒂𝒚𝒆𝒓 𝝈 𝒑𝑷𝒐𝒔=𝒈𝒐𝒂𝒍𝒊𝒆 ∧ 𝒄𝑵𝒂𝒎𝒆=𝑨𝑺𝑼 𝑻𝒓𝒚𝒐𝒖𝒕 College Player Tryout cName state enr LSU LA 18000 ASU AZ 12000 OSU OK 22000 FSU FL pID pName yCard HS 10001 Andy no 1200 20002 Blake 1600 30003 Chuck 600 40004 Dave yes 50005 Ed 300 60006 Ford 250 pID cName pPos dec 10001 LSU goalie no ASU mid yes 20002 FSU strike 30003 OSU 40004 50005 Unary operator that yields a horizontal subset of a table College Tryout Layout idea from J. Widom

10 Figure Select

11 Projection: Players and Yellow cards only Eliminate Cname from Tryout
College Player Tryout Unary operator that yields a vertical subset of a table cName state enr LSU LA 18000 ASU AZ 12000 OSU OK 22000 FSU FL pID pName yCard HS 10001 Andy no 1200 20002 Blake 1600 30003 Chuck 600 40004 Dave yes 50005 Ed 300 60006 Ford 250 pID cName pPos dec 10001 LSU goalie no ASU mid yes 20002 FSU strike 30003 OSU 40004 50005 College Tryout `

12 Figure Project

13 Selection & Projection together
ID and Name of players with Yellow cards 𝒑𝑰𝑫, 𝒑𝑵𝒂𝒎𝒆 𝝈 𝒚𝑪𝒂𝒓𝒅=𝒚𝒆𝒔 𝑷𝒍𝒂𝒚𝒆𝒓 Names of Colleges that chose players and the player’s position: 𝒄𝑵𝒂𝒎𝒆, 𝒑𝑷𝒐𝒔 𝝈 𝒅𝒆𝒄=𝒚𝒆𝒔 𝑻𝒓𝒚𝒐𝒖𝒕 College Player Tryout cName state enr LSU LA 18000 ASU AZ 12000 OSU OK 22000 FSU FL pID pName yCard HS 10001 Andy no 1200 20002 Blake 1600 30003 Chuck 600 40004 Dave yes 50005 Ed 300 60006 Ford 250 pID cName pPos dec 10001 LSU goalie no ASU mid yes 20002 FSU strike 30003 OSU 40004 50005 College Tryout `

14 Cartesian Product: × × = . . . College Player cName state enr Akron OH
12000 Maryland MD 37000 UCLA CA 40675 N.Dame IN 8452 cName state enr pID pName yCard HS Akron OH 12000 1 Alice AHS 2 Bob DHS 3 Carol RHS 4 Dave UHS Maryland MD 37000 UCLA CA 40675 × = Player pID pName yCard HS 1 Alice AHS 2 Bob DHS 3 Carol RHS 4 Dave UHS Show this slide Yields all possible pairs of rows from two tables . . .

15 Figure Product

16 Union Compatible If two relations A and B have
For the Union, Difference, and Intersection operators the two relations must be Union Compatible: If two relations A and B have The same number of attributes, and Attribute Ai has a domain compatible with that of Bi Then A and B are said to be Union Compatible .

17 Difference: − MySQL ID’s of players who tried out for goalie at LSU and failed, but did not try out for goalie at A&M. 𝒑𝑰𝑫 𝝈 𝒄𝑵𝒂𝒎𝒆 = ′ 𝑳𝑺 𝑼 ′ ∧𝒑𝑷𝒐𝒔 = ′ 𝒈𝒐𝒂𝒍𝒊 𝒆 ′ ∧𝒅𝒆𝒄 = ′ 𝒏 𝒐 ′ 𝑻𝒓𝒚𝒐𝒖𝒕 − 𝒑𝑰𝑫 𝝈 𝒄𝑵𝒂𝒎𝒆 = ′ 𝑨& 𝑴 ′ ∧𝒑𝑷𝒐𝒔 = ′ 𝒈𝒐𝒂𝒍𝒊 𝒆 ′ 𝑻𝒓𝒚𝒐𝒖𝒕 College Player Tryout Yields all rows in one table that are not found in the other table NOTE: Not directly supported in MySQL. So do a MINUS b this way: SELECT * FROM a WHERE (x,y) NOT IN ( SELECT * FROM b) NOTE: Union-compatible: Tables share the same number of columns, and their corresponding columns share compatible domains cName state enr LSU LA 18000 ASU AZ 12000 OSU OK 22000 FSU FL pID pName yCard HS 10001 Andy no 1200 20002 Blake 1600 30003 Chuck 600 40004 Dave yes 50005 Ed 300 60006 Ford 250 pID cName pPos dec 10001 LSU goalie no ASU mid yes 20002 FSU strike 30003 OSU 40004 50005 College Tryout `

18 Difference example 2 𝑐𝑁𝑎𝑚𝑒 𝐶𝑜𝑙𝑙𝑒𝑔𝑒 − 𝑐𝑁𝑎𝑚𝑒 𝜎 𝑑𝑒𝑐 = ′ 𝑦𝑒 𝑠 ′ 𝑇𝑟𝑦𝑜𝑢𝑡
MySQL Colleges that did not have any successful player tryouts (including colleges that didn’t have any tryouts): 𝑐𝑁𝑎𝑚𝑒 𝐶𝑜𝑙𝑙𝑒𝑔𝑒 − 𝑐𝑁𝑎𝑚𝑒 𝜎 𝑑𝑒𝑐 = ′ 𝑦𝑒 𝑠 ′ 𝑇𝑟𝑦𝑜𝑢𝑡 College Player Tryout cName state enr LSU LA 18000 ASU AZ 12000 OSU OK 22000 FSU FL pID pName yCard HS 10001 Andy no 1200 20002 Blake 1600 30003 Chuck 600 40004 Dave yes 50005 Ed 300 60006 Ford 250 pID cName pPos dec 10001 LSU goalie no ASU mid yes 20002 FSU strike 30003 OSU 40004 50005 College Tryout `

19 Figure Difference

20 Union: ID’s of players who tried out for goalie at LSU and succeeded, or who tried out for goalie at ASU and failed 𝒑𝑰𝑫 𝝈 𝒄𝑵𝒂𝒎𝒆 = ′ 𝑳𝑺 𝑼 ′ ∧𝒑𝑷𝒐𝒔 = ′ 𝒈𝒐𝒂𝒍𝒊 𝒆 ′ ∧𝒅𝒆𝒄 = ′ 𝒚𝒆 𝒔 ′ 𝑻𝒓𝒚𝒐𝒖𝒕 ∪ 𝒑𝑰𝑫 𝝈 𝒄𝑵𝒂𝒎𝒆 = ′ 𝑨𝑺 𝑼 ′ ∧𝒑𝑷𝒐𝒔 = ′ 𝒈𝒐𝒂𝒍𝒊 𝒆 ′ ∧𝒅𝒆𝒄 = ′ 𝒏 𝒐 ′ 𝑻𝒓𝒚𝒐𝒖𝒕 College Player Tryout Combines all rows from two tables, excluding duplicate rows NOTE: Union-compatible: Tables share the same number of columns, and their corresponding columns share compatible domains cName state enr LSU LA 18000 ASU AZ 12000 OSU OK 22000 FSU FL pID pName yCard HS 10001 Andy no 1200 20002 Blake 1600 30003 Chuck 600 40004 Dave yes 50005 Ed 300 60006 Ford 250 pID cName pPos dec 10001 LSU goalie no ASU mid yes 20002 FSU strike 30003 OSU 40004 50005 College Tryout `

21 Figure Union

22 Intersect: MySQL ID’s of players who tried out for goalie at LSU and succeeded AND who tried out for goalie at A&M and failed 𝒑𝑰𝑫 𝝈 𝒄𝑵𝒂𝒎𝒆 = ′ 𝑳𝑺 𝑼 ′ ∧ 𝒑𝑷𝒐𝒔 = ′ 𝒈𝒐𝒂𝒍𝒊 𝒆 ′ ∧ 𝒅𝒆𝒄 = ′ 𝒚𝒆 𝒔 ′ 𝑻𝒓𝒚𝒐𝒖𝒕 𝒑𝑰𝑫 𝝈 𝒄𝑵𝒂𝒎𝒆 = ′ 𝑨𝑺 𝑼 ′ ∧ 𝒑𝑷𝒐𝒔 = ′ 𝒈𝒐𝒂𝒍𝒊 𝒆 ′ ∧𝒅𝒆𝒄 = ′ 𝒏 𝒐 ′ 𝑻𝒓𝒚𝒐𝒖𝒕 College Player Tryout Yields only the rows that appear in both tables NOTE: Not directly supported in MySQL. So do it this way: SELECT * FROM a WHERE (x,y) IN ( SELECT * FROM b) NOTE: Union-compatible: Tables share the same number of columns, and their corresponding columns share compatible domains Note: intersection doesn’t add any expressive power E1 INTERSECT E2 == E1 – (E1 – E2) Explain with Venn cName state enr LSU LA 18000 ASU AZ 12000 OSU OK 22000 FSU FL pID pName yCard HS 10001 Andy no 1200 20002 Blake 1600 30003 Chuck 600 40004 Dave yes 50005 Ed 300 60006 Ford 250 pID cName pPos dec 10001 LSU goalie no ASU mid yes 20002 FSU strike 30003 OSU 40004 50005 College Tryout `

23 Figure Intersect MySQL

24 Rename: 𝞺 Find the size of the smallest high school (using a copy of Player under the new name s) College Player Tryout SQL Query would be (using NOT IN because MySQL has no MINUS operator): SELECT DISTINCT HS FROM Player, Tryout WHERE Player.pID = Tryout.pID AND HS NOT IN (SELECT Player.HS FROM Player, Player AS s, Tryout WHERE Player.HS > s.HS AND s.pID = Tryout.pID); For the textbook example using the RA tool, the query was: \project_{salary}instructor \diff (\project_{salary1} (\select_{salary1<salary2} (\rename_{ID1,name1,dept_name1,salary1} instructor \cross \rename_{ID2,name2,dept_name2,salary2} instructor))); cName state enr LSU LA 18000 ASU AZ 12000 OSU OK 22000 FSU FL pID pName yCard HS 10001 Andy no 1200 20002 Blake 1600 30003 Chuck 600 40004 Dave yes 50005 Ed 300 60006 Ford 250 pID cName pPos dec 10001 LSU goalie no ASU mid yes 20002 FSU strike 30003 OSU 40004 50005 College Tryout `

25 Player.pID Player.pName Player.yCard Player.HS s.pID s.pName s.yCard s.HS 10001 Andy no 1200 20002 Blake 1600 30003 Chuck 600 40004 Dave yes 50005 Ed 300 60006 Ford 250 π HS (Player) - π Player.HS (σ Player.HS>s.HS (Player ⨯ (ρ s (Player)))) `

26 ` Player.pID Player.pName Player.yCard Player.HS s.pID s.pName s.yCard
s.HS 10001 Andy no 1200 30003 Chuck 600 50005 Ed yes 300 60006 Ford 250 20002 Blake 1600 40004 Dave π HS (Player) - π Player.HS (σ Player.HS>s.HS (Player ⨯ (ρ s (Player)))) `

27 - = Relational Algebra is SET based! No duplicates! Player.HS 1200
1600 600 300 250 Player.HS 1200 1600 600 300 Player.HS 250 - = π HS (Player) - π Player.HS (σ Player.HS>s.HS (Player ⨯ (ρ s (Player)))) Relational Algebra is SET based! No duplicates! `

28 Three forms of Rename: 𝞺
Expression E under the name X Attributes of E renamed Everything renamed College Player Tryout cName state enr LSU LA 18000 ASU AZ 12000 OSU OK 22000 FSU FL pID pName yCard HS 10001 Andy no 1200 20002 Blake 1600 30003 Chuck 600 40004 Dave yes 50005 Ed 300 60006 Ford 250 pID cName pPos dec 10001 LSU goalie no ASU mid yes 20002 FSU strike 30003 OSU 40004 50005 College Tryout `

29 Division: ÷ McCann* describes it this way: Think of Division as the opposite of cross-product The Division operator helps you find the tuples that are not in the answer, and removes them. *

30 Division: ÷ PlayerGoals Venues pName Goals vName Joe 1 Pepsi Ann TD-Garden 2 AA Jim 3 United vName City State TD-Garden Boston MA United Chicago IL AA Dallas TX Pepsi Denver CO 1. Who played in all of the venues? ΠpName,vName (PlayerGoals) ÷ ΠvName (Venues)  (Joe,Ann) 2. Who played & scored in all of the venues? ΠpName,vName σGoals>0(PlayerGoals) ÷ ΠvName (Venues)  (Joe) Pi pName,vName PlayerGoals ÷ Pi vName (Venues) Pi pName, vName sigma Goals>0 PlayerGoals ÷ Pi vName Venues Uses one 2-column table as the dividend and one single-column table as the divisor The quotient table is made up of those values of one column for which a second column had all of the values in the divisor. NOTE: Not directly supported in MySQL. So do it this way: Ask class what the answer means: Ann and Joe played in every venue, but only Joe scored at least once in every venue!

31 Figure Divide

32 Player Tryout College Tryout College ` cName state enr LSU LA 18000
Results: Dave 1600 cName state enr LSU LA 18000 ASU AZ 12000 OSU OK 22000 FSU FL pID pName yCard HS 10001 Andy no 1200 20002 Blake 1600 30003 Chuck 600 40004 Dave yes 50005 Ed 300 60006 Ford 250 pID cName pPos dec 10001 LSU goalie no ASU mid yes 20002 FSU strike 30003 OSU 40004 50005 College Tryout `

33 Strikers accepted by FSU who didn’t tryout anywhere else
College Player Tryout How to draw this as a venn Diagram? cName state enr LSU LA 18000 ASU AZ 12000 OSU OK 22000 FSU FL pID pName yCard HS 10001 Andy no 1200 20002 Blake 1600 30003 Chuck 600 40004 Dave yes 50005 Ed 300 60006 Ford 250 pID cName pPos dec 10001 LSU goalie no ASU mid yes 20002 FSU strike 30003 OSU 40004 50005 College Tryout ` `

34 Natural Join Find the names of players with no yellow cards and the colleges where they tried out. College Player Tryout Pi cName, pName (sigma yCard='no' ( Player ⨝ Tryout)) Links tables by selecting only the rows with common values in their common attributes The Cross-Product can introduce nonsense tuples that you have to get rid with selects. Inner join: Only returns matched records from the tables that are being joined The Natural Join operator makes this easier. HOWEVER, the matching columns are BOTH/ALL included in the resulting relation cName state enr LSU LA 18000 ASU AZ 12000 OSU OK 22000 FSU FL pID pName yCard HS 10001 Andy no 1200 20002 Blake 1600 30003 Chuck 600 40004 Dave yes 50005 Ed 300 60006 Ford 250 pID cName pPos dec 10001 LSU goalie no ASU mid yes 20002 FSU strike 30003 OSU 40004 50005 College Tryout `

35 Natural Join Player Tryout College Tryout College
Find the names of all students from high schools with less than 1000 students who tried out for the position of ‘mid’ at colleges with more than 10,000 students and failed at least once. College Player Tryout cName state enr LSU LA 18000 ASU AZ 12000 OSU OK 22000 FSU FL pID pName yCard HS 10001 Andy no 1200 20002 Blake 1600 30003 Chuck 600 40004 Dave yes 50005 Ed 300 60006 Ford 250 pID cName pPos dec 10001 LSU goalie no ASU mid yes 20002 FSU strike 30003 OSU 40004 50005 College Tryout `

36 Natural Join Player Tryout College Tryout College
Find the IDs and names of students who registered but didn’t tryout anywhere. College Player Tryout cName state enr LSU LA 18000 ASU AZ 12000 OSU OK 22000 FSU FL pID pName yCard HS 10001 Andy no 1200 20002 Blake 1600 30003 Chuck 600 40004 Dave yes 50005 Ed 300 60006 Ford 250 pID cName pPos dec 10001 LSU goalie no ASU mid yes 20002 FSU strike 30003 OSU 40004 50005 College Tryout `

37 Theta Join If the Player schema included the State in which the Player went to High School, we could have: Find the names of students who tried out in the same state as their high school College PlayerSt Tryout cName state enr LSU LA 18000 ASU AZ 12000 OSU OK 22000 FSU FL pID pName yCard HS HSst 10001 Andy no 1200 AZ 20002 Blake 1600 NH 30003 Chuck 600 OK 40004 Dave yes TX 50005 Ed 300 MD 60006 Ford 250 VT pID cName pPos dec 10001 LSU goalie no ASU mid yes 20002 FSU strike 30003 OSU 40004 50005 College Tryout `

38 Theta Join: Find which Trucks will be able to tow which trailers.
When using theta join, the two relations need not have attribute with the same name if the theta join condition involves attributes from each. Find which Trucks will be able to tow which trailers. Truck Trailer Model MaxTowWeight Tundra 10000 Sierra 11200 Silverado F-150 7600 Titan 9390 RAM150 8750 Model MaxGrossWeight Mech-10 7800 Mech-20 9200 Deca 10000 Mini 7200 Tryout `

39 Outer Join – Example Relation instructor2 Relation teaches2 ID name
dept_name 10101 Srinivasan CompSci 12121 Wu Finance 15151 Mozart Music ID course_id 10101 CS-101 12121 FIN-201 76766 BIO-101

40 Outer Join – Example ID name dept_name course_id 10101 Srinivasan
CompSci CS-101 12121 Wu Finance FIN-201 ID name dept_name course_id 10101 Srinivasan CompSci CS-101 12121 Wu Finance FIN-201 15151 Mozart Music null Outer join: Matched pairs are retained and unmatched values in the other table are left null Left outer join: Yields all of the rows in the first table, including those that do not have a matching value in the second table Right outer join: Yields all of the rows in the second table, including those that do not have matching values in the first table

41 Outer Join – Example ID name dept_name course_id 10101 Srinivasan
CompSci CS-101 12121 Wu Finance FIN-201 76766 null BIO-101 ID name dept_name course_id 10101 Srinivasan CompSci CS-101 12121 Wu Finance FIN-201 15151 Mozart Music null 76766 BIO-101

42 A natural join is equiv to an inner join with duplicate attributes omitted

43 Aggregate Functions

44 Aggregate Example Find the average salary in each department
instructor result from Silberschatz


Download ppt "A Relation sID sName Major 1025 Alice PHY 1026 Bob CSC 1028"

Similar presentations


Ads by Google