Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Dance Fever Case Study

Similar presentations


Presentation on theme: "The Dance Fever Case Study"— Presentation transcript:

1 The Dance Fever Case Study
Jenny Jirathammakul Karen Sin Whitney Tang Distinction Assignment, Autumn 2007

2 Introduction to Dance Fever
Dance Fever is a database that shows the dance classes offered by the Mosman Community College. It allows one to search for classes offered according to when one wants to commence their class, the instructor present, the costs, the level of the course and class locations.

3 The ERD for Dance Fever InstructorID* LocationID* ClassID ClassName
MarkovaStal05DanceFeverClass InstructorID* LocationID* ClassID ClassName ClassLvl ClassDay ClassCost ClassStarts ClassFinish MarkovaStal05DanceFeverInstructor InstructorID InstructorName InstructorSurname MarkovaStal05DanceFever Location LocationID LocationName LocationStreet LocationSuburb Run Teach Location_Class Instructor_Class Enrolled Class_ClassStudent Enrolled MarkovaStal05DanceFever Student StudentID StudentName StudentSurname MarkovaStal05DanceFever ClassStudent ClassID* StudentID* ClassStudent_Student

4 SQL Queries on a Single Entity/Table

5 Project ( using “select” )
Select ALL students enrolled in Dance Fever. select * from MarkovaStal05DanceFeverStudent; studentid | studentname | studentsurname 1 | Andrew | Liceenko 2 | Anthia | Bey 3 | Denisse | Dufey 4 | Dianna | Shmarokova 5 | Kennedy | Gandhi 6 | Antonio | Hays 7 | Nadia | Bossi 8 | Natali | Spears 9 | Carola | Chain 10 | Paul | Dufey 11 | Danielle | Croker 12 | Gustavo | Diab 13 | Stal | Markova 14 | Carollina | James 15 | Vivian | Flood (15 rows) The columns you list between ‘SELECT’ and ‘FROM’ are displayed.

6 Restrict (using ‘where’)
Get all the information stored in MarkovaStal05DanceFeverClass select * from MarkovaStal05DanceFeverClass; locationid | instructorid | classid | classname | classlvl | classday | classcost | classstarts | classfinish 2 | | | Afro Brasilian Dance | | Wednesday | | | 2 | | | Ballroom Dancing | BEG | Tuesday | | | 2 | | | Ballroom Dancing | INT | Tuesday | | | 3 | | | Ballet | BEG | Wednesday | | | 3 | | | Ballet | ADV | Wednesday | | | 3 | | | Belly Dancing | BEG | Monday | | | 3 | | | Belly Dancing | ADV | Tuesday | | | 3 | | | Belly Dancing | INT | Wednesday | | | 1 | | | Social Modern Dance | INT | Wednesday | | | 1 | | | Social Modern Dance | ADV | Wednesday | | | 1 | | | Social Modern Dance | BEG | Tuesday | | | 2 | | | International Solo Dance | | Monday | | | 2 | | | Hot Salsa Dance | BEG | Monday | | | 2 | | | Hot Salsa Dance | ADV | Monday | | | 1 | | | Argentine Tango | BEG | Tuesday | | | 1 | | | Argentine Tango | INT | Monday | | | 1 | | | Argentine Tango | ADV | Monday | | | (17 rows) Get all the classes occurring on ‘Tuesday’. select * from MarkovaStal05DanceFeverClass where classday = 'Tuesday'; locationid | instructorid | classid | classname | classlvl | classday | classcost | classstarts | classfinish 2 | | | Ballroom Dancing | BEG | Tuesday | | | 2 | | | Ballroom Dancing | INT | Tuesday | | | 3 | | | Belly Dancing | ADV | Tuesday | | | 1 | | | Social Modern Dance | BEG | Tuesday | | | 1 | | | Argentine Tango | BEG | Tuesday | | | (5 rows)

7 Project and restrict combo
List the classes’ id, name, level and cost where cost is greater than $100. select classid, classname, classlvl, classcost from MarkovaStal05DanceFeverClass where classcost > 100; classid | classname | classlvl | classcost 41102 | Afro Brasilian Dance | | 41106 | Ballet | BEG | 41107 | Ballet | ADV | 41108 | Belly Dancing | BEG | 41109 | Belly Dancing | ADV | 41110 | Belly Dancing | INT | (6 rows)

