Presentation is loading. Please wait.

Presentation is loading. Please wait.

The One-to-Many Relationship Cow of many-well milked and badly fed Spanish proverb.

Similar presentations


Presentation on theme: "The One-to-Many Relationship Cow of many-well milked and badly fed Spanish proverb."— Presentation transcript:

1 The One-to-Many Relationship Cow of many-well milked and badly fed Spanish proverb

2 2 The one-to-many relationship Entities are related to other entities A 1:m relationship

3 3 Hierarchical relationships Occur frequently Multiple 1:m relationships

4 4 STOCK with additional columns stock stkcodestkfirmstkpricestkqtystkdivstkpenatnameexchrate FCFreedonia Copper27.50105291.8416United Kingdom1.00 PTPatagonian Tea55.25126352.5010United Kingdom1.00 ARAbyssinian Ruby31.82220101.3213United Kingdom1.00 SLGSri Lankan Gold50.37328682.6816United Kingdom1.00 ILZIndian Lead &Zinc37.7563903.0012United Kingdom1.00 BEBurmese Elephant.071547130.013United Kingdom1.00 BSBolivian Sheep12.752316781.7811United Kingdom1.00 NGNigerian Geese35.00123231.6810United Kingdom1.00 CSCanadian Sugar52.7847162.5015United Kingdom1.00 ROFRoyal Ostrich Farms33.7512349233.00 6United Kingdom1.00 MGMinnesota Gold53.878161221.0025USA0.67 GPGeorgia Peach2.35387333.20 5USA0.67 NENarembeen Emu12.34456191.00 8Australia0.46 QDQueensland Diamond6.7389251.50 7Australia0.46 IRIndooroopilly Ruby15.9256147.5020Australia0.46 BDBombay Duck25.551673821.0012India 0.0228

5 5 Create another entity to avoid update anomalies Insert Delete Update

6 6 Mapping to a relational database Each entity becomes a table The entity name becomes the table name Each attribute becomes a column Add a column to the table at the many end of a 1:m relationship Put the identifier of the one end in the added column

7 NATION and STOCK nation natcodenatnameexchrate UKUnited Kingdom1.00 USAUnited States0.67 AUSAustralia0.46 INDIndia0.0228 stock stkcodestkfirmstkpricestkqtystkdivstkpenatcode FCFreedonia Copper27.50105291.8416UK PTPatagonian Tea55.25126352.5010UK ARAbyssinian Ruby31.82220101.3213UK SLGSri Lankan Gold50.37328682.6816UK ILZIndian Lead &Zinc37.7563903.0012UK BEBurmese Elephant.071547130.013UK BSBolivian Sheep12.752316781.7811UK NGNigerian Geese35.00123231.6810UK CSCanadian Sugar52.7847162.5015UK ROFRoyal Ostrich Farms33.7512349233.00 6UK MGMinnesota Gold53.878161221.0025USA GPGeorgia Peach2.35387333.205USA NENarembeen Emu12.34456191.008AUS QDQueensland Diamond6.7389251.507AUS IRIndooroopilly Ruby15.9256147.5020AUS BDBombay Duck25.551673821.0012IND

8 8 Foreign keys A foreign key is a column that is a primary key of another table natcode in stock is a foreign key because natcode is the primary key of nation Record a 1:m relationship

9 9 Referential integrity constraint For every value of a foreign key there is a primary key with that value For every value of natcode in stock there is a value of natcode in nation A primary key must exist before the foreign key can be defined Must create the nation before its stocks

10 10 Creating the tables CREATE TABLE nation ( natcodeCHAR(3), natnameVARCHAR(20), exchrateDECIMAL(9,5), PRIMARY KEY (natcode)); CREATE TABLE stock ( stkcodeCHAR(3), stkfirmVARCHAR(20), stkpriceDECIMAL(6,2), stkqtyDECIMAL(8), stkdivDECIMAL(5,2), stkpeDECIMAL(5), natcodeCHAR(3), PRIMARY KEY(stkcode), CONSTRAINT fk_has_nation FOREIGN KEY(natcode) REFERENCES nation(natcode));

11 11 Representing a 1:m relationship in MySQL Workbench A non-identifying relationship in MySQL Workbench

12 12 Representing a 1:m relationship in MS Access

13 Exercise Develop a data model to keep track of a distance runner’s times over various lengths Create the database and add 3 rows for each of 2 athletes 13

