64
The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

Embed Size (px)

Citation preview

Page 1: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

The Matthias Knecht Tae Kwon Do Case Study

BHAMBRA ManjyotKOHLI GarimaMANGAL StorayTAWHEED Zureen

Distinction Assignment, Autumn 2007

Page 2: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

2

Introduction to the The Matthias Knecht Tae Kwon Do Case Study

This database is reflective of a Tae Kwon Do challenge and introduces us to all the entities that are involved in such a challenge and the relationships existing between them.

In a typical tournament fighters engage in a challenge and are both judged on their performance.

Competitors choosing to undergo training receive adequate preparation from professional club members. Alternatively competitors can still compete possessing private skills.

Page 3: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

3

The ERD for Matthias Knecht Tae Kwon Do

KnFighter

KnFighterID

KnFighterName

KnFighterBirth

KnFighterGender

KnFighterWeightclass

KnFederation

KnRules

KnTrainer

KnTrainerID

KnTrainerName

KnTrainerBirth

KnTrainerGender

KnClubID *

KnFederationName *

KnRing

KnRingID

KnSize

KnRing

KnSize

KnJudge

KnJudgeID

KnJudgeName

KnJudgeBirth

KnFederationName *

KnTraining

KnFighterID *

KnTrainerID *

KnChallenge

KnFigherID1*

KnFigherID2*

KnFighterGender1

KnFighterGender2

KnFighterWeightclass1

KnFighterWeightclass2

KnRingID *

KnJudgeID *

KnWinner

KnLoser

KnFighterGender1

KnFighterGender2

KnFighterWeightclass1

KnFighterWeightclass2

KnRingID *

KnJudgeID *

KnWinner

KnLoser

KnClub

KnClubID

KnClubName

KnFederationName

Page 4: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

SQL Queries on a Single Entity/ Table

Page 5: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

5

Project [using “select”]

This SELECT query reports the fighters ID number, the fighters name

and his/her weight class.

KnFighter!D KnFighterName KnFighterWeightClass

200001 Karl Dall 5

200002 Martin Knecht 5

200003 Liz Taylor 5

200004 Young Parawan 3

200005 Bruce Lee 3

200006 Elisabeth Brown 3

200007 Christoph Knecht 3

200008 Lars Augenstein 4

200009 Lisa Mueller 4

200010 Larry Wahnhold 4

200011 Chin Moban 4

200012 Chels McGyvan 4

200013 Nathan Johhns 3

200014 Karl Mueller 2

200015 Linda Sureban 2

200016 Isidor Kraus 4

200017 Jet Lee 2

200018 Ling Suk Pran 2

200019 Janina Federholzer 3

200020 Mario Kleinschmid 4

200021 Jana Kralle 5

200022 Ingwan Young Kam 5

The columns listed after SELECT are the ones that are displayed in the table

SELECT

KnFighterID, KnFighterName, KnFighterWeightclass FROM KnFighter;

Page 6: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

6

Restrict [using “where”]Allows the selection of the specific rows of interest from the table

KnFighter!D KnFighterName KnFighterWeightClass

200001 Karl Dall 5

200002 Martin Knecht 5

200003 Liz Taylor 5

200021 Jana Kralle 5

200022 Ingwan Young Kam 5

Using the same sql as the previous slide; the output can be further restricted by simply adding an extra line:

SELECT KnFighterID, KnFighterName, KnFighterWeightclass

FROM KnFighter

WHERE KnFighterWeightClass = 5;

This will restrict the output to showing only those fighters who are categorised as being in the fifth weight class

Page 7: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

7

KnFighter!D KnFighterName KnFighterWeightClass

200001 Karl Dall 5

200002 Martin Knecht 5

200003 Liz Taylor 5

200004 Young Parawan 3

200005 Bruce Lee 3

200006 Elisabeth Brown 3

200007 Christoph Knecht 3

200008 Lars Augenstein 4

200009 Lisa Mueller 4

200010 Larry Wahnhold 4

200011 Chin Moban 4

200012 Chels McGyvan 4

200013 Nathan Johhns 3

200014 Karl Mueller 2

200015 Linda Sureban 2