8 Example of IS NULL select classid, classname, classlvl, classcost from MarkovaStal05DanceFeverClass where classlvl is null; classid | classname | classlvl | classcost 41102 | Afro Brasilian Dance | | 41116 | International Solo Dance | | (2 rows)

9 Example of IS NOT NULL Select classid, classname, classlvl, classcost from MarkovaStal05DanceFeverClass where classlvl is not null; classid | classname | classlvl | classcost 41104 | Ballroom Dancing | BEG | 41105 | Ballroom Dancing | INT | 41106 | Ballet | BEG | 41107 | Ballet | ADV | 41108 | Belly Dancing | BEG | 41109 | Belly Dancing | ADV | 41110 | Belly Dancing | INT | 41113 | Social Modern Dance | INT | 41114 | Social Modern Dance | ADV | 41115 | Social Modern Dance | BEG | 41117 | Hot Salsa Dance | BEG | 41118 | Hot Salsa Dance | ADV | 41119 | Argentine Tango | BEG | 41120 | Argentine Tango | INT | 41126 | Argentine Tango | ADV | (15 rows)

10 IN Select all classes with ballet and hot salsa dance.
select * from MarkovaStal05DanceFeverClass where classname in ('Ballet','Hot Salsa Dance'); locationid | instructorid | classid | classname | classlvl | classday | classcost | classstarts | classfinish 3 | | | Ballet | BEG | Wednesday | | | 3 | | | Ballet | ADV | Wednesday | | | 2 | | | Hot Salsa Dance | BEG | Monday | | | 2 | | | Hot Salsa Dance | ADV | Monday | | | (4 rows)

11 NOT IN Select all classes without ballet and hot salsa dance.
select * from MarkovaStal05DanceFeverClass WHERE classname not in ('Ballet','Hot Salsa Dance'); locationid | instructorid | classid | classname | classlvl | classday | classcost | classstarts | classfinish 2 | | | Afro Brasilian Dance | | Wednesday | | | 2 | | | Ballroom Dancing | BEG | Tuesday | | | 2 | | | Ballroom Dancing | INT | Tuesday | | | 3 | | | Belly Dancing | BEG | Monday | | | 3 | | | Belly Dancing | ADV | Tuesday | | | 3 | | | Belly Dancing | INT | Wednesday | | | 1 | | | Social Modern Dance | INT | Wednesday | | | 1 | | | Social Modern Dance | ADV | Wednesday | | | 1 | | | Social Modern Dance | BEG | Tuesday | | | 2 | | | International Solo Dance | | Monday | | | 1 | | | Argentine Tango | BEG | Tuesday | | | 1 | | | Argentine Tango | INT | Monday | | | 1 | | | Argentine Tango | ADV | Monday | | | (13 rows)

12 Ordering Columns select studentname, studentsurname from MarkovaStal05DanceFeverStudent wehre studentname like 'A%'; studentname | studentsurname Andrew | Liceenko Anthia | Bey Antonio | Hays (3 rows) Select studentsurname, studentname from MarkovaStal05DanceFeverStudent where studentname like 'A%'; studentsurname | studentname Liceenko | Andrew Bey | Anthia Hays | Antonio

13 Ordering rows: “order by”
Ordered by the studentname. select studentname, studentsurname from MarkovaStal05DanceFeverStudent order by studentname; studentname | studentsurname Andrew | Liceenko Anthia | Bey Antonio | Hays Carola | Chain Carollina | James Danielle | Croker Denisse | Dufey Dianna | Shmarokova Gustavo | Diab Kennedy | Gandhi Nadia | Bossi Natali | Spears Paul | Dufey Stal | Markova Vivian | Flood (15 rows)

14 Calculating select classstarts, classfinish, classfinish-classstarts as classduration from MarkovaStal05DanceFeverClass; classstarts | classfinish | classduration 19 | | 20.3 | | 20 | | 18.3 | | 19 | | 19.15 | | 20.15 | | 19 | | 21 | | (17 rows)

