A 3 Solution
A 3 Solution
A 3 Solution
E}
={A,B} so the FD AE is NOT redundant.
(E)
+
K-{E
D}
={E,B} so the FD ED is NOT redundant.
(A)
+
K-{A
B}
={A,E,B} so the FD AB is redundant and will be removed from K.
(BD)
+
K-{BD
E}
={B,D,H} so the FD BDE is NOT redundant.
(F)
+
K-{F
A}
={F} so the FD FA is NOT redundant.
(E)
+
K-{E
B}
={E,D,H} so the FD EB is NOT redundant.
(D)
+
K-{D
H}
={D} so the FD DH is NOT redundant.
(BG)
+
K-{BG
F}
={B,G} so the FD BGF is NOT redundant.
(CD)
+
K-{CD
A}
={C,D,H} so the FD CDA is NOT redundant.
So the minimal cover is as follows:
AE
ED
BDE
FA
EB
DH
BGF
CDA
(b) A possible 3NF decomposition:
R
1
(A,E) AE
CSC343 Introduction to Databases Assignment 3
ver 2 - solutions Page 11 of 18
R
2
(B,D,E) ED, BDE, EB
R
3
(A,F) FA
R
4
(D,H) DH
R
5
(B,F,G) BGF
R
6
(A,C,D) CDA
R
7
(B,C,G) no functional dependency.
(c) The solution to part (b) is also in BCNF.
CSC343 Introduction to Databases Assignment 3
ver 2 - solutions Page 12 of 18
Question 4. XML DTDs and Queries [50 marks]
Consider the following student database. We are interested in placing this information in
document format.
Student Information:
STID Name Phone St.
No
Street
name
City Prov. Courses
taken
Student
Status
Degree
995435245 Ali 416-4245979 406 Main Toronto ON 1 3 4 5 Degree
Student
BSc
995267842 Bob 613-5345660 12 Charles Ottawa ON 3 4 5 Special
Student
997458623 Carlos 905-2348638 5 King Oshawa ON Special
Student
998112455 Fernando 101 Avenue Montreal QC 2 4 8 10 Degree
Student
BEng
993457622 J ason 204-4562983 32 Main Winnipeg MB 1 2 3 Degree
Student
BA
996112321 J un 204-7893242 160 Pembina Winnipeg MB 5 7 10 Degree
Student
BEng
995987345 Lee 647-9982342 35 Charles Toronto ON 3 4 7 9 Degree
Student
BSc
997821345 Lueng 11 Yong Toronto ON Degree
Student
BSc
996453222 Mark 613-4561190 30 University Ottawa ON 1 5 6 7 Special
Student
997424563 Maria 11 Queen Ottawa ON 5 Special
Student
997345632 Nicolas 613-8932456 13 Cumberland Ottawa ON 9 10 Special
Student
Courses information:
ID Course Name Department Allowable Student Status
1 Introduction to Databases CS Degree Student, Special Student
2 Numerical Methods CS Degree Student, Special Student
3 Operating Systems CS Degree Student
4 Computer Graphics CS Degree Student
5 Calculus Sci I MAT Special Student
6 Complex Variables MAT Degree Student, Special Student
7 Groups and Symmetry MAT Degree Student
8 Introduction to Economics Economics Degree Student, Special Student
9 Microeconomic Theory Economics Degree Student
10 Energy & Resources Economics Special Student
CSC343 Introduction to Databases Assignment 3
ver 2 - solutions Page 13 of 18
(a) [10 marks] Define a DTD, students.dtd, for documents that list all courses offered, and then
the courses taken by each student. The DTD should allow each registered student to take 0 or
more courses. Phone numbers are optional for. Also, only degree students have a degree
attribute. A course may be taken by students whose status is degree, special or both. A student
may be taking a course for which she cant get credit (e.g., a special student taking a course for
which only degree students get credit).
<?xml version="1.0" encoding="UTF-16LE"?>
<!ELEMENT Report (Students,Courses)>
<!ELEMENT Students (DegreeStudents,SpecialStudents)>
<!ELEMENT Courses (Course+)>
<!ELEMENT DegreeStudents (Student+)>
<!ELEMENT SpecialStudents (Student+)>
<!ELEMENT Student (Address,(Phone?),(CourseTaken*))>
<!ELEMENT Address EMPTY>
<!ELEMENT CourseTaken EMPTY>
<!ELEMENT Phone (#PCDATA)>
<!ELEMENT Course (Acceptance+)>
<!ELEMENT Acceptance (#PCDATA)>
<!ATTLIST CourseTaken CourseID IDREF #REQUIRED>
<!ATTLIST Address StNo CDATA #REQUIRED>
<!ATTLIST Address StName CDATA #REQUIRED>
<!ATTLIST Address City CDATA #REQUIRED>
<!ATTLIST Address Prov CDATA #REQUIRED>
<!ATTLIST Student STID CDATA #REQUIRED>
<!ATTLIST Student Name CDATA #REQUIRED>
<!ATTLIST Student Degree CDATA #IMPLIED>
<!ATTLIST Student Status CDATA #REQUIRED>
<!ATTLIST Course CourseID ID #REQUIRED>
<!ATTLIST Course Name CDATA #REQUIRED>
<!ATTLIST Course Department CDATA #REQUIRED>
(b) [10 marks] Provide an XML document, students.xml, that captures the information given in
the tables above and is consistent with students.dtd.
<?xml version="1.0"?><!DOCTYPE Report SYSTEM "students.dtd">
<Report>
<Students>
<DegreeStudents>
<Student STID="995435245" Name="Ali" Degree="BSc" Status="Degree Student">
<Address StNo="406" StName="Main" City="Toronto" Prov="ON"/>
<Phone>416-4245979</Phone>
<CourseTaken CourseID="c1" />
<CourseTaken CourseID="c3" />
<CourseTaken CourseID="c4" />
<CourseTaken CourseID="c5" />
</Student>
<Student STID="998112455" Name="Fernando" Degree="BEng" Status="Degree Student">
<Address StNo="101" StName="Avenue" City="Montreal" Prov="QC"/>
<CourseTaken CourseID="c2" />
<CourseTaken CourseID="c4" />
<CourseTaken CourseID="c8" />
<CourseTaken CourseID="c10" />
</Student>
<Student STID="993457622" Name="J ason" Degree="BA" Status="Degree Student">
<Address StNo="32" StName="Main" City="Winnipeg" Prov="MB"/>
<Phone>204-4562983</Phone>
<CourseTaken CourseID="c1" />
CSC343 Introduction to Databases Assignment 3
ver 2 - solutions Page 14 of 18
<CourseTaken CourseID="c2" />
<CourseTaken CourseID="c3" />
</Student>
<Student STID="996112321" Name="J un" Degree="BEng" Status="Degree Student">
<Address StNo="160" StName="Pembina" City="Winnipeg" Prov="MB"/>
<Phone>204-7893242</Phone>
<CourseTaken CourseID="c5" />
<CourseTaken CourseID="c7" />
<CourseTaken CourseID="c10" />
</Student>
<Student STID="995987345" Name="Lee" Degree="BSc" Status="Degree Student">
<Address StNo="35" StName="Charlse" City="Toronto" Prov="ON"/>
<Phone>647-9982342</Phone>
<CourseTaken CourseID="c3" />
<CourseTaken CourseID="c4" />
<CourseTaken CourseID="c7" />
</Student>
<Student STID="997821345" Name="Leung" Degree="BSc" Status="Degree Student">
<Address StNo="11" StName="Yong" City="Toronto" Prov="ON"/>
</Student>
</DegreeStudents>
<SpecialStudents>
<Student STID="995267842" Name="Bob" Status="Special Student">
<Address StNo="12" StName="Charles" City="Ottawa" Prov="ON"/>
<Phone>613-5345660</Phone>
<CourseTaken CourseID="c3" />
<CourseTaken CourseID="c4" />
<CourseTaken CourseID="c5" />
</Student>
<Student STID="997458623" Name="Carlos" Status="Special Student">
<Address StNo="5" StName="King" City="Oshawa" Prov="ON"/>
<Phone>905-2348638</Phone>
</Student>
<Student STID="996453222" Name="Mark" Status="Special Student">
<Address StNo="30" StName="University" City="Ottawa" Prov="ON"/>
<Phone>613-4561190</Phone>
<CourseTaken CourseID="c1" />
<CourseTaken CourseID="c5" />
<CourseTaken CourseID="c6" />
<CourseTaken CourseID="c7" />
</Student>
<Student STID="997424563" Name="Maria" Status="Special Student">
<Address StNo="11" StName="Queen" City="Ottawa" Prov="ON"/>
<CourseTaken CourseID="c5" />
</Student>
<Student STID="997345632" Name="Nicolas" Status="Special Student">
<Address StNo="13" StName="Cumberland" City="Ottawa" Prov="ON"/>
<Phone>613-8932456</Phone>
<CourseTaken CourseID="c9" />
<CourseTaken CourseID="c10" />
</Student>
</SpecialStudents>
</Students>
<Courses>
<Course CourseID="c1" Name="Introduction to Databases" Department="CS">
<Acceptance>Degree Student</Acceptance>
<Acceptance>Special Student</Acceptance>
CSC343 Introduction to Databases Assignment 3
ver 2 - solutions Page 15 of 18
</Course>
<Course CourseID="c2" Name="Numerical Methods" Department="CS">
<Acceptance>Degree Student</Acceptance>
<Acceptance>Special Student</Acceptance>
</Course>
<Course CourseID="c3" Name="Operating Systems" Department="CS">
<Acceptance>Degree Student</Acceptance>
</Course>
<Course CourseID="c4" Name="Computer Graphics" Department="CS">
<Acceptance>Degree Student</Acceptance>
</Course>
<Course CourseID="c5" Name="Calculus Sci I" Department="MAT">
<Acceptance>Special Student</Acceptance>
</Course>
<Course CourseID="c6" Name="Complex Variables" Department="MAT">
<Acceptance>Degree Student</Acceptance>
<Acceptance>Special Student</Acceptance>
</Course>
<Course CourseID="c7" Name="Groups and Symmetry" Department="MAT">
<Acceptance>Degree Student</Acceptance>
</Course>
<Course CourseID="c8" Name="Introduction to Economics" Department="ECO">
<Acceptance>Degree Student</Acceptance>
<Acceptance>Special Student</Acceptance>
</Course>
<Course CourseID="c9" Name="Microeconomic Theory" Department="ECO">
<Acceptance>Degree Student</Acceptance>
</Course>
<Course CourseID="c10" Name="Energy and Resource" Department="ECO">
<Acceptance>Degree Student</Acceptance>
</Course>
</Courses>
</Report>
(c) [30 marks] Represent the following queries in XQuery:
I. Display student name and status for all students. Sort the result by student name in
ascending order.
<result>
{
for $c in doc("students.xml")//Student
order by $c/@Name ascending
return <Student>{$c/@Name, $c/@Status} </Student>
}
</result>
output:
<result>
<Student Name="Ali" Status="Degree Student"/>
<Student Name="Bob" Status="Special Student"/>
<Student Name="Carlos" Status="Special Student"/>
<Student Name="Fernando" Status="Degree Student"/>
<Student Name="J ason" Status="Degree Student"/>
<Student Name="J un" Status="Degree Student"/>
<Student Name="Lee" Status="Degree Student"/>
<Student Name="Leung" Status="Degree Student"/>
<Student Name="Maria" Status="Special Student"/>
CSC343 Introduction to Databases Assignment 3
ver 2 - solutions Page 16 of 18
<Student Name="Mark" Status="Special Student"/>
<Student Name="Nicolas" Status="Special Student"/>
</result>
II. Display the number of students for each city mentioned in the database; sort the result by
number of students in a descending order. You need to output city names and the number
of students for each city.
<result>
{
for $c in distinct-values(
doc("students.xml")//Address/@City
)
let $num :=count( for $s in doc("students.xml")//Student
where $s/Address/@City=$c
return $s
)
order by $num descending
return <city>{$c}, {$num}</city>
}
</result>
Output:
<result>
<city>Ottawa, 4</city>
<city>Toronto, 3</city>
<city>Winnipeg, 2</city>
<city>Montreal, 1</city>
<city>Oshawa, 1</city>
</result>
III. Display the number of students who can get a credit for each course. This includes all
students whose status is allowed by any given course. Display course ID, course name and
the number of students who are allowed for each course.
<result>{
for $c in doc("students.xml")//Course
let $students :=
count (
for $s in doc("students.xml")//Student
where
some $ids in $s/CourseTaken/@CourseID ,
$status in $c/Acceptance
satisfies $c/@CourseID =$ids and $s/@Status =$status
return $s
)
order by $students descending
return
<course>
{$c/@ID, $c/@Name}
<students>{ $students }</students>
</course>
}
</result>
CSC343 Introduction to Databases Assignment 3
ver 2 - solutions Page 17 of 18
Output:
<result>
<course ID="1" Name="Introduction to Databases"><students>3</students></course>
<course ID="3" Name="Operating Systems"><students>3</students></course>
<course ID="4" Name="Computer Graphics"><students>3</students></course>
<course ID="5" Name="Calculus Sci I"><students>3</students></course>
<course ID="2" Name="Numerical Methods"><students>2</students></course>
<course ID="7" Name="Groups and Symmetry"><students>2</students></course>
<course ID="10" Name="Energy and Resource"><students>2</students></course>
<course ID="6" Name="Complex Variables"><students>1</students></course>
<course ID="8" Name="Introduction to Economics"><students>1</students></course>
<course ID="9" Name="Microeconomic Theory"><students>0</students></course>
</result>
IV. Display the name of students who have no credits for CS department courses. Include in
your answer students who sit in a CS course without having allowable student status. Sort
the result by student name in an ascending order.
<return>
{
for $s in doc("students.xml")//Student
where
not( for $ids in $s/CourseTaken/@CourseID,
$c in doc("students.xml")//Course[@Department="CS"]
where $ids=$c/@CourseID
and (
some $status in $c[@CourseID=$ids]/Acceptance
satisfies $s/@Status =$status
)
return $ids
)
order by $s/@Name ascending
return <Student>{$s/@Name} </Student>
}
</return>
Output:
<return>
<Student Name="Bob"/>
<Student Name="Carlos"/>
<Student Name="J un"/>
<Student Name="Leung"/>
<Student Name="Maria"/>
<Student Name="Nicolas"/>
</return>
CSC343 Introduction to Databases Assignment 3
ver 2 - solutions Page 18 of 18
V. Display the names of students who take at least two courses they are allowed to get credit
for from the same department. Sort the result by student name in descending order.
<result>
{
let $departments :=distinct-values( doc("students.xml")//Course/@Department)
for $s in doc("students.xml")//Student
where
not(
empty(
for $d in $departments
where count(
for $ids in $s/CourseID,
$c in doc("courses.xml")//Course[@Department=$d]
where $ids=$c/@ID
and (
some $status in $c[@ID=$ids]/Acceptance
satisfies $s/@Status =$status
)
return $ids
)>1
return $d
)
)
order by $s/@Name descending
return <student>{$s/@Name} </student>
}
</result>
Output:
<result>
<student Name="Mark"/>
<student Name="Lee"/>
<student Name="J ason"/>
<student Name="Fernando"/>
<student Name="Ali"/>
</result>