14 14 Join Create a new table from two existing tables by matching on a common column SELECT * FROM stock, nation WHERE stock.natcode = nation.natcode; stkcodestkfirmstkpricestkqtystkdivstkpenatcode natnameexchrate NENarembeen Emu12.34456191.008AUS Australia0.46000 IRIndooroopilly Ruby15.92561470.5020AUS Australia0.46000 QDQueensland Diamond6.73892510.507AUS Australia0.46000 BDBombay Duck25.551673821.0012IND India0.02280 ROFRoyal Ostrich Farms33.7512349233.006UK United Kingdom1.00000 CSCanadian Sugar52.7847162.5015UK United Kingdom1.00000 FCFreedonia Copper27.50105291.8416UK United Kingdom1.00000 BSBolivian Sheep12.752316781.7811UK United Kingdom1.00000 BEBurmese Elephant0.071547130.013UK United Kingdom1.00000 ILZIndian Lead & Zinc37.7563903.0012UK United Kingdom1.00000 SLGSri Lankan Gold50.37328682.6816UK United Kingdom1.00000 ARAbyssinian Ruby31.82220101.3213UK United Kingdom1.00000 PTPatagonian Tea55.25126352.5010UK United Kingdom1.00000 NGNigerian Geese35.00123231.6810UK United Kingdom1.00000 MGMinnesota Gold53.878161221.0025US United States0.67000 GPGeorgia Peach2.353873330.205US United States0.67000

15 15 Join Report the value of each stock holding in UK pounds. Sort the report by nation and firm. SELECT natname, stkfirm, stkprice, stkqty, exchrate, stkprice*stkqty*exchrate AS stkvalue FROM stock,nation WHERE stock.natcode = nation.natcode ORDER BY natname, stkfirm; natnamestkfirmstkpricestkqtyexchratestkvalue AustraliaIndooroopilly Ruby15.92561470.46000411175.71 AustraliaNarembeen Emu12.34456190.46000258951.69 AustraliaQueensland Diamond6.73892510.46000276303.25 IndiaBombay Duck25.551673820.0228097506.71 United KingdomAbyssinian Ruby31.82220101.00000700358.20 United KingdomBolivian Sheep12.752316781.000002953894.50 United KingdomBurmese Elephant0.071547131.0000010829.91 United KingdomCanadian Sugar52.7847161.00000248910.48 United KingdomFreedonia Copper27.50105291.00000289547.50 United KingdomIndian Lead & Zinc37.7563901.00000241222.50 United KingdomNigerian Geese35.00123231.00000431305.00 United KingdomPatagonian Tea55.25126351.00000698083.75 United KingdomRoyal Ostrich Farms33.7512349231.0000041678651.25 United KingdomSri Lankan Gold50.37328681.000001655561.16 United StatesGeorgia Peach2.353873330.67000609855.81 United StatesMinnesota Gold53.878161220.6700029456209.73

16 16 GROUP BY - reporting by groups Report by nation the total value of stockholdings. SELECT natname, SUM(stkprice*stkqty*exchrate) AS stkvalue FROM stock, nation WHERE stock.natcode = nation.natcode GROUP BY natname; natnamestkvalue Australia946430.65 India97506.71 United Kingdom48908364.25 United States30066065.54

17 17 HAVING - the WHERE clause of groups Report the total value of stocks for nations with two or more listed stocks. SELECT natname, SUM(stkprice*stkqty*exchrate) AS stkvalue FROM stock, nation WHERE stock.natcode = nation.natcode GROUP BY natname HAVING COUNT(*) >= 2; natnamestkvalue Australia946430.65 United Kingdom48908364.25 United States30066065.54

18 Exercise Report the total dividend payment for each country that has three or more stocks in the portfolio 18

19 Structure of SQL statements 19

20 Regular expression Search for a string not containing specified characters [^a-f] means any character not in the set containing a, b, c, d, e, or f List the names of nations with non- alphabetic characters in their names SELECT * FROM nation WHERE natname REGEXP '[^a-z|A-Z]'

21 Regular expression Search for a string containing a repetition {n} means repeat the pattern n times List the names of firms with a double ‘e’. SELECT * FROM stock WHERE stkfirm REGEXP '[e]{2}'

22 Regular expression Search for a string containing several different specified strings | means alternation (or) List the names of firms with a double ‘s’ or a double ‘n’. SELECT * FROM stock WHERE stkfirm REGEXP '[s]{2}|[n]{2}'

23 Regular expression Search for multiple versions of a string [ea] means any character from the set containing e and a It will match for ‘e’ or ‘a’ List the names of firms with names that include ‘inia’ or ‘onia’. SELECT * FROM stock WHERE stkfirm REGEXP '[io]nia'

24 24 Regular expression Find firms with ‘t’ as the third letter of their name. SELECT shrfirm FROM share WHERE shrfirm REGEXP '^(.){2}t'; Find firms not containing an ‘s’ in their name. SELECT shrfirm FROM share WHERE shrfirm NOT REGEXP 's|S';

25 regexlib.com A library of regular expressions Cheat sheet for creating expressions Regex Tester

26 Exercise Report the names of nations starting with ‘United’ 26

27 27 Subqueries A query nested within another query Report the names of all Australian stocks. SELECT stkfirm FROM stock WHERE natcode IN (SELECT natcode FROM nation WHERE natname = 'Australia'); stkfirm Narembeen Emu Queensland Diamond Indooroopilly Ruby