15 Built-in functions Count the number of students.
select count (*) as numberofstudents from MarkovaStal05DanceFeverStudent; numberofstudents 15 (1 row) select count (locationname) as numberoflocations from MarkovaStal05DanceFeverLocation; numberoflocations 3

16 MIN and MAX Functions Select price of cheapest class.
select MIN(classcost) from MarkovaStal05DanceFeverClass; min ----- 99 (1 row) Select price of most expensive class. select MAX(classcost) from MarkovaStal05DanceFeverClass; max 156

17 AVG and SUM Functions Average of entire Dance Fever classes offered.
select avg(classcost) from MarkovaStal05DanceFeverClass; avg (1 row) Sum of entire Dance Fever classes offered. select sum(classcost) from MarkovaStal05DanceFeverClass; sum ------ 1913

18 LIKE – Pattern matching
List all students with name starting with ‘A’. select studentsurname, studentname from MarkovaStal05DanceFeverStudent where studentname like 'A%'; studentsurname | studentname Liceenko | Andrew Bey | Anthia Hays | Antonio (3 rows) List all students that contain ‘Andrew’ in their name. select studentsurname, studentname from MarkovaStal05DanceFeverStudent where studentname like '%Andrew%'; (1 row)

19 LIKE – Pattern matching 2
Find student name with ‘a’ as the third letter in their name. select studentsurname, studentname from MarkovaStal05DanceFeverStudent where studentname like '__a%'; studentsurname | studentname Shmarokova | Dianna Markova | Stal (2 rows)

20 DISTINCT select classname from MarkovaStal05DanceFeverClass;
Afro Brasilian Dance Ballroom Dancing Ballet Belly Dancing Social Modern Dance International Solo Dance Hot Salsa Dance Argentine Tango (17 rows) select DISTINCT classname from MarkovaStal05DanceFeverClass; classname Afro Brasilian Dance Argentine Tango Ballet Ballroom Dancing Belly Dancing Hot Salsa Dance International Solo Dance Social Modern Dance (8 rows) DISTINCT eliminates the duplicate rows

21 Inserting rows INSERT INTO MarkovaStal05DanceFeverStudent VALUES (16,'Hero','Kim'); studentid | studentname | studentsurname 1 | Andrew | Liceenko 2 | Anthia | Bey 3 | Denisse | Dufey 4 | Dianna | Shmarokova 5 | Kennedy | Gandhi 6 | Antonio | Hays 7 | Nadia | Bossi 8 | Natali | Spears 9 | Carola | Chain 10 | Paul | Dufey 11 | Danielle | Croker 12 | Gustavo | Diab 13 | Stal | Markova 14 | Carollina | James 15 | Vivian | Flood 16 | Hero | Kim (16 rows)

22 Foreign Keys and Natural Joins

23 STUDENT and CLASS STUDENT
select classid, studentid, studentname, studentsurname from MarkovaStal05DanceFeverStudent natural join MarkovaStal05DanceFeverClassStudent; classid | studentid | studentname | studentsurname 41102 | | Danielle | Croker 41104 | | Natali | Spears 41104 | | Gustavo | Diab 41106 | | Antonio | Hays 41106 | | Nadia | Bossi 41107 | | Nadia | Bossi 41108 | | Denisse | Dufey 41108 | | Dianna | Shmarokova 41108 | | Vivian | Flood 41109 | | Dianna | Shmarokova 41109 | | Carola | Chain 41110 | | Carola | Chain 41113 | | Andrew | Liceenko 41113 | | Dianna | Shmarokova 41113 | | Paul | Dufey 41113 | | Stal | Markova 41117 | | Andrew | Liceenko 41117 | | Anthia | Bey 41117 | | Kennedy | Gandhi 41117 | | Paul | Dufey 41118 | | Denisse | Dufey 41118 | | Dianna | Shmarokova 41118 | | Paul | Dufey 41118 | | Stal | Markova 41119 | | Carollina | James (25 rows)

