Title: Stephen Taylor
1Analysis of the Gareth Walsh Rovers Database
- Stephen Taylor
- Anthony Ung
2Database Purpose and Contents
- This database is designed to catalogue the
admission of new members into the Australian
Scouting organisation the Rovers. The database
stores information regarding the new members as
well as which group they have joined. - Additional tables also store data regarding the
crews and badges which can be earned by the
various members. - The activities table also keeps track of all
Rover related activities.
3Entity Relationship Diagram
4SQL Queries on a Single Entity/ Table
5Projection
- SELECT MEMFirstName, MEMLastName
- FROM WalshRovers05Member
memfirstname Memlastname
Gareth Walsh
Garth Nagle
Alison McKay
6Restrict Using Where
- SELECT
- FROM WalshRovers05Fees
- WHERE FeesDatepaid Is Not Null
memcrewid feesdatedue feesdatepaid feesamount
1 2005-06-05 2005-08-24 12
2 2005-06-05 2005-10-26 100
3 2005-06-05 2005-04-14 100
4 2005-06-05 2005-03-05 12
7Projection and Where
- SELECT memcrewid, feesamount
- FROM WalshRovers05Fees
- WHERE FeesDatepaid Is Not Null
memcrewid feesamount
1 12
2 100
3 100
4 12
5 12
6 100
7 100
8Use Of Is Null
- SELECT MEMFirstName, MEMLastName
- FROM WalshRovers05Member
- WHERE MEMKnightingDate Is Null
Memfirstname memlastname
Jenna Hall
Karina Hammond
Shaun Sproule
George Smith
9Use Of Is Not Null
- SELECT MEMFirstName, MEMLastName
- FROM WalshRovers05Member
- WHERE MEMResigneddate Is Not Null
memfirstname memlastname
Alison Mckay
Jenna Hall
10Use Of In
- SELECT MEMFirstName, MEMLastName
- FROM WalshRovers05Member
- WHERE MEMCrewID In (1,2,3)
Memfirstname memlastname
Gareth Walsh
Garth Nagle
Alison Mckay
11Use of Not In
- SELECT MEMFirstName, MEMLastName
- FROM WalshRovers05Member
- WHERE MEMCrewID Not In (1,2,3)
Memfirstname memlastname
Lloyd Schrader
Kent Palmer
Jenna Hall
12Ordering Columns
- SELECT MEMFirstName, MEMLastName
- FROM WalshRovers05Member
- SELECT MEMLastName, MEMFirstName
- FROM WalshRovers05Member
Memfirstname memlastname
Gareth Walsh
Garth Nagle
Alison Mckay
memlastname Memfirstname
Walsh Gareth
Nagle Garth
Mckay Alison
13Ordering Rows
- SELECT MEMFirstName, MEMLastName
- FROM WalshRovers05Member
- ORDER BY MEMLastName
memlastname Memfirstname
Brent Butler
Jenna Hall
Karina Hammond
Tim Hammond
Russell Lyons
14Calculation (Including As)
- SELECT memcrewidfeesamount AS random_no
- FROM Walshrovers05fees
random_no
12
200
400
500
600
700
800
15Count(X)
- When Counting by a specific column Null Values
are not counted - SELECT COUNT(MEMResignedDate)
- FROM WalshRovers05Member
Count
2
16Count()
- Count All will count all rows which have a value
within the table - SELECT COUNT()
- FROM WalshRovers05Member
Count
13
17Use Of Average
- SELECT avg(feesamount)
- FROM walshrovers05fees
AVG
83.2380952380952381
18Use of Sum
- SELECT sum(feesamount) as TOTAL_FEES
- FROM walshrovers05fees
total_fees
1748
19Use of Min
- SELECT min(feesamount)
- FROM walshrovers05fees
min
12
20Use of Max
- SELECT max(feesamount)
- FROM walshrovers05fees
max
100
21Use of Like ()
- SELECT memcrewid,memscoutid,memfirstname,memlastna
me - FROM walshrovers05member
- WHERE memfirstname LIKE 'Gar'
memcrewid memscoutid memfirstname memlastname
1 136369 Gareth Walsh
2 136368 Garth Nagle
22Use of Like (_)
- SELECT memcrewid,memscoutid,memfirstname,memlastna
me - FROM walshrovers05member
- WHERE memfirstname LIKE '_ar'
memcrewid memscoutid memfirstname memlastname
1 136369 Gareth Walsh
2 136368 Garth Nagle
7 987654 Karina Hammond
23Use of Distinct
- Distinct Ensures no rows of identical values are
displayed - The same crewid appears many times in the
activity table but distinct displays it only once - SELECT DISTINCT memcrewid
- FROM walshrovers05memberactivity
- WHERE memcrewid 1
memcrewid
1
24Insert Command
- INSERT INTO WalshRovers05MemberActivity VALUES(
8,'Kiddy Moot', '2005/09/30' )
memcrewid activityname activitystartdate
8 Kiddy Moot 2005/09/30
25Foreign Keys and Natural Joins
26Natural Join
- SELECT MEMFirstName, MEMLastName
- FROM WalshRovers05Member NATURAL JOIN
WalshRovers05Position - WHERE PosTitle 'Secretary'
- GROUP BY MEMFirstName, MEMLastName
Memfirstname Memlastname
Jenna Hall
27Cross Product Notation
- SELECT MEMFirstName, MEMLastName
- FROM WalshRovers05Member, WalshRovers05Position
- WHERE WalshRovers05Member.MEMCrewID
WalshRovers05Position.MEMCrewID - AND PosTitle 'Secretary
Memfirstname Memlastname count
Jenna Hall 5
28Foreign Entities and Relationships
29One to Many Relationship
- SELECT MEMFirstName,MEMLastName
- FROM WalshRovers05Member
- WHERE MEMCrewID 1
- SELECT
- FROM WalshRovers05Fees
- WHERE MEMCrewID 1
Memfirstname memlastname
Gareth Walsh
memcrewid feesdatedue feesdatepaid feesamount
1 2005-06-05 2005-08-24 12
1 2004-06-05 2004-08-24 12
30Many to Many Relationship
- There is a many-to-many relationship between the
members and activities table which is separated
into two one-to-many relationships. There can be
many members in each activity and each member can
be in many activities. - SELECT
- FROM WalshRovers05MemberActivity
- WHERE MEMCrewID 1
memcrewid activityname activitystartdate
1 Kiddy Moot 2005-09-30
1 Jota 2005-10-14
1 Bushdance 2005-09-17
31Many to Many Relationship
- SELECT
- FROM WalshRovers05MemberActivity
- WHERE ActivityName 'Bushdance'
memcrewid activityname activitystartdate
1 Bushdance 2005-09-17
2 Bushdance 2005-09-17
7 Bushdance 2005-09-17
32Group By's, Sub-Queries and Complex Joins
33Group By
- SELECT MEMFirstName, MEMLastName,
Count(ActivityName) - FROM WalshRovers05MemberActivity NATURAL JOIN
WalshRovers05Member NATURAL JOIN
WalshRovers05Position - WHERE PosTitle 'Crew Leader'
- GROUP BY MEMFirstName, MEMLastName
memfirstname memlastname count
Garth Nagle 7
Tim Hammond 5
Gareth Walsh 7
34Group By Having
- SELECT MEMFirstName, MEMLastName,
Count(ActivityName) - FROM WalshRovers05MemberActivity NATURAL JOIN
WalshRovers05Member NATURAL JOIN
WalshRovers05Position - WHERE PosTitle 'Crew Leader'
- GROUP BY MEMFirstName, MEMLastName
- HAVING Count(ActivityName) gt 5
memfirstname memlastname count
Garth Nagle 7
Gareth Walsh 7
35Sub Query Showing One Value
- SELECT MEMFirstName, MEMLastName
- FROM WalshRovers05Member
- WHERE MEMCrewID (SELECT avg(MEMCrewID)
- FROM WalshRovers05Member
Activity - WHERE ActivityName
'Bushdance')
memfirstname memlastname
Jenna Hall
36Sub Query Using Min/Max
- SELECT MEMFirstName, MEMLastName
- FROM WalshRovers05Member
- WHERE MEMCrewID (SELECT max(MEMCrewID)
- FROM WalshRovers05Member
Activity - WHERE ActivityName
'Bushdance')
memfirstname memlastname
Brent Butler
37Sub Query Using Any/All
- SELECT MEMFirstName, MEMLastName
- FROM WalshRovers05Member
- WHERE MEMCrewID Any (SELECT MEMCrewID
- FROM WalshRovers05Member
Activity - WHERE ActivityName
'Bushdance')
memfirstname memlastname
Gareth Walsh
Garth Nagle
Karina Hammond
Tim Hammond
Brent Butler
38Sub Query Using In
- SELECT MEMFirstName, MEMLastName
- FROM WalshRovers05Member
- WHERE MEMCrewID IN (SELECT MEMCrewID
- FROM WalshRovers05Member
Activity - WHERE ActivityName
'Bushdance')
memfirstname memlastname
Gareth Walsh
Garth Nagle
Karina Hammond
Tim Hammond
Brent Butler
39Left Outer Join
- SELECT WalshRovers05Member.memcrewid, Postitle
- FROM WalshRovers05Member Left Join
WalshRovers05Position Using (memcrewid) -
Memcrewid postitle
1 Crew Leader
2 Crew Leader
3 NULL
4 NULL
40Right Outer Join
- SELECT WalshRovers05Member.memcrewid, Postitle
- FROM WalshRovers05Member Right Join
WalshRovers05Position Using (memcrewid)
Memcrewid postitle
1 Crew Leader
8 Crew Leader
2 Crew Leader
5 Treasurer
41Self Join
- SELECT squires.MEMSquiringDate as
investature_date , squires.MEMFirstName AS
Squire_FirstName, - squires.MEMLastName AS Squire_LastName,
rovers.MEMFirstName AS Rover_FirstName,
rovers.MEMLastName AS - Rover_LastName
- FROM WalshRovers05Member squires,
WalshRovers05Member rovers - WHERE squires.MEMSquiringDate
rovers.MEMKnightingDate
Investature_date Squire_firstname Squire_lastname Rover_firstname Rover_Lastname
2003-04-18 Jenna Hall Brent Butler
2003-04-18 Jenna Hall Alison Mckay
42Data Integrity With SQL
43Creation of a Foreign Key
- Create TABLE WalshRovers05Member
- (
- MEMCrewID INTEGER,
- CONSTRAINT WalshRovers05Member_PK PRIMARY KEY
(MEMCrewID), - Create TABLE WalshRovers05Position
- (
- MEMCrewID INTEGER,
- CONSTRAINT WalshRovers05Position_MEMCrewID_FK
FOREIGN KEY (MEMCrewID) REFERENCES
WalshRovers05Member - ON DELETE RESTRICT
44Check Statement
- CONSTRAINT ValidGender CHECK ( MEMGender 'M' OR
MEMGender 'F'), - CONSTRAINT ValidDOB CHECK ( MEMDOB BETWEEN
'1900-01-01' AND '2005-01-01'), - CONSTRAINT ValidMEMJoiningDate CHECK (
MEMJoiningDate BETWEEN '1900-01-01' AND
'2006-01-01'), - CONSTRAINT ValidMEMSquiringDate CHECK (
MEMSquiringDate BETWEEN '1900-01-01' AND
'2006-01-01'), - CONSTRAINT ValidMEMKnightingDate CHECK (
MEMKnightingDate BETWEEN '1900-01-01' AND
'2006-01-01')
45On Delete Restrict
- CONSTRAINT WalshRovers05Fees_MEMCrewID_FK FOREIGN
KEY (MEMCrewID) REFERENCES WalshRovers05Member - ON DELETE RESTRICT
46On Delete Cascade
- CONSTRAINT WalshRovers05Badge_MEMCrewID_FK
FOREIGN KEY (MEMCrewID) REFERENCES
WalshRovers05Member - ON DELETE CASCADE
47Normalization
(removed from this case study, as normalization
was not done well.)
48Creation of a View
- CREATE VIEW fees1 (memcrewid, FeesDatedue,
FeesDatePaid, FeesAmount) - AS SELECT
- FROM WalshRovers05Fees
- SELECT FROM Fees1
memcrewid feesdatedue feesdatepaid feesamount
1 2005-06-05 2005-08-24 12
2 2005-06-05 2005-10-26 100