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 Create another entity to avoid update anomalies Insert Delete Update

5 5 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

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 foreign key can never be null 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) ON DELETE RESTRICT);

11 11 Representing a 1:m relationship in MS Access

12 12 Join Create a new table from two existing tables by matching on a common column SELECT * FROM stock, nation WHERE stock.natcode = nation.natcode; stkcodestkfirmstkpricestkqtystkdivstkpenatcodenatcode1natnameexchrate 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

13 13 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

14 14 Control break reporting

15 15 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

16 16 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

17 17 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

18 18 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

19 19 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;

20 20 Views - querying Query exactly as if a table SELECT nation, firm FROM stkvalue WHERE value > 100000; nationfirmvalue AustraliaNarembeen Emu AustraliaIndooroopilly Ruby AustraliaQueensland Diamond United KingdomRoyal Ostrich Farms United KingdomCanadian Sugar United KingdomFreedonia Copper United KingdomBolivian Sheep United KingdomIndian Lead & Zinc United KingdomSri Lankan Gold United KingdomAbyssinian Ruby United KingdomPatagonian Tea United KingdomNigerian Geese United StatesMinnesota Gold United StatesGeorgia Peach

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

22 22 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