Considering the following four tables FarmerSIN name age sex
Considering the following four tables:
Farmer(SIN, name, age, sex, vname)
Village(name, area, population, province)
Kids(SIN, f_sin, m_sin, s_name)
School(sname, vname, no_classes)
Where: sname=school name, vname =village name, f_sin and m_sin stand for the father`s and mother`s SIN, respectively.
1) Define the four tables as flat xml structure (using key and key-ref)?
2) Code the following queries in XQuery:
a) Find SIN of farmers who have some kids going to school?
b) Find name of each school attended only by kids from the same village where the school is located?
c) Find the name of the most crowded village(s) (most population)?
d) Find SIN numbers of farmers who have at least one kid in every school registered in the database?
e) Find names of villages with no schools?
f) Find the school attended by all kids of Mark? (all kids of Mark must go to school and must attend same school)
g) Find names of farmers whose kids go only to schools outside the Province where the farmers live? (the farmer may have kids not going to schools at all, but if some of the kids go to schools then the schools they attend must be in other Provinces)
Thank you!
Solution
CREATE TABLE Farmer
(
name varchar(255),
age int NOT NULL,
sex varchar(255),
vname varchar(255),
SIN varchar(255),
)
CREATE TABLE Village
(
name varchar(255),
area int NOT NULL,
population int NOT,
province varchar(255),
)
CREATE TABLE Kids
(
SIN varchar(255),
f_sin int NOT NULL,
m_sin int NOT,
s_name varchar(255),
)
CREATE TABLE school
(
sname varchar(255),
vname int NOT NULL,
no_classes int NOT,
s_name varchar(255),
)
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<Farmer>
<SIN>COLGATE</SIN>
<Village>vzm</village>
<age>41</age>
<sex>m</sex>
<vnam>FBWU7897630</vname>
</Farmer>
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<Village>
<name>ram</name>
<area>hyd</area>
<population>41</population>
<province>m</province>
</village>
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<kids>
<SIN>ram</SIN>
<f_sin>hyd</f_sin>
<m_sin>41</m_sin>
<s_name>m</s_name>
</kids>
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<school>
<sname>ram</sname>
<vname>hyd</vname>
<no_classes>41</no_classes>
</school>

