Presentation is loading. Please wait.

Presentation is loading. Please wait.

One-to-One and Recursive Relationships Self-reflection is the school of wisdom Baltastar Gracián.

Similar presentations


Presentation on theme: "One-to-One and Recursive Relationships Self-reflection is the school of wisdom Baltastar Gracián."— Presentation transcript:

1 One-to-One and Recursive Relationships Self-reflection is the school of wisdom Baltastar Gracián

2 2 An organization chart

3 3 Modeling a 1:1 relationship 1:1 relationship is labeled A relationship descriptor Obvious relationships are not labeled DEPT *deptname deptfloor deptphone Department ’ s boss EMP *empno empfname empsalary

4 4 Modeling a recursive relationship A recursive relationship relates an entity to itself Label recursive relationships DEPT *deptname deptfloor deptphone department ’ s boss employee ’ s boss EMP *empno empfname empsalary

5 5 Mapping a 1:1 relationship Usual rules apply Where do you put the foreign key? DEPT EMP Both tables

6 6 Mapping a recursive relationship Usual rules 1:m The entity gets an additional column for the foreign key Need a name different from the primary key

7 7 Results of mapping dept deptnamedeptfloordeptphoneempno Management520011 Marketing120022 Accounting420035 Purchasing420047 Personnel & PR120059 emp empnoempfnameempsalarydeptnamebossno 1Alice75000Management 2Ned45000Marketing1 3Andrew25000Marketing2 4Clare22000Marketing2 5Todd38000Accounting1 6Nancy22000Accounting5 7Brier43000Purchasing1 8Sarah56000Purchasing7 9Sophie35000Personnel & PR1

8 8 Creating the tables CREATE TABLE dept ( deptname VARCHAR(15), deptfloor SMALLINTNOT NULL, deptphone SMALLINTNOT NULL, empnoSMALLINTNOT NULL, PRIMARY KEY(deptname)); CREATE TABLE emp ( empno SMALLINT, empfnameVARCHAR(10), empsalary DECIMAL(7,0), deptname VARCHAR(15), bossno SMALLINT, PRIMARY KEY(empno), CONSTRAINT fk_emp_dept FOREIGN KEY(deptname) REFERENCES dept);

9 9 Querying a 1:1 relationship List the salary of each department’s boss. SELECT empfname, deptname, empsalary FROM emp WHERE empno IN (SELECT empno FROM dept); empfnamedeptnameempsalary AliceManagement75000 NedMarketing45000 ToddAccounting38000 BrierPurchasing43000 SophiePersonnel & PR35000

10 10 Querying a recursive relationship Find the salary of Nancy’s boss. SELECT wrk.empfname, wrk.empsalary, boss.empfname, boss.empsalary FROM emp wrk, emp boss WHERE wrk.empfname = 'Nancy' AND wrk.bossno = boss.empno; wrk.empfnamewrk.empsalaryboss.empfnameboss.empsalary Nancy22000Todd38000

11 Joining a table with itself emp empnoempfnameempsalarydeptnamebossno 1Alice75000Management 2Ned45000Marketing1 3Andrew25000Marketing2 4Clare22000Marketing2 5Todd38000Accounting1 6Nancy22000Accounting5 7Brier43000Purchasing1 8Sarah56000Purchasing7 9Sophie35000Personnel & PR1 emp empnoempfnameempsalarydeptnamebossno 1Alice75000Management 2Ned45000Marketing1 3Andrew25000Marketing2 4Clare22000Marketing2 5Todd38000Accounting1 6Nancy22000Accounting5 7Brier43000Purchasing1 8Sarah56000Purchasing7 9Sophie35000Personnel & PR1

12 12 Querying a recursive relationship Find the names of employees who earn more than their boss. SELECT wrk.empfname FROM emp wrk, emp boss WHERE wrk.bossno = boss.empno AND wrk.empsalary > boss.empsalary; wrkboss empnoempfnameempsalarydeptnamebossnoempnoempfnameempsalarydeptnamebossno 2Ned45,000Marketing11Alice75,000Management 3Andrew25,000Marketing22Ned45,000Marketing1 4Clare22,000Marketing22Ned45,000Marketing1 5Todd38,000Accounting11Alice75,000Management 6Nancy22,000Accounting55Todd38,000Accounting1 7Brier43,000Purchasing11Alice75,000Management 8Sarah56,000Purchasing77Brier43,000Purchasing1 9Sophie35,000Personnel & PR11Alice75,000Management empfname Sarah

