Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Management Fall 2003 The one-to-many relationship Joins, Views, Subqueries & Group by.

Similar presentations


Presentation on theme: "Database Management Fall 2003 The one-to-many relationship Joins, Views, Subqueries & Group by."— Presentation transcript:

1 Database Management Fall 2003 The one-to-many relationship Joins, Views, Subqueries & Group by

2 stock stkcodestkfirmstkpricestkqtystkdivstkpe FCFreedonia Copper27.50105291.8416 PTPatagonian Tea55.25126352.5010 ARAbyssinian Ruby31.82220101.3213 SLGSri Lankan Gold50.37328682.6816 ILZIndian Lead &Zinc37.7563903.0012 BEBurmese Elephant.071547130.013 BSBolivian Sheep12.752316781.7811 NGNigerian Geese35.00123231.6810 CSCanadian Sugar52.7847162.5015 ROFRoyal Ostrich Farms33.7512349233.00 6 MGMinnesota Gold53.878161221.0025 GPGeorgia Peach2.35387333.20 5 NENarembeen Emu12.34456191.00 8 QDQueensland Diamond6.7389251.50 7 IRIndooroopilly Ruby15.9256147.5020 BDBombay Duck25.551673821.0012 Add exchange rate information

3 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 Diamond 6.7389251.50 7Australia0.46 IRIndooroopilly Ruby15.9256147.5020Australia0.46 BDBombay Duck25.551673821.0012India 0.0228

4 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 8Australia 0.51 QDQueensland Diamond 6.7389251.50 7Australia0.46 IRIndooroopilly Ruby15.9256147.5020Australia0.46 BDBombay Duck25.551673821.0012India 0.0228 Update Anomaly

5 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 Japan0.85 MGMinnesota Gold53.878161221.0025USA0.67 GPGeorgia Peach2.35387333.20 5USA0.67 NENarembeen Emu12.34456191.00 8Australia0.46 QDQueensland Diamond 6.7389251.50 7Australia0.46 IRIndooroopilly Ruby15.9256147.5020Australia0.46 BDBombay Duck25.551673821.0012India 0.0228 Insert Anomaly

6 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 Diamond 6.7389251.50 7Australia0.46 IRIndooroopilly Ruby15.9256147.5020Australia0.46 Delete Anomaly – India exchrate data lost

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 Foreign Key

8 nation natcodenatnameexchrate UKUnited Kingdom1.00 USAUnited States0.67 AUSAustralia 0.51 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 Update anomaly eliminated

9 nation natcodenatnameexchrate UKUnited Kingdom1.00 USAUnited States0.67 AUSAustralia0.46 INDIndia0.0228 JPJapan0.85 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 Insert anomaly eliminated

10 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 Delete anomaly eliminated

11 Data modeling – representing the 1:M relationship Watson’s looks like this: Ours will look like this:

12 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 the foreign key goes on the M side foreign key-primary key matching Names for a foreign key and the corresponding primary key are often the same, but not always

13 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

14 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_stock_nation FOREIGN KEY(natcode) REFERENCES nation)

15 o Doing a join using WHERE with = (an equijoin) and qualification. o Using GROUP BY with one or more operations (e.g., COUNT, SUM) that produce a single result (results row) for each group. o Using HAVING (functionally similar to WHERE) to pick a subset of the groups identified by a GROUP BY. o Using a subquery (inner query, nested SELECT) to define a condition to evaluate in the WHERE clause of an outer query. Also – the correlated subquery. o Using CREATE VIEW to create a view or virtual table. Other basic SQL

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

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

18 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

19 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

20 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

21 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

22 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

23 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

24 Views - querying Query exactly as if a table SELECT nation, firm FROM stkvalue WHERE value > 100000 nationfirm 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

25 Key ideas (… so to speak) o the foreign key goes on the M side othe foreign key doesn’t go on the 1 side (this would create a multi-valued attribute – bad) o foreign key-primary key matching o names for a foreign key and the corresponding primary key are often the same, but not always oreferential integrity constraint: … when a record (row) on the M side references a record on the 1 side, the latter must exist! This implies that the 1-side record must be added first; it also implies that it cannot be deleted as long as any record on the M side references it. Implementing the 1:M relationship in the actual database …


Download ppt "Database Management Fall 2003 The one-to-many relationship Joins, Views, Subqueries & Group by."

Similar presentations


Ads by Google