28 28 Correlated subquery Solves the inner query many times Find those stocks where the quantity is greater than the average for that country. SELECT natname, stkfirm, stkqty FROM stock, nation WHERE stock.natcode = nation.natcode AND stkqty > (SELECT AVG(stkqty) FROM stock WHERE stock.natcode = nation.natcode); natnamestkfirmstkqty AustraliaQueensland Diamond89251 United KingdomBolivian Sheep231678 United KingdomRoyal Ostrich Farms1234923 United StatesMinnesota Gold816122 Correlated subqueries can be resource intensive

29 Correlated subquery nation natcodenatnameexchrate UKUnited Kingdom1.00 USAUnited States0.67 AUSAustralia0.46 INDIndia0.0228 stock stkcodestkfirmstkpricestkqtystkdivstkpenatcode FCFreedonia Copper27.50105291.8416UK PTPatagonian Tea55.25126352.5010UK ARAbyssinian Ruby31.82220101.3213UK SLGSri Lankan Gold50.37328682.6816UK ILZIndian Lead &Zinc37.7563903.0012UK BEBurmese Elephant.071547130.013UK BSBolivian Sheep12.752316781.7811UK NGNigerian Geese35.00123231.6810UK CSCanadian Sugar52.7847162.5015UK ROFRoyal Ostrich Farms33.7512349233.00 6UK MGMinnesota Gold53.878161221.0025USA GPGeorgia Peach2.35387333.205USA NENarembeen Emu12.34456191.008AUS QDQueensland Diamond6.7389251.507AUS IRIndooroopilly Ruby15.9256147.5020AUS BDBombay Duck25.551673821.0012IND SELECT natname, stkfirm, stkqty FROM stock, nation WHERE stock.natcode = nation.natcode AND stkqty > (SELECT AVG(stkqty) FROM stock WHERE stock.natcode = nation.natcode);

30 Correlated subquery nation natcodenatnameexchrate UKUnited Kingdom1.00 USAUnited States0.67 AUSAustralia0.46 INDIndia0.0228 stock stkcodestkfirmstkpricestkqtystkdivstkpenatcode FCFreedonia Copper27.50105291.8416UK PTPatagonian Tea55.25126352.5010UK ARAbyssinian Ruby31.82220101.3213UK SLGSri Lankan Gold50.37328682.6816UK ILZIndian Lead &Zinc37.7563903.0012UK BEBurmese Elephant.071547130.013UK BSBolivian Sheep12.752316781.7811UK NGNigerian Geese35.00123231.6810UK CSCanadian Sugar52.7847162.5015UK ROFRoyal Ostrich Farms33.7512349233.00 6UK MGMinnesota Gold53.878161221.0025USA GPGeorgia Peach2.35387333.205USA NENarembeen Emu12.34456191.008AUS QDQueensland Diamond6.7389251.507AUS IRIndooroopilly Ruby15.9256147.5020AUS BDBombay Duck25.551673821.0012IND SELECT natname, stkfirm, stkqty FROM stock, nation WHERE stock.natcode = nation.natcode AND stkqty > 172275.5;

31 Exercise Report the country, firm, and stock holding for the maximum quantity of stock held for each country 31

32 32 Views - virtual tables An imaginary table constructed by the DBMS when required Only the definition of the view is stored, not the result CREATE VIEW stkvalue (nation, firm, price, qty, exchrate, value) AS SELECT natname, stkfirm, stkprice, stkqty, exchrate, stkprice*stkqty*exchrate FROM stock, nation WHERE stock.natcode = nation.natcode;

33 33 Views - querying Query exactly as if a table SELECT nation, firm, value FROM stkvalue WHERE value > 100000; nationfirmvalue United KingdomFreedonia Copper289547.50 United KingdomPatagonian Tea698083.75 United KingdomAbyssinian Ruby700358.20 United KingdomSri Lankan Gold1655561.16 United KingdomIndian Lead & Zinc241222.50 United KingdomBolivian Sheep2953894.50 United KingdomNigerian Geese431305.00 United KingdomCanadian Sugar248910.48 United KingdomRoyal Ostrich Farms41678651.25 United StatesMinnesota Gold29456209.73 United StatesGeorgia Peach609855.80 AustraliaNarembeen Emu258951.69 AustraliaQueensland Diamond276303.24 AustraliaIndooroopilly Ruby411175.71

34 34 Why create a view? Simplify query writing Calculated columns Restrict access to parts of a table

35 Exercise Create a view for dividend payment 35

36 36 Summary New topics 1:m relationship Foreign key Correlated subquery GROUP BY HAVING clause View


Download ppt "The One-to-Many Relationship Cow of many-well milked and badly fed Spanish proverb."

Similar presentations


Ads by Google