13 13 Modeling a 1:1 recursive relationship The English monarchy

14 14 Mapping a 1:1 recursive relationship monarch montypemonnamemonnumrgnbegpremonnamepremonnum QueenVictoriaI1837/6/20WilliamIV KingEdwardVII1901/1/22VictoriaI KingGeorgeV1910/5/6EdwardVII KingEdwardVIII1936/1/20GeorgeV KingGeorgeVI1936/12/11EdwardVIII QueenElizabethII1952/2/6GeorgeVI

15 15 Creating the table CREATE TABLE monarch ( montypeVARCHAR(5), monnameVARCHAR(15)NOT NULL, monnumVARCHAR(5)NOT NULL, rgnbegDATE, premonnameVARCHAR(15), premonnumVARCHAR(5), PRIMARY KEY(monname,monnum));

16 16 Querying a 1:1 recursive relationship Who preceded Elizabeth II? SELECT premonname, premonnum FROM monarch WHERE monname = 'Elizabeth' and MONNUM = 'II ' ; premonnamepremonnum GeorgeVI

17 17 Querying a 1:1 recursive relationship Was Elizabeth II's predecessor a king or queen? SELECT pre.montype FROM monarch cur, monarch pre WHERE cur.premonname = pre.monname AND cur.premonnum = pre.monnum AND cur.monname = 'Elizabeth' AND cur.monnum = 'II ' ; montype King

18 18 Querying a 1:1 recursive relationship List the kings and queens of England in ascending chronological order. SELECT montype, monname, monnum, rgnbeg FROM monarch ORDER BY rgnbeg; montypemonnamemonnumrgnbeg QueenVictoriaI1837-06-20 KingEdwardVII1901-01-22 KingGeorgeV1910-05-06 KingEdwardVIII1936-01-20 KingGeorgeVI1936-12-11 QueenElizabethII1952-02-06

19 19 Modeling an m:m recursive relationship Bill of materials problem A product can appear as part of many other products and can be made up of many products ASSEMBLY quantity PRODUCT *prodid proddesc prodcost prodprice

20 20 Mapping an m:m recursive relationship product prodidproddescprodcostprodprice 1000Animal photography kit725 10135mm camera150300 102Camera case1015 10370-210 zoom lens125200 10428-85 zoom lens115185 105Photographer ’ s vest2540 106Lens cleaning cloth11.25 107Tripod3545 10824 exposure, 100 ASA, 35mm color negative film.851 assembly quantityprodidsubprodid 11000101 11000102 11000103 11000104 11000105 21000106 11000107 101000108

21 21 Creating the tables CREATE TABLE product ( prodidINTEGER, proddescVARCHAR(30), prodcostDECIMAL(9,2), prodpriceDECIMAL(9,2), PRIMARY KEY(prodid)); CREATE TABLE assembly ( quantityINTEGERNOT NULL, prodidINTEGER, subprodidINTEGER, PRIMARY KEY(prodid, subprodid), CONSTRAINT fk_assembly_product FOREIGN KEY(prodid) REFERENCES product, CONSTRAINT fk_assembly_subproduct FOREIGN KEY(subprodid) REFERENCES product);

22 22 Querying an m:m recursive relationship List the product identifier of each component of the animal photography kit. SELECT subprodid FROM product, assembly WHERE proddesc = 'Animal photography kit' AND product.prodid = assembly.prodid; subprodid 101 106 107 105 104 103 102 108

23 23 Querying an m:m recursive relationship List the product description and cost of each component of the animal photography kit. SELECT proddesc, prodcost FROM product WHERE prodid IN (SELECT subprodid FROM product, assembly WHERE proddesc = 'Animal photography kit' AND product.prodid = assembly.prodid); proddescprodcost 35mm camera150.00 Camera case10.00 70-210 zoom lens125.00 28-85 zoom lens115.00 Photographer’s vest25.00 Lens cleaning cloth1.00 Tripod35.00 24 exp. 100ASA 35mm col neg0.85


Download ppt "One-to-One and Recursive Relationships Self-reflection is the school of wisdom Baltastar Gracián."

Similar presentations


Ads by Google