24 Cross Product Natural Join
select classid, MarkovaStal05DanceFeverStudent.studentid, studentname, studentsurname from MarkovaStal05DanceFeverClassStudent, MarkovaStal05DanceFeverStudent where MarkovaStal05DanceFeverStudent.StudentId = MarkovaStal05DanceFeverClassStudent.StudentId; classid | studentid | studentname | studentsurname 41102 | | Danielle | Croker 41104 | | Natali | Spears 41104 | | Gustavo | Diab 41106 | | Antonio | Hays 41106 | | Nadia | Bossi 41107 | | Nadia | Bossi 41108 | | Denisse | Dufey 41108 | | Dianna | Shmarokova 41108 | | Vivian | Flood 41109 | | Dianna | Shmarokova 41109 | | Carola | Chain 41110 | | Carola | Chain 41113 | | Andrew | Liceenko 41113 | | Dianna | Shmarokova 41113 | | Paul | Dufey 41113 | | Stal | Markova 41117 | | Andrew | Liceenko 41117 | | Anthia | Bey 41117 | | Kennedy | Gandhi 41117 | | Paul | Dufey 41118 | | Denisse | Dufey 41118 | | Dianna | Shmarokova 41118 | | Paul | Dufey 41118 | | Stal | Markova 41119 | | Carollina | James (25 rows)

25 Entities and Relationships

26 1:M Relationship ONE Location can teach MANY classes
MarkovaStal05DanceFever Class InstructorID* LocationID* ClassID ClassName ClassLvl ClassDay ClassCost ClassStarts ClassFinish MarkovaStal05DanceFever Location LocationID LocationName LocationStreet LocationSuburb Run ONE Location can teach MANY classes select * from MarkovaStal05DanceFeverLocation locationid | locationname | locationstreet | locationsuburb 1 | Mosman High School | Gladstone Ave | Mosman 2 | Mosman Primary School Hall | Belmont Rd | Mosman Location_Class select * from MarkovaStal05DanceFeverClass where locationid = ‘2’; locationid | instructorid | classid | classname | classlvl | classday | classcost | classstarts | classfinish 2 | | | Afro Brasilian Dance | | Wednesday | | | 2 | | | Ballroom Dancing | BEG | Tuesday | | |

27 M:M relationship MANY Students are in MANY classes select studentname
MarkovaStal05DanceFever Class InstructorID* LocationID* ClassID ClassName ClassLvl ClassDay ClassCost ClassStarts ClassFinish MANY Students are in MANY classes select studentname from MarkovaStal05Dance FeverStudent; studentname Andrew Anthia Denisse Dianna Kennedy Antonio Nadia Natali Carola Paul Danielle Gustavo Stal Carollina Vivian (15 rows) select Distinct Classname from MarkovaStal05DanceFeverClass; classname Afro Brasilian Dance Argentine Tango Ballet Ballroom Dancing Belly Dancing Hot Salsa Dance International Solo Dance Social Modern Dance (8 rows) Enrolled Class_ClassStudent Enrolled MarkovaStal05DanceFever Student StudentID StudentName StudentSurname MarkovaStal05DanceFever ClassStudent ClassID* StudentID* ClassStudent_Student Fix it up…need the tables in there…

28 Group by, sub-queries and complex joins

29 GROUP BY without HAVING
Find out how many classes are on at Mosman high school select locationname, count(*) as Noofclasses from MarkovaStal05DanceFeverLocation natural join MarkovaStal05DanceFeverClass where locationid = 1 group by locationname; locationname | noofclasses Mosman High School | (1 row)

30 GROUP BY with HAVING Find out which students attend more than 2 classes. select studentname, count(*) as Noofclasses from MarkovaStal05DanceFeverClass natural join MarkovaStal05DanceFeverClassStudent natural join MarkovaStal05DanceFeverstudent group by studentname having count(*) >2; studentname | noofclasses Dianna | Paul | (2 rows)

31 Subquery - return one value
Find the name of the earliest class on at St Josephs Preschool select Classname, classstarts from MarkovaStal05DanceFeverClass natural join MarkovaStal05DanceFeverLocation where locationid =3 and classstarts <= all (select classstarts from MarkovaStal05DanceFeverClass natural join MarkovaStal05DanceFeverLocation) ; classname | classstarts Ballet | (1 row)

