# 6 - One-to-One and Recursive Relationships

1.
Draw data models for the following problems:
1a.
(i)
A dairy farmer, who is also a part-time cartoonist, has several herds of cows. He has assigned each cow to a particular herd. In each herd, the farmer has one cow that is his favorite--often that cow is featured in a cartoon.
1c.
The legion, the basic combat unit of the ancient Roman army, contained 3,000 to 6,000 men, consisting primarily of heavy infantry (hoplites), supported by light infantry (velites), and sometimes by cavalry. The hoplites were drawn up in three lines. The hastati (youngest men) were in the first, the principes (seasoned troops) in the second, and the triarii (oldest men) behind them, reinforced by velites. Each line was divided into 10 maniples, consisting of two centuries (60 to 80 men per century) each. Each legion had a commander, and a century was commanded by a centurion. Julius Caesar, through one of his Californian channelers, has asked you to design a database to maintain details of soldiers. Of course, Julius is a little forgetful at times, and he has not supplied the titles of the officers who command maniples, lines, and hoplites, but he expects that you can handle this lack of fine detail.
1e.
The Center for the Study of World Trade keeps track of trade treaties between nations. For each treaty, it records details of the countries signing the treaty and where and when it was signed.
1g.
The IS department of a large organization makes extensive use of software modules. New applications are built, where possible, from existing modules. Software modules can also contain other modules. The IS manager realizes that she now needs a database to keep track of which modules are used in which applications or other modules. (Hint: it is helpful to think of an application as a module.)
2.
Write SQL to answer the following queries using the dept and emp tables described in this chapter:
2a.
Find the departments where all the employees earn less than their boss.
```WITH
wrk as (SELECT * FROM emp),
boss as (SELECT * FROM emp)
SELECT DISTINCT (deptname) FROM dept WHERE deptname NOT IN (SELECT wrk.deptname FROM wrk JOIN boss ON wrk.bossno = boss.empno WHERE wrk.empsalary > boss.empsalary);```
empfname
Andrew
Clare
Nancy
Sarah
2c.
List the departments having an average salary greater than \$25,000.
```SELECT deptname, AVG(empsalary) FROM emp
GROUP BY deptname HAVING AVG(empsalary) > 25000;```
deptname expr1001
2e.
List the names and manager of the employees of the Marketing department who have a salary greater than \$25,000.
```WITH
wrk as (SELECT * FROM emp),
boss as (SELECT * FROM emp)
SELECT wrk.empfname, boss.empfname FROM wrk JOIN boss ON wrk.bossno = boss.empno WHERE wrk.deptname = 'Marketing' AND wrk.empsalary > 25000;```
3.
Write SQL to answer the following queries using the monarch table described in this chapter:
3a.
Who succeeded Victoria I?
```SELECT monname, monnum FROM monarch
WHERE premonname = 'Victoria'
AND premonnum = 'I'; ```
monname monnum
Edward VII
3c.
How many kings are there in the table?
`SELECT COUNT(montype) FROM monarch WHERE montype = 'King'; `
4.
Write SQL to answer the following queries using the product and assembly tables:
4a.
How many different items are there in the animal photography kit?
`SELECT COUNT(*) FROM product JOIN assembly ON product.prodid = assembly.prodidWHERE proddesc = 'Animal photography kit'; `
4c.
What is the total cost of the components of the animal photography kit?
`SELECT SUM(PRODCOST*quantity) AS 'Total cost' FROM product JOIN assembly ON product.prodid = assembly.subprodidWHERE product.prodid IN (SELECT subprodid FROM product JOIN assembly ON product.prodid = assembly.prodidWHERE proddesc = 'Animal photography kit');`