200016 Isidor Kraus 4

200017 Jet Lee 2

200018 Ling Suk Pran 2

200019 Janina Federholzer 3

200020 Mario Kleinschmid 4

200021 Jana Kralle 5

200022 Ingwan Young Kam 5

Restrict [using “where”]

Rows highlighted here from the

original table are the ones that

match the criteria in the ‘restrict’

query using ‘where’.

In each of the matching records, the fighter weight

class is equal to 5.

Page 8: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

8

Allows specification of any combination of columns and rows for instance:

The following sql would be used to restrict the output to showing fighter names and weight classes of those fighters who have a weight class [knfighterweightclass] of no more than 3 [<= 3]:

SELECT knFighterName, knFighterWeightClass

FROM knFighter

WHERE KnFighterWeightClass <= 3;

Project and Restrict Combo

Page 9: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

9

KnFighter!D KnFighterName KnFighterWeightClass

200004 Young Parawan 3

200005 Bruce Lee 3

200006 Elisabeth Brown 3

200007 Christoph Knecht 3

200013 Nathan Johhns 3

200014 Karl Mueller 2

200015 Linda Sureban 2

200017 Jet Lee 2

200018 Ling Suk Pran 2

200019 Janina Federholzer 3

Project and Restrict Combo

The result of the previous query would be as seen below:

Here, only those fighters with a weight class of 3 or less are listed

Page 10: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

10

IS NULLUsing the IS NULL restriction, the result shows a record with a field where no value has

been inserted.

For instance; to display all the challenges where there was no winner as the match may have been a tie or it may have been uncompleted; the winner field [knwinner] would be null and the query would be written as follows:

SELECT knfighterid1, knfighterid2, knwinner, knloser FROM knchallengeWHERE knwinner IS NULL;

This query would display the following result:

Knfighterid1 Knfighterid2 Knwinner Knloser

200020 200010 200010

Page 11: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

11

Using the IS NOT NULL restriction, the result ensures that the field has a value inserted.

For instance; to display all the judges and the birth year where their birth year is known [i.e. IS NOT NULL], the query would be written as follows:

SELECT knjudgename, knjudgebirth FROM knjudgeWHERE knjudgebirth IS NOT NULL

This query would display the following result:

IS NOT NULL

Knjudgename knjudgebirth

Young Sik Park 1963

Kwung Do Den 1944

Hannes Maurer 1969

Dolo Dobendan 1983

Nunzio Caico 1954

Hung Kwan Do 1931

Klaus Meier 1977

Raymond Lister 1961

Boris Becker 1955

Karl Heinz Hannebu1/4her 1933

Page 12: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

12

INUsed on data to generate required results – works similarly to a ‘where’ function as seen below:

A query using IN looks like:SELECT * FROM KnTrainerWHERE KnClubID IN (‘3',‘10');

This is the same as writing:SELECT * FROM KnTrainerWHERE KnClubID = ‘3' OR KnClubID = ‘10';

Either way; the output will be:

KnTrainerID KnTrainerName KnTrainerBirth KnTrainerGender KnClubID KnFederationName

100003 Lizz Tough 1946 F 3 ITF

100010 Chelsea Lai 1983 F 10 WTF

100011 Matthias Sammer 1963 M 10 WTF

100012 Kim Kwak 1967 M 10 WTF

100014 Susi Ingelheimer 1947 F 3 ITF

Page 13: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

13

NOT INUsed to eliminate some records that match the specified criteria – also works similarly to a ‘where’ function as seen below:

A query using NOT IN would look like:SELECT * FROM KnTrainer WHERE KnTrainerGender NOT IN (‘M');

This is the same as writing: SELECT * FROM KnTrainer WHERE KnTrainerGender <> ‘M‘;

Either way; the output will be:

KnTrainerID KnTrainerName KnTrainerBirth KnTrainerGender KnClubID KnFederationName

100003 Lizz Tough 1946 F 3 ITF

100004 Lollo Ingenheim 1975 F 4 ITF

100007 Claudia Schiffer 1953 F 7 WTF

100009 Lara Croft 1975 F 9 WTF

100010 Chelsea Lai 1983 F 10 WTF

100014 Susi Ingelheimer 1947 F 3 ITF

Page 14: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

14

Ordering columns

For instance:

SELECT KnTrainerID, KnTrainerName FROM KnTrainer WHERE KnTrainerBirth > 1980;

The order in which the columns appear in the table is dependent on the order the columns placed in the SQL following “SELECT”

KnTrainerID KnTrainerName

100010 Chelsea Lai

100013 Kwak Kum Sik

100015 Andre Agassi

Page 15: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

15

Ordering rows using “order by”

This command is used where the results need to be ordered in a specific way – i.e. ascending or descending. For instance:

SELECT KnTrainerName, KnTrainerBirth

FROM KnTrainer

WHERE KnTrainerBirth > 1980

ORDER BY KnTrainerBirth;

KnTrainerName KnTrainerBirth

Chelsea Lai 1983

Andre Agassi 1985

Kwak Kum Sik 1985

Note: the same result would be generated for:SELECT KnTrainerName, KnTrainerBirth FROM KnTrainer WHERE KnTrainerBirth > 1980 ORDER BY KnTrainerBirth ASC;

Page 16: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

16

Calculating – with the use of ‘AS’Using the “AS” function allows the count column to be renamed

For instance, for such a query:

SELECT knwinner, count (*) as number_of_wins FROM knchallengeWHERE knwinner = 200020GROUP BY knwinner;

The count column counting the number of wins by this fighter will be renamed as number_of_wins as seen below:

Knwinner number_of_wins

200020 3

Or even for something simpler; could have SELECT knwinner as WINNER

FROM knchallenge

Page 17: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

17

Built-in functions – Count(*)COUNT(*) – a function that counts all the rows in a table you specify

For instance:

SELECT knWinner, count (*) as number_of_wins FROM knChallengeWHERE knWinner = 200020GROUP BY knWinner;

Note: always requires a group by function when counting all – specifies what is being used to group and count

Knwinner number_of_wins

200020 3 This result shows how many times this fighter has won a challenge

Page 18: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

18

COUNT(X) – a function that counts all the rows in that column (“X”)

For instance; the following will count the number of entries made for ‘knWinner’

SELECT count (knWinner) FROM knChallenge

Furthermore;

SELECT count (distinct knWinner) FROM knChallenge

would count all the ‘distinct’ values in that column – i.e. values that occur only once. So here, it would count each time a fighter wins a fight their first time

Built-in functions – Count(X)

count

20

count

14

Page 19: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

19

Built-in functions [Sum/Avg]

SUM – gives the total for a specified column

SELECT sum (knSize) FROM knRing

AVERAGE

SELECT avg (knSize) FROM knRing

This could be renamed using the AS function by doing:

SELECT avg (knSize) as Average_Ring_Size FROM knRing

avg

11.8

Average_Ring_Size

11.8

Sum

59

Page 20: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

20

Built-in functions [Min/Max]

MIN – gives the minimum value for that column

So the smallest ring where a challenge can be held is:

SELECT min (knSize) FROM knRing

MAX – returns the maximum input for that column

So the highest weight class a fighter can belong to is:

SELECT max (knFighterWeightClass) FROM knFighter

Min

9

Max

5

Page 21: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

21

LIKE – and its operators

KnFighterName

Martin Knecht

Mario Kleinschmid

The LIKE function allows the query to be more specified by either specifying what the entries must start with [using the %] operator or indicating, for instance, a letter that must be present in the result.

The following will return results for names that start with the letter ‘M’

SELECT KnFighterName

FROM KnFighter

WHERE KnFighterName LIKE ‘M%’

Alternatively; a query can be used to return results with a name containing ‘er’ :

SELECT KnFighterName

FROM KnFighter

WHERE KnFighterName LIKE ‘%er%’

KnFighterName

Lisa Mueller

Karl Mueller

Janina Federholzer

Page 22: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

22

LIKE – and its operatorsFor instance, to find a name containing the letter ‘l’ but the it being the

2nd letter in the name:

SELECT knFighterName

FROM knFighter

WHERE knFighterName LIKE ‘_l%’

knFighterName

Elisabeth Brown

KnFighterName

Bruce Lee

Christoph Knecht

Jet Lee

The NOT LIKE function can also be used to do the opposite:

For Instance – names that DO NOT contain the letter ‘a’

SELECT knFighterName

FROM knFighter

WHERE knFighterName NOT LIKE ‘%a%’

Page 23: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

23

DISTINCTThis function works to eliminate any duplicate rows For instance, to view how many different weight classes there are, use:

SELECT DISTINCT KnFighterWeightClass FROM KnFighter;

Furthermore; the total different weight classes are:

SELECT Count (DISTINCT KnFighterWeightClass) FROM KnFighter;

KnFighterWeightClass

2

3

4

5

Count

4

Page 24: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

24

Inserting rows

INSERT INTO KnFighter VALUES (200001,'Karl Dall',1981,'M',5);

This is representative of the fields:

INSERT INTO KnFighter VALUES (KnFighterID,KnFighterName,KnFighterBirth,KnFighterGender,KnFighterWeightclass);

so here: KnFighterID = 200001KnFighterName = Karl Dall

KnFighterBirth = 1981 KnFighterGender = M KnFighterWeightClass = 5

The following insert statement can be used to insert statements into the KnFighter table:

Page 25: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

Foreign Keys and Natural Joins

Page 26: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

26

Club ID and TrainerKnClubid KnClubname

1 Black Tiger Tae-Kwon-Do

2 Sydney Fight Club

3 Do-Kwon-Dan

4 Budo Club Toronto

5 TKD e.V. Freiburg

6 Hiko-Dotan

Primary keys

Foreign keys

KnTrainerid KnTrainername KnTrainerbirth KnTrainergender KnClubid KnFederationname

100001 Hannes Mariz 1977 M 1 ITF

100002 Young Kawan 1967 M 2 ITF

100015 Andre Agassi 1985 M 2 ITF

This illustrates that the primary keys of one table can be foreign keys in another table

Page 27: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

27

The Natural JoinThis function, using existing tables, creates a new table by matching

columns and adding those that are not common

So to join the table KnTrainer and the table KnClub the following query would be used to display this new table where all columns from both tables are displayed:

SELECT * FROM KnTrainer natural join KnClub;

KnTrainerid KnTrainername KnTrainerbirth KnTrainergender KnClubid KnFederationname KnClubname

100001 Hannes Mariz 1977 M 1 ITF Black Tiger Tae-Kwon-Do

100002 Young Kawan 1967 M 2 ITF Sydney Fight Club

100015 Andre Agassi 1985 M 2 ITF Sydney Fight Club

Page 28: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

28

The Cross product function is simple another method of joining two tables (as opposed to the “natural join” on the previous

So once again, to join the tables KnTrainer and KnClub using the cross product, the query would be:

SELECT * FROM KnTrainer, KnClubWHERE KnClub.KnClubid = KnTrainer.KnClubid;

The sql is written as table name followed by the DOT followed by column name and then the name of the other table with the same column separated by ‘=’. This clarifies that the two columns which appear in different tables are actually the same. Thus disambiguating the columns.

• NOTE: If you don’t use a * after SELECT, and you specify “KnClubid”, you will need a to use a dot.

Cross Product

The two columns that are the same

Page 29: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

29

Cross Product

As we can see, the result from both the Natural Join and the Cross Product is the same, the only difference is the cross product defines the common columns and which table they are extracted from

KnTrainerID KnTraineNname KnTrainerBirth KnTrainerGender KnClubID KnFederationName KnClubname

100001 Hannes Mariz 1977 M 1 ITF Black Tiger Tae-Kwon-Do

100002 Young Kawan 1967 M 2 ITF Sydney Fight Club

100015 Andre Agassi 1985 M 2 ITF Sydney Fight Club

Page 30: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

30

More Cross ProductThe Cross Product function can also like other functions be restricted to

displaying certain columns and rows in addition to select data fulfilling certain conditions placed by the sql

For instance, to view the birth, and federation name of judges beginning with ‘K’ the insert statement would be:

Select KnJudgeName, KnJudgeBirth, KnJudge.KnFederationNameFROM KnFederation, KnJudge WHERE KnFederation.KnFederationName = KnJudge.KnFederationName

AND KnJudgeName LIKE ‘K%’;

The KnFederation needs to be assigned a table from which it will be extracted since it exists in two tables and although it is the same, the program does not recognise this and thus it must be pointed out [which is where the Cross Product comes in]

Page 31: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

31

Cross Product

The output of the query from the previous slide would be:

KnJudgeName KnJudgeBirth KnFederationName

Kwung Do Dan 1944 WTF

Klaus Meier 1977 ITF

Karl Heinz Hannebucher 1933 ITF

As we can see, only the columns specified are shown as compared with the KnTrainer and KnClub tables where all the columns from the two tables were shown

Page 32: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

32

More cross Product

KnTrainerID KnTrainerName KnClubID

100001 Hannes Mariz 1

100002 Young Kawan 2

100003 Lizz Tough 3

100004 Lollo Ingenheim 4

Similarly a query where we want to the find the trainer ID, name and which club they belong to would look like :

Where the KnClubID has been extracted from the KnClub table to the KnTrainer table

Page 33: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

Entities and Relationships

Page 34: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

34

KnTraining − KnTrainer1:m relationship

The ERD on slide 3, shows a one to many relationship, like this one

The Actual Tables …

KnTrainerID other columns

100016 …

… etc … …

KnTrainerID KnFighterID

100016 200004

100016 200005

Foreign Key

Page 35: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

35

KnChallenge − KnJudge 1:m relationship

The ERD on slide 3, shows a one to many relationship, like this one

Actual Tables …

KnJudgeID other columns

30006 …

… etc … …

Foreign Key

KnJudgeID KnFighterID1 KnFighterID2

30006 200004 200005

30006 200003 200021

Page 36: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

36

The ERD on slide 3, shows a many to many relationship, like this one

KnFighter − KnTrainer m:m relationship

KnFighterID KnTrainerID

200001 100016

200001 100012

200002 100015

200002 100011

The table “Training”

Page 37: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

37

• These are the different tables associated with this relationship

KnFighter − KnTrainer m:m relationship

KnTrainerID KnTrainerName …

100016 Bruce Perrot …

100012 Kim Kwak …

100015 Andre Agassi …

100011 Matthias Sammer …

KnFighterID KnFighterName …

200001 Karl Dall …

200002 Martin Knecht …

The table “KnTraining”

The table “KnFighter”

The table “KnTrainer”

Page 38: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

38

• These are the different tables associated with this relationship

KnFighter − KnTrainer m:m relationship

KnFighterID KnTrainerID

200001 100016

200001 100012

200002 100015

200002 100011

The table “KnTraining”

The table “KnFighter”

The table “KnTrainer”

KnTrainerID KnTrainerName …

100016 Bruce Perrot …

100012 Kim Kwak …

100015 Andre Agassi …

100011 Matthias Sammer …

KnFighterID KnFighterName …

200001 Karl Dall …

200002 Martin Knecht …

Page 39: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

Group by, sub-queries and complex joins

Page 40: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

40

GROUP BYReporting the number of fighters within each fighter weight class

SELECT count(knfightername) as fighters, knfighterweightclass)

FROM knfighterGROUP BY knfighterweightclass;

fighters knfighterweightclass

5 5

7 4

6 3

4 2

(4 rows)

Page 41: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

41

HAVING – like WHERE, but after the grouping

Report the number of fighters in each class below weight-class 5

count(knfightername) as fighters, knfighterweightclass)