32 Subquery- include Max and Min
Find out the name and location of the class that ends the latest. select classname, locationname, classfinish from MarkovaStal05DanceFeverClass natural join MarkovaStal05DanceFeverLocation where classfinish >= (select Max(classfinish) from MarkovaStal05DanceFeverClass natural join MarkovaStal05DanceFeverLocation); classname | locationname | classfinish Ballroom Dancing | Mosman Primary School Hall | Hot Salsa Dance | Mosman Primary School Hall | (2 rows) Find out the name and location of the class that ends the earliest. where classfinish <= (select Min(classfinish) from MarkovaStal05DanceF everClass natural join MarkovaStal05DanceFeverLocation); classname | locationname | classfinish Ballet | St Josephs Preschool | International Solo Dance | Mosman Primary School Hall |

33 Subquery-All Finds the name of the class and location that finishes the latest. select classname, locationname, classfinish from MarkovaStal05DanceFeverClass natural join MarkovaStal05DanceFeverLocation where classfinish >= all(select classfinish from MarkovaStal05DanceFeverClass natural join MarkovaStal05DanceFeverLocation); classname | locationname | classfinish Ballroom Dancing | Mosman Primary School Hall | Hot Salsa Dance | Mosman Primary School Hall | (2 rows) Using Is the same as using where classfinish >= (select Max(classfinish) from MarkovaStal05DanceFeverClass natural join MarkovaStal05DanceFeverLocation);

34 Subquery- Any Find out which class offers beginners class.
select classname from MarkovaStal05DanceFeverClass where classlvl = ANY (select classlvl from MarkovaStal05DanceFeverClass where classlvl = 'BEG'); classname Ballroom Dancing Ballet Belly Dancing Social Modern Dance Hot Salsa Dance Argentine Tango (6 rows)

35 Subquery ‘IN’ select classname from MarkovaStal05DanceFeverClass
where classlvl IN (select classlvl from MarkovaStal05DanceFeverClass where classlvl = 'BEG'); classname Ballroom Dancing Ballet Belly Dancing Social Modern Dance Hot Salsa Dance Argentine Tango (6 rows)

36 Left Outer Join select instructorname, instructorsurname, classlvl
from MarkovaStal05DanceFeverclass left join MarkovaStal05DanceFeverinstructor using (instructorid); instructorname | instructorsurname | classlvl Edivaldo | Ribeiro | George | Constantine | BEG George | Constantine | INT Michelle | Abdilla | BEG Michelle | Abdilla | ADV Midge | Bennett | BEG Midge | Bennett | ADV Midge | Bennett | INT Tony | Leoni | INT Tony | Leoni | ADV Tony | Leoni | BEG Magdalena | Ilie | Magdalena | Ilie | BEG Magdalena | Ilie | ADV (17 rows)

37 Right Outer Join select instructorname, instructorsurname, classlvl
from MarkovaStal05DanceFeverclass right join MarkovaStal05DanceFeverinstructor using (instructorid); instructorname | instructorsurname | classlvl Edivaldo | Ribeiro | George | Constantine | BEG George | Constantine | INT Magdalena | Ilie | Magdalena | Ilie | BEG Magdalena | Ilie | ADV Michelle | Abdilla | BEG Michelle | Abdilla | ADV Midge | Bennett | BEG Midge | Bennett | ADV Midge | Bennett | INT Tony | Leoni | INT Tony | Leoni | ADV Tony | Leoni | BEG (17 rows)

38 Self Join List all the classes that occur on Wednesday and their class level select name.classname, c1.classid as wednesday, c1.classlvl from MarkovaStal05DanceFeverClass name, MarkovaStal05DanceFeverClass c1 where c1.classday = 'Wednesday‘ and name.classid = c1.classid; classname | wednesday | classlvl Afro Brasilian Dance | | Ballet | | BEG Ballet | | ADV Belly Dancing | | INT Social Modern Dance | | INT Social Modern Dance | | ADV (6 rows)

39 Data Integrity with SQL

40 Declare foreign key Alter table MarkovaStal05DanceFeverClass add Constraint MarkovaStal05DanceFeverLocation_MarkovaStal05DanceFeverClass foreign key (LocationID) references MarkovaStal05DanceFeverLocation (LocationID) on update restrict on delete restrict;

41 CHECK statements Constraint MarkovaStal05DanceFeverClass_ClassStarts CHECK ((ClassStarts > 0) AND (ClassStarts <= 24)), Constraint MarkovaStal05DanceFeverClass_ClassDay CHECK (ClassDay IN ('Monday', 'Tuesday', 'Wednesday'))

42 ON DELETE RESTRICT Alter table MarkovaStal05DanceFeverClass add Constraint MarkovaStal05DanceFeverLocation_MarkovaStal05DanceFeverClass foreign key (LocationID) references MarkovaStal05DanceFeverLocation (LocationID) on update restrict on delete restrict;

43 ON DELETE CASCADE Alter table MarkovaStal05DanceFeverClass add Constraint MarkovaStal05DanceFeverLocation_MarkovaStal05DanceFeverClass foreign key (LocationID) references MarkovaStal05DanceFeverLocation (LocationID) on update restrict on delete cascade;

44 Normalisation

45 1st Normal Form LocationID InstructorID ClassID LocationName LocationStreet Other Columns… 1 6 41114 Mosman High School Gladstone Ave Etc… 2 41106 St Josephs Preschool Lindsay St 3 4 41119 The Primary Keys 1st Normal Form has all the data entered into one table. This is a problem as there would be redundant data. The solution is to spilt the tables into 3 separate tables LocationID  LocationName, LocationStreet, LocationSuburb InstructorID  InstructorName, InstructorSurname ClassID  StudentID

46 2nd Normal Form – Almost Perfect
LocationID LocationName LocationStreet LocationSuburb 1 Mosman High School Gladstone Ave Mosman 2 St Josephs Preschool Lindsay St Neutral Bay 3 InstructorID InstructorName InstructorSurname 6 Tony Leoni 4 Michelle Abdilla ClassID StudentID 41114 4 41106 14 41119 6 The respective Primary Keys of each table 2nd Normal form splits the 1st Normal form table into 3 separate tables in order to be rid of repetitive data.

47 2nd Normal Form – The Problem
ClassID StudentID StudentName StudentSurname 41114 4 Dianna Shmarokova 41106 14 Carollina James 41119 6 Antonio Hays The Primary Key Although it splits the table so that the data is not redundant, the table above, ClassID can be further segregated into another table (for StudentID) in order to reduce more redundant data ClassID  StudentID StudentID  StudentName, StudentSurname

48 3rd Normal Form ClassID StudentID 41114 4 41106 14 41119 6 StudentID StudentName StudentSurname 4 Dianna Shmarokova 14 Carollina James 6 Antonio Hays The respective Primary Keys of each table 3rd Normal form allows for StudentID column to be separated into its own table with StudentName and StudentSurname. Therefore this has solved the problem of redundant data.

49 CREATE VIEW create view class (id, class, level, cost, start, finish, day, location, instructor) as select classid, classname, classlvl, classcost, classstarts, classfinish, classday, locationname, instructorname from MarkovaStal05DanceFeverClass, MarkovaStal05DanceFeverInstructor, MarkovaStal05DanceFeverLocation where MarkovaStal05DanceFeverLocation.locationid = MarkovaStal05DanceFeverClass.locationid and MarkovaStal05DanceFeverClass.instructorid = MarkovaStal05DanceFeverInstructor.instructorid;

50 Example of Querying a VIEW
Create a view that shows the class, instructor’s name, cost, start time, finish time and location for the Intermediate Level classes select class, instructor, cost, start, finish, location from class where level = 'INT'; class | instructor | cost | start | finish | location Ballroom Dancing | George | 99 | | | Mosman Primary School Hall Belly Dancing | Midge | 140 | | | St Josephs Preschool Social Modern Dance | Tony | 99 | | | Mosman High School Argentine Tango | Tony | 99 | | | Mosman High School (4 rows)


Download ppt "The Dance Fever Case Study"

Similar presentations


Ads by Google