FROM knfighterGROUP BY knfighterweightclass;

HAVING COUNT knfighterweightclass <5;

fighters knfighterweightclass

7 4

6 3

4 2

(4 rows)

Page 42: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

42

Sub Queries• A query within a query

Report all females with a birth year greater than the average birth year of females in weight-class 3.

SELECT knfighterbirth, knfightergender FROM knfighter WHERE knfightergender ='F‘

and knfighterweightclass =3 and knfighterbirth >= (select avg (knfighterbirth)

from knfighter);

AVG

1995 F 3

knfighterbirth knfightergender

knfighterweightclass

1976 F 3

1995 F 3

(2 rows)

Page 43: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

43

Using subqueries to find the maximum (or minimum)

Find the highest weight-class of female competitors. • SELECT distinct knfightergender,

knfighterweightclass

FROM knfighter WHERE knfightergender ='F' and knfighterweightclass >= (select

max(knfighterweightclass) from knfighter where knfightergender ='F');

knfightergender knfighterweightclass

F 5

(1 rows)

Page 44: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

44

Alternate way to find the maximum (or minimum): “ALL”

Give pizza and prices for pizzas that are more expensive than all Italian pizzas.select knfightergender,

knfighterweightclass

from knfighter

where knfightergender ='F' and knfighterweightclass >= ALL ( select knfighterweightclass from knfighter

where knfightergender ='F');

knfightergender knfighterweightclass

F 5

F 5

(1 rows)

Page 45: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

45

Another ALL example

select distinct knfightergender, knfighterweightclass

from knfighterwhere knfightergender ='F' and knfighterweightclass <= ALL ( select knfighterweightclass from knfighter where knfightergender ='F');

this is equivalent to …

WHERE knfightergender = (select min(knfighterweightclass);

Page 46: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

46

ANY operator

List federation names with at least ‘one’ female trainer

Select distinct knfederationname From kntrainerWhere knfederationname = ANY (select knfederationname from kntrainer where kntrainergender ='F');

knfederationname

ITF

WTF

Page 47: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

47

In: an Alternate to ANY

List federation names with at least ‘one’ female trainer

Select distinct knfederationname From kntrainer

Where knfederationname IN (select knfederationname from kntrainer where

kntrainergender ='F');

knfederationname

ITF

WTF

Page 48: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

48

Left Outer Join

KnClubid KnTrainerid KnTrainername knTrainerbirth KnTrainergender KnFederationname KnClubname

1 100001 Hannes Mariz 1977 M ITF Black Tiger Tae-Kwon-Do

2 100002 Young Kawan 1967 M ITF Sydney Fight Club

2 100006 Bruce Perrot 1945 M ITF Sydney Fight Club

Where the table will always contain all the records of the left table regardless of whether matching records of the right table exist or not. In the case where there is no data in that particular field of the right table, null placed in the table.

The query for a left outer join where the table KnTrainer is the left table and KnClub is the right, i.e. KnCub is being joined to the KnTrainer using a common table i.e. KnClubid, would be as follows:

Select * from KnTrainer left join KnClub using (KnClubid);

Page 49: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

49

Right outer Join

A right outer join works in exactly the same way as a left outer join however the tables will be reversed.

In that, it will be the right table that is the “main” table or the table where every record will be joined at least once and where, in the left table there are no matching rows, a null will be placed in the left table for those rows.

In the case of this database, there are no null values and thus the two tables will be exactly the same.

Page 50: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

50

Self-Join

• Join a table to itself• Usually involve a self-referencing

relationship• Useful to find relationships among rows of the same table emp

supervises

Page 51: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

51

Querying a recursive relationship

SELECT distinct t1.kntrainername, t2.kntrainername AS trainername, t1.knclubID, t2.knclubID AS ClubID

FROM kntrainer t1, kntrainer t2WHERE t1.kntrainername = t2.kntrainernameAND t1.knclubID = t2. knClubID;

kntrainername Trianername KnclubID ClubID

Andre Aggassi Andre Aggassi 2 2

Bruce Perrot Bruce Perrot 2 2

Chelsea Lai Chelsea Lai 10 10

Claudia Schiffer Claudia Schiffer 7 7

Hannes Mariz Hannes Mariz 1 1

Ken Ferret Ken Ferret 6 6

Kim Kwak Kim Kwak 10 10

Matthias Sammer Matthias Sammer 10 10

Page 52: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

Data Integrity with SQL

Page 53: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

53

Foreign Keys KnClubID KnClubName

1 Black Tiger Tae-Kwon-Do

2 Sydney Fight Club

3 Do-Kwan-Dan

4 Budo Club Toronto

5 1. TKD e.V. Freiburg

6 Hiko-Dotan

7 Wollongong Crows

8 Korean National Team

9 Dobok-Hin-Dokan

10 Matthias Knecht TKD Team

KnTrainerID KnTrainerName KnTrainerBirth KnTrainerGender KnClubID KnTrainerID

100001 Hannes Mariz 1977 M 1 ITF

100002 Young Kawan 1967 M 2 ITF

100003 Liz Tough 1946 F 3 ITF

100004 Lollo Ingenheim 1975 F 4 ITF

100005 Kwan Do Inkuban 1934 M 5 ITF

100006 Ken Ferret 1963 M 6 WTF

100007 Claudia Schiffer 1953 F 7 WTF

100008 Mr. Pink 1966 M 8 WTF

100009 Lara Croft 1975 F 9 WTF

100010 Chelsea Lai 1983 F 10 WTF

100011 Matthias Sammer 1963 M 10 WTF

100012 Kim Kwak 1967 M 10 WTF

100013 Kwak Kum Sik 1985 M 6 WTF

100014 Susi Ingelheimer 1947 F 3 ITF

100015 Andre Agassi 1985 M 2 ITF

100016 Bruce Perrot 1945 M 2 ITF

Foreign Key

KnTrainer Table

KnClub Table

from KnClub Table

Primary Key

Page 54: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

54

Creating the linked tables-----------------------------------------------------------------------------------------------------------------CREATE TABLE KnClub (

KnClubID INTEGER NOT NULL,KnClubName VARCHAR(30),

CONSTRAINT PKKnClub PRIMARY KEY (KnClubID));

-----------------------------------------------------------------------------------------------------------------

CREATE TABLE KnTrainer (KnTrainerID INTEGER NOT NULL,KnTrainerName CHAR(30),KnTrainerBirth INTEGER,KnTrainerGender CHAR(1),KnClubID INTEGER NOT NULL,KnFederationName CHAR(3) NOT NULL,

CONSTRAINT PKKnTrainer PRIMARY KEY (KnTrainerID),CONSTRAINT FKKnClubID FOREIGN KEY (KnClubID) REFERENCES KnClub

ON UPDATE CASCADE,

As well as other constraints that are needed…[excluded here]);

-----------------------------------------------------------------------------------------------------------------

Page 55: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

55

CREATE TABLE KnFighter

( KnFighterID, KnFighterName, KnFighterBirth, KnFighterGender, KnFighterWeightclass

CONSTRAINT PKKnFighter PRIMARY KEY (KnFighterID),

CONSTRAINT ValidKnFighterGender CHECK (KnFighterGender IN ('M', 'F')),

CONSTRAINT ValidKnFighterBirth CHECK (KnFighterBirth BETWEEN 1900 AND 2007),

CONSTRAINT ValidKnFighterWeightclass CHECK (KnFighterWeightclass BETWEEN 1 AND 7),

CONSTRAINT ValidFighterID CHECK (KnFighterID LIKE '2%')

);

CHECK Statements

Page 56: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

56

SQL Syntax for ActionsCREATE TABLE KnTraining

( KnFighterID INTEGERNOT NULL,

KnTrainerID INTEGERNOT NULL,

CONSTRAINT PKKnTraining PRIMARY KEY(KnFighterID, KnTrainerID),

CONSTRAINT FKKnFighterID FOREIGN KEY (KnFighterID) REFERENCES KnFighter

ON DELETE RESTRICT

ON UPDATE CASCADE,

CONSTRAINT FKKnTrainerID FOREIGN KEY (KnTrainerID) REFERENCES KnTrainer

ON DELETE RESTRICT

ON UPDATE CASCADE,

CONSTRAINT ValidTraining CHECK (KnFighterID <> KnTrainerID)

);

So here, a deletion of a KnFighterID entry in the KnFighter table WONT affect this table – i.e. the fighter will still be displayed in the KnTraining Table

An update to an entry in KnTrainer ID in the tabe KnTrainer, will be followed through to this table too. i..e changes made in that table are also reflected here.

Page 57: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

Normalization

Page 58: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

58

An example of 1st normal form

KnFighterID

KnTrainerID

KnFightername

KnFighterweightclass

KnClubID

KnTrainerName KnTrainerBirth

200006 100009 Elisabeth Brown 3 9 Lara Croft 1975

200012 100008 Chels McGyvan 4 8 Claudia Schiffer 1953

200006 100015 Elisabeth Brown 3 2 Andre Agassi 1985

200012 100009 Chels McGyvan 4 9 Lara Croft 1975

The primary key

KnFighterID KnFighterName, KnFighterGender, KnFighterWeightclass

KnTrainerID KnTrainerName, KnTrainerBirth

KnTrainerID, KnFighterID KnClubID

key

Solution: split into two or more Tables.

This is basically where redundancies and more than one primary key exist in the table

Page 59: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

59

Solution to 1st normal form: Second Normal Form

KnFighterID KnFightername KnFighterGender KnFighterweightclass

200006 Elisabeth Brown F 3

200012 Chels McGyvan F 4

200006 Elisabeth Brown F 3

200012 Chels McGyvan F 4

KnFighterID KnTrainerID

200006 100009

200012 100008

200006 100015

200012 100009

KnTrainerID KnTrainerName KnTrainerBirth

100009 Lara Croft 1975

100008 Claudia Schiffer 1953

100015 Andre Agassi 1985

100009 Lara Croft 1975

The respective primary keys of each table

Different tables have been created to reduce redundancies in the table itself. Creating a table which joins the KnFighter Table to the KnTrainer table allows respective information of each table to be stored in it only.

Page 60: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

60

An example of 2nd normal form

The primary key

KnTrainerID KnTrainerName, KnTrainerBirth

KnClubID KnClubName key

Solution: split into two or more Tables.

KnTrainerID KnTrainerName KnTrainerBirth KnClubID KnClubName

100009 Lara Croft 1975 9 Dobok-Hin-Doken

100008 Claudia Schiffer 1953 8 Korean National Team

100015 Andre Agassi 1985 2 Sydney Fight Club

100009 Lara Croft 1975 9 Dobok-Hin-Doken

Is where redundancies in the form of extra information about the foreign keys exist

Page 61: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

61

Solution to 2nd normal form: Third Normal Form

KnClubID KnClubName

9 Dobok-Hin-Doken

8 Korean National Team

2 Sydney Fight Club

9 Dobok-Hin-Doken

The respective primary keys of each table

Leaving only the KnClubID in the KnTrainer table removes also having the names of those trainers in that table. This makes it efficient as there is only one ID to a trainer therefore removing redundant data. In other words only information strictly related to the respective table apart from the foreign keys is left.

KnTrainerID KnTrainerName KnTrainerBirth KnClubID

100009 Lara Croft 1975 9

100008 Claudia Schiffer 1953 8

100015 Andre Agassi 1985 2

100009 Lara Croft 1975 9

Page 62: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

62

Boyce-Codd normal form (BCNF)The basic principle of BCNF is that it does not allow multiple primary keys to

exist in an entity when only one is needed to identify the relation. As this creates multiple dependencies for an entity for one relation and thus creating redundant data.

The Mattias Knect Tae Kwon Do database is in the 3NF, however since there are no functional dependencies it also complies with BCNF.

An example of where the database would not comply with BCNF is when for example:If the Training table also included KnTrainerName and KnFighterName in addition to the IDs of each and these two were also the primary keys.

Basically making it so that the primary key is related to each attributive keys

Key

Page 63: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

63

An Example of Creating a View

CREATE VIEW fighterweight AS SELECT knFighterID, knFighterName, knFighterWeightClass FROM knFighterWHERE knFighterWeightClass >3;

Page 64: The Matthias Knecht Tae Kwon Do Case Study BHAMBRA Manjyot KOHLI Garima MANGAL Storay TAWHEED Zureen Distinction Assignment, Autumn 2007

64

An Example of Querying a View• Query exactly as if a

table SELECT knFighterName,

knfighterWeightClass

FROM fighterweight;

Knfightername knfighterweightclass

Karl Dall 5

Martin Knecht 5

Liz Taylor 5

Lars Augenstein 4

Lisa Mueller 4

Larry Wahnhold 4

Chin Moban 4

Chels McGyvan 4

Isidor Kraus 4

Mario Kleinschmid 4

Jana Kralle 5

Ingwan Young Kam 5