Match-ID

Transcription

Match-ID
SQL: Structured Query Language
SQL is a database manipulation language developped by
IBM (San José, 1981)
Structured Query Language:
Can be written by humans
Can be saved in files
Can be exchanged between applications
Structured Query Language:
Can be automatically processed by machines
SQL is a standard but there exist dialects (Access,
MySQL), i.e., non-standard variants
2
Ex.: a Rugby Database
Player
Team
Team-ID
TeamName
Country
Coach
...
Match
Player-ID
Team-ID
FirstName
LastName
Position
Match-ID
Team1-ID
Team2-ID
Date
Score1
Score2
Venue
NbSpectators
Has-Played
Player-ID
Match-ID
NbPoints
Team(Team-ID, TeamName, Country, Coach)
Player(Player-ID, Team-ID, FirstName, LastName, Position)
Match(Match-ID, Team1-ID, Team2-ID, Date, Score1, Score2, Venue, NbSpectators)
Has-Played(Player-ID, Match-ID, NbPoints)
3
Extension (content of the database)
Table Team
Team-ID
TeamName
Country
Coach
FRA
XV de France
France
Laporte
NZL
All Blacks
New Zealand
Henry
ENG
XV of the Rose
England
Ashton
AUS
Wallabies
Australia
Connolly
RSA
Spring Boks
South Africa
White
ARG
Pumas
Argentina
Loffreda
Scotland
Hadden
Fidgi
Tabua
SCO
FJI
Fidgi
Table Match
Match-ID Team1-ID Team2-ID
Date
Score1
Score2
Venue
NbSpectators
41
FRA
NZL
06/10/2007
20
18
Cardiff
73350
42
AUS
ENG
06/10/2007
10
12
Marseille
59500
43
RSA
FJI
07/10/2007
37
20
Marseille
59500
44
ARG
SCO
07/10/2007
19
13
Saint Denis
80000
45
ENG
FRA
13/10/2007
14
9
Saint Denis
80000
46
RSA
ARG
14/10/2007
37
13
Saint Denis
80000
47
FRA
ARG
19/10/2007
10
34
Paris
47870
48
RSA
ENG
20/10/2007
15
6
Saint Denis
80000
4
Extension (contd.)
Table Has-Played
Table Player
Player-ID
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Team-ID
FirstName
LastName
Position
FRA
FRA
FRA
FRA
FRA
NZL
NZL
NZL
NZL
ARG
ARG
ARG
ARG
AUS
AUS
AUS
ENG
RSA
RSA
FJI
SCO
Jean-Baptiste
Vincent
Lionel
Lionel
Sébastien
Joe
Doug
Dan
Nick
Felipe
Juan Martin
Lucas
Ignacio
Matt
Stirling
Drew
Jonny
Bryan
Percy
Nicky
Alasdair
Elissalde
Clerc
Beauxis
Nallet
Chabal
Rokocoko
Howlett
Carter
Evans
Contepomi
Hernandez
Borges
Corleto
Gitean
Mortlock
Mitchel
Wilkinson
Habana
Montgomery
Little
Dickinson
scrumhalf
wing
flyhalf
lock
back row
wing
wing
flyhalf
flyhalf
flyhalf
fullback
wing
wing
...
...
...
...
...
...
...
...
5
Player-ID Match-ID NbPoints
1
1
1
2
2
2
3
6
7
8
9
17
17
17
18
18
18
19
19
19
...
41
45
47
41
45
47
41
41
41
41
41
42
45
48
43
46
48
43
46
48
...
21
15
8
12
6
7
10
12
15
20
25
14
22
16
10
11
9
24
22
30
...
Simple Queries
General form:
SELECT <listOfColumns>
FROM <listOfTables>;
-- This is the SELECT clause
-- This is the FROM clause
DO NOT FORGET THIS!
6
Simple Queries
General form:
SELECT <listOfColumns>
FROM <listOfTables>;
Ex.: list of all players
SELECT *
FROM Player;
-- '*' means "all columns"
Player-ID
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
7
Team-ID
FirstName
LastName
Position
FRA
FRA
FRA
FRA
FRA
NZL
NZL
NZL
NZL
ARG
ARG
ARG
ARG
AUS
AUS
AUS
ENG
RSA
RSA
FJI
SCO
Jean-Baptiste
Vincent
Lionel
Lionel
Sébastien
Joe
Doug
Dan
Nick
Felipe
Juan Martin
Lucas
Ignacio
Matt
Stirling
Drew
Jonny
Bryan
Percy
Nicky
Alasdair
Elissalde
Clerc
Beauxis
Nallet
Chabal
Rokocoko
Howlett
Carter
Evans
Contepomi
Hernandez
Borges
Corleto
Gitean
Mortlock
Mitchel
Wilkinson
Habana
Montgomery
Little
Dickinson
scrumhalf
wing
flyhalf
lock
back row
wing
wing
flyhalf
flyhalf
flyhalf
fullback
wing
wing
...
...
...
...
...
...
...
...
Simple Queries
General form:
SELECT <listOfColumns>
FROM <listOfTables>;
Ex.: list of all players
SELECT *
FROM Player;
In relation algebra:
Player
Player-ID
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
8
Team-ID
FirstName
LastName
Position
FRA
FRA
FRA
FRA
FRA
NZL
NZL
NZL
NZL
ARG
ARG
ARG
ARG
AUS
AUS
AUS
ENG
RSA
RSA
FJI
SCO
Jean-Baptiste
Vincent
Lionel
Lionel
Sébastien
Joe
Doug
Dan
Nick
Felipe
Juan Martin
Lucas
Ignacio
Matt
Stirling
Drew
Jonny
Bryan
Percy
Nicky
Alasdair
Elissalde
Clerc
Beauxis
Nallet
Chabal
Rokocoko
Howlett
Carter
Evans
Contepomi
Hernandez
Borges
Corleto
Gitean
Mortlock
Mitchel
Wilkinson
Habana
Montgomery
Little
Dickinson
scrumhalf
wing
flyhalf
lock
back row
wing
wing
flyhalf
flyhalf
flyhalf
fullback
wing
wing
...
...
...
...
...
...
...
...
Projection
Projection on columns: name the columns in the SELECT
Position
clause (there can be duplicates)
Ex.: list all positions of players
SELECT position
FROM Player;
9
scrumhalf
wing
flyhalf
lock
back row
wing
wing
flyhalf
flyhalf
flyhalf
fullback
wing
wing
...
...
...
...
...
...
...
...
Projection
Projection on columns: name the columns in the SELECT
Position
clause (there can be duplicates)
Ex.: list all positions of players
SELECT position
FROM Player;
In relation algebra:
πposition ( Player )
10
scrumhalf
wing
flyhalf
lock
back row
wing
wing
flyhalf
flyhalf
flyhalf
fullback
wing
wing
...
...
...
...
...
...
...
...
Projection
Projection on columns: name the columns in the SELECT
Position
clause (there can be duplicates)
Ex.: list all positions of players
SELECT position
FROM Player;
In relation algebra:
πposition ( Player )
Projection without duplicates
SELECT DISTINCT position
FROM Player;
11
Position
scrumhalf
wing
flyhalf
lock
back row
fullback
...
scrumhalf
wing
flyhalf
lock
back row
wing
wing
flyhalf
flyhalf
flyhalf
fullback
wing
wing
...
...
...
...
...
...
...
...
Selection
SELECT <listOfColumns>
FROM <listOfTables>
WHERE <condition>;
-- Projection (the SELECT clause)
-- Selection (the WHERE clause)
12
Selection
SELECT <listOfColumns>
FROM <listOfTables>
WHERE <condition>;
-- Projection (the SELECT clause)
-- Selection (the WHERE clause)
Ex.: list of French players' names
SELECT FirstName, LastName
FROM Player
WHERE Team-ID = 'FRA';
13
FirstName
LastName
Jean-Baptiste
Vincent
Lionel
Lionel
Sébastien
Elissalde
Clerc
Beauxis
Nallet
Chabal
Selection
SELECT <listOfColumns>
FROM <listOfTables>
WHERE <condition>;
-- Projection (the SELECT clause)
-- Selection (the WHERE clause)
Ex.: list of French players' names
SELECT FirstName, LastName
FROM Player
WHERE Team-ID = 'FRA';
FirstName
LastName
Jean-Baptiste
Vincent
Lionel
Lionel
Sébastien
Elissalde
Clerc
Beauxis
Nallet
Chabal
In relational algebra:
πFirstName,LastName( σTeam-ID='FRA' ( Player ))
14
Ordering Results
SELECT <listOfColumns>
FROM <listOfTables>
WHERE <condition>
ORDER BY <column> [<order>] [,<column> [<order>]] …
-- <order> is either ASC or DESC (ascending or descending)
15
Ordering Results
SELECT <listOfColumns>
FROM <listOfTables>
WHERE <condition>
ORDER BY <column> [<order>] [,<column> [<order>]] …
-- <order> is either ASC or DESC (ascending or descending)
Ex.: list of French players' names, alphabetically ordered
SELECT FirstName, LastName
FROM Player
WHERE Team-ID = 'FRA'
ORDER BY LastName ASC;
16
FirstName
LastName
Lionel
Sébastien
Vincent
Jean-Baptiste
Lionel
Beauxis
Chabal
Clerc
Elissalde
Nallet
Combination of Conditions
Boolean connectors: AND, OR, NOT (negation)
Comparison operators: =, <, >, <=, >=, <>
Ex.: matches played by the French team
SELECT Match-ID,Team1-ID,Team2-ID,Date,NbSpectators
FROM Match
WHERE Team1-ID = 'FRA' OR Team2-ID = 'FRA';
Match-ID Team1-ID Team2-ID
Date
NbSpectators
41
FRA
NZL
06/10/2007
7335
45
ENG
FRA
13/10/2007
8000
47
FRA
ARG
19/10/2007
4787
17
Combination of Conditions
Boolean connectors: AND, OR, NOT (negation)
Comparison operators: =, <, >, <=, >=, <>
Ex.: matches played by the French team
SELECT Match-ID,Team1-ID,Team2-ID,Date,NbSpectators
FROM Match
WHERE Team1-ID = 'FRA' OR Team2-ID = 'FRA';
Match-ID Team1-ID Team2-ID
Date
NbSpectators
41
FRA
NZL
06/10/2007
73350
45
ENG
FRA
13/10/2007
80000
47
FRA
ARG
19/10/2007
47870
In relational algebra:
π
(
Match-ID,Team1-ID,Team2-ID,Date,NbSpectators
σ
18
(Match))
Team1-ID='FRA' or Team2-ID='FRA'
Combination of Conditions (contd.)
Ex.: List of matches played by the French team and having
more than 60000 spectators
SELECT Match-ID,Team1-ID,Team2-ID,Date,NbSpectators
FROM Match
WHERE (Team1-ID = 'FRA' OR Team2-ID = 'FRA')
AND NbSpectators > 60000;
Match-ID Team1-ID Team2-ID
Date
NbSpectators
41
FRA
NZL
06/10/2007
73350
45
ENG
FRA
13/10/2007
80000
19
Combination of Conditions (contd.)
Ex.: List of matches played by the French team and having
more than 60000 spectators
SELECT Match-ID,Team1-ID,Team2-ID,Date,NbSpectators
FROM Match
WHERE (Team1-ID = 'FRA' OR Team2-ID = 'FRA')
AND NbSpectators > 60000;
Match-ID Team1-ID Team2-ID
Date
NbSpectators
41
FRA
NZL
06/10/2007
73350
45
ENG
FRA
13/10/2007
80000
In relational algebra:
π
Match-ID,Team1-ID,Team2-ID,Date,NbSpectators
(σ
(Team1-ID='FRA' or Team2-ID='FRA') and NbSpectators>60000
20
(Match)
)
Special Conditions
The "empty" value NULL can be tested with a special syntax:
SELECT <listOfColumns>
FROM <listOfTables>
WHERE <columnName> IS NULL;
or
SELECT <listOfColumns>
FROM <listOfTables>
WHERE <columnName> IS NOT NULL;
!
Do not write:
WHERE <columnName> = NULL;
21
Cartesian Product
Name the tables in the FROM clause
First player
SELECT *
FROM Player,Has-Played;
Player-ID
1
1
1
1
1
1
1
1
1
1
...
2
2
2
2
Team-ID
FirstName
LastName
Position
Player-ID
Match-ID
NbPoints
FRA
FRA
FRA
FRA
FRA
FRA
FRA
FRA
FRA
FRA
...
FRA
FRA
FRA
FRA
Jean-Baptiste
Jean-Baptiste
Jean-Baptiste
Jean-Baptiste
Jean-Baptiste
Jean-Baptiste
Jean-Baptiste
Jean-Baptiste
Jean-Baptiste
Jean-Baptiste
...
Vincent
Vincent
Vincent
Vincent
Elissalde
Elissalde
Elissalde
Elissalde
Elissalde
Elissalde
Elissalde
Elissalde
Elissalde
Elissalde
...
Clerc
Clerc
Clerc
Clerc
scrumhalf
scrumhalf
scrumhalf
scrumhalf
scrumhalf
scrumhalf
scrumhalf
scrumhalf
scrumhalf
scrumhalf
...
wing
wing
wing
wing
1
1
1
2
2
2
3
6
7
8
...
1
1
1
2
41
45
47
41
45
47
41
41
41
41
...
41
45
47
41
21
15
8
12
6
7
10
12
15
20
...
21
15
8
12
In relation algebra:
Player ⊗ Has-Played
Second player
22
Join
Cartesian product followed by a selection
Ex.: List of players who played match #41
SELECT Team-ID,LastName,Match-ID
FROM Player,Has-Played
WHERE Player.Player-ID = Has-Played.Player-ID
AND Match-ID = 41;
Team-ID
LastName
Match-ID
FRA
FRA
FRA
NZL
NZL
NZL
NZL
Elissalde
Clerc
Beauxis
Rokocoko
Howlett
Carter
Evans
41
41
41
41
41
41
41
23
Join
Cartesian product followed by a selection
Ex.: List of players who played match #41
SELECT Team-ID,LastName,Match-ID
FROM Player,Has-Played
WHERE Player.Player-ID = Has-Played.Player-ID
AND Match-ID = 41;
Team-ID
LastName
Match-ID
FRA
FRA
FRA
NZL
NZL
NZL
NZL
Elissalde
Clerc
Beauxis
Rokocoko
Howlett
Carter
Evans
41
41
41
41
41
41
41
In relational algebra
πTeam-ID,LastName,Match-ID
)
24
Join of more tables
Ex.: Points scored by the All Blacks players
SELECT
FROM
WHERE
AND
AND
AND
LastName,TeamName,Match-ID,Date,
Team1-ID,Team2-ID,NbPoints
Match,Player,Has-Played,Team
Player.Player-ID = Has-Played.Player-ID
Match.Match-ID = Has-Played.Match-ID
Team.Team-ID = Player.Team-ID
TeamName = 'All Blacks';
LastName
TeamName
Match-ID
Date
Team1-ID
Team2-ID
NbPoints
Rokocoko
Howlett
Carter
Evans
All Blacks
All Blacks
All Blacks
All Blacks
41
41
41
41
06/10/07
06/10/07
06/10/07
06/10/07
FRA
FRA
FRA
FRA
NZL
NZL
NZL
NZL
12
15
20
25
25
Join of more tables
Ex.: Points scored by the All Blacks players
SELECT
FROM
WHERE
AND
AND
AND
LastName,TeamName,Match-ID,Date,
Team1-ID,Team2-ID,NbPoints
Match,Player,Has-Played,Team
Player.Player-ID = Has-Played.Player-ID
Match.Match-ID = Has-Played.Match-ID
Team.Team-ID = Player.Team-ID
TeamName = 'All Blacks';
LastName
TeamName
Match-ID
Date
Team1-ID
Team2-ID
NbPoints
Rokocoko
Howlett
Carter
Evans
All Blacks
All Blacks
All Blacks
All Blacks
41
41
41
41
06/10/07
06/10/07
06/10/07
06/10/07
FRA
FRA
FRA
FRA
NZL
NZL
NZL
NZL
12
15
20
25
In relational algebra
πLastName,TeamName,Match-ID,Date,Team1-ID,Team2-ID,NbPoints
(σPlayer.Player-ID=Has-Played.Player-ID(Match⊗Player⊗Has-Played⊗Team)
and Match.Match-ID=Has-Played.Match-ID and Team.Team-ID=Player.Team-ID and TeamName="All Blacks"
26
Nested Queries
By use of set-operators:
IN
NOT IN
–- set membership
–- set non-membership
IN is an operator that determine whether the value of an expression is
equal to one or more values belonging to a set.
Ex.: List of players who played match #41, with projection
onto the player's last name
LastName
Elissalde
Clerc
Beauxis
Rokocoko
Howlett
Carter
Evans
SELECT LastName
FROM Player
WHERE Player-ID IN
(SELECT Player-ID
FROM Has-Played
WHERE Match-ID = 41);
27
Nested Queries (contd.)
NOT IN
–- set non-membership
NOT IN is often used instead of the difference operation
Ex.: Players who have not played any match at all
SELECT Player-ID
FROM Player
WHERE Player-ID NOT IN
(SELECT Player-ID
FROM Has-Played);
Player-ID
4
5
10
11
12
13
14
15
16
20
21
28
Nested Queries (contd.)
NOT IN
–- set non-membership
NOT IN is often used instead of the difference operation
Ex.: Players who have not played any match at all
SELECT Player-ID
FROM Player
WHERE Player-ID NOT IN
(SELECT Player-ID
FROM Has-Played);
Player-ID
In relational algebra
πPlayer-ID(Player) – πPlayer-ID(Has-Played)
29
4
5
10
11
12
13
14
15
16
20
21
Other SQL operations
selection of rows that belong to either the first or the
second relation (with deletion of duplicates).
UNION
Ex.: Names of players and coaches of the French team
(SELECT LastName
FROM Player
WHERE Team-ID = 'FRA')
UNION
(SELECT Coach
FROM Team
WHERE Team-ID = 'FRA');
LastName
Elissalde
Clerc
Beauxis
Nallet
Chabal
Laporte
30
Other SQL operations
selection of rows that belong to either the first or the
second relation (with deletion of duplicates).
UNION
Ex.: Names of players and coaches of the French team
(SELECT LastName
FROM Player
WHERE Team-ID = 'FRA')
UNION
(SELECT Coach
FROM Team
WHERE Team-ID = 'FRA');
LastName
Elissalde
Clerc
Beauxis
Nallet
Chabal
Laporte
Other operators:
INTERSECTION
selection of rows that belong to both relations
MINUS
selection of the rows of one relation which do
not belong to the second one (difference)
31
Aggregates
COUNT
MAX
MIN
SUM
AVG
Number of values of a set
Maximum value of a set of values
Minimum value of a set of values
Sum of a set of values
Average of a set of values
Ex.: Total number of players
SELECT COUNT(*) AS NbPlayer
FROM Player;
NbPlayer
21
32
Aggregates
COUNT
MAX
MIN
SUM
AVG
Number of values of a set
Maximum value of a set of values
Minimum value of a set of values
Sum of a set of values
Average of a set of values
Ex.: Total number of players
SELECT COUNT(*) AS NbPlayer
FROM Player;
NbPlayer
21
Ex.: Maximum number of spectators
SELECT MAX(NbSpectators) AS MaxAttendance
FROM Match;
33
MaxAttendance
80000
Aggregates (contd.)
COUNT
MAX
MIN
SUM
AVG
Number of values of a set
Maximum value of a set of values
Minimum value of a set of values
Sum of a set of values
Average of a set of values
Ex.: Average number of spectators
SELECT AVG(NbSpectators) AS AvgSpectators
FROM Match;
AvgSpectators
70027
Or
SELECT SUM(NbSpectators) / COUNT(*)
FROM Match;
Maths operations (+, -, *, /)
can be used
34
Partitioning relations
make a partition of the result, based on an
attribute; aggregates are computed for each
group in the partition
GROUP BY
Player-ID
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Team-ID
FirstName
LastName
Position
FRA
FRA
FRA
FRA
FRA
NZL
NZL
NZL
NZL
ARG
ARG
ARG
ARG
AUS
AUS
AUS
ENG
RSA
RSA
FJI
SCO
Jean-Baptiste
Vincent
Lionel
Lionel
Sébastien
Joe
Doug
Dan
Nick
Felipe
Juan Martin
Lucas
Ignacio
Matt
Stirling
Drew
Jonny
Bryan
Percy
Nicky
Alasdair
Elissalde
scrumhalf
Clerc
wing
Beauxis
flyhalf
Nallet
lock
Chabal
back row
Rokocoko
wing
Howlett
wing
Carter
flyhalf
Evans
flyhalf
Contepomi
flyhalf
Hernandez
fullback
Borges
wing
Corleto
wing
Gitean
...
Mortlock
...
Mitchel
...
Wilkinson
...
Habana
...
Montgomery
...
Little
...
35
Dickinson
...
SELECT COUNT(*)
AS NbPlayer
FROM Player
GROUP BY Team-ID;
Partitioning relations
make a partition of the result, based on an
attribute; aggregates are computed for each
group in the partition
GROUP BY
Player-ID
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Team-ID
FirstName
LastName
Position
FRA
FRA
FRA
FRA
FRA
NZL
NZL
NZL
NZL
ARG
ARG
ARG
ARG
AUS
AUS
AUS
ENG
RSA
RSA
FJI
SCO
Jean-Baptiste
Vincent
Lionel
Lionel
Sébastien
Joe
Doug
Dan
Nick
Felipe
Juan Martin
Lucas
Ignacio
Matt
Stirling
Drew
Jonny
Bryan
Percy
Nicky
Alasdair
Elissalde
scrumhalf
Clerc
wing
Beauxis
flyhalf
Nallet
lock
Chabal
back row
Rokocoko
wing
Howlett
wing
Carter
flyhalf
Evans
flyhalf
Contepomi
flyhalf
Hernandez
fullback
Borges
wing
Corleto
wing
Gitean
...
Mortlock
...
Mitchel
...
Wilkinson
...
Habana
...
Montgomery
...
Little
...
36
Dickinson
...
SELECT COUNT(*)
AS NbPlayer
FROM Player
GROUP BY Team-ID;
Partitioning relations
make a partition of the result, based on an
attribute; aggregates are computed for each
group in the partition
GROUP BY
Player-ID
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Team-ID
FirstName
LastName
Position
FRA
FRA
FRA
FRA
FRA
NZL
NZL
NZL
NZL
ARG
ARG
ARG
ARG
AUS
AUS
AUS
ENG
RSA
RSA
FJI
SCO
Jean-Baptiste
Vincent
Lionel
Lionel
Sébastien
Joe
Doug
Dan
Nick
Felipe
Juan Martin
Lucas
Ignacio
Matt
Stirling
Drew
Jonny
Bryan
Percy
Nicky
Alasdair
Elissalde
scrumhalf
Clerc
wing
Beauxis
flyhalf
Nallet
lock
Chabal
back row
Rokocoko
wing
Howlett
wing
Carter
flyhalf
Evans
flyhalf
Contepomi
flyhalf
Hernandez
fullback
Borges
wing
Corleto
wing
Gitean
...
Mortlock
...
Mitchel
...
Wilkinson
...
Habana
...
Montgomery
...
Little
...
37
Dickinson
...
SELECT COUNT(*)
AS NbPlayer
FROM Player
GROUP BY Team-ID;
This is a partition
Partitioning relations
make a partition of the result, based on an
attribute; aggregates are computed for each
group in the partition
GROUP BY
Player-ID
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Team-ID
FirstName
LastName
Position
FRA
FRA
FRA
FRA
FRA
NZL
NZL
NZL
NZL
ARG
ARG
ARG
ARG
AUS
AUS
AUS
ENG
RSA
RSA
FJI
SCO
Jean-Baptiste
Vincent
Lionel
Lionel
Sébastien
Joe
Doug
Dan
Nick
Felipe
Juan Martin
Lucas
Ignacio
Matt
Stirling
Drew
Jonny
Bryan
Percy
Nicky
Alasdair
Elissalde
scrumhalf
Clerc
wing
Beauxis
flyhalf
Nallet
lock
Chabal
back row
Rokocoko
wing
Howlett
wing
Carter
flyhalf
Evans
flyhalf
Contepomi
flyhalf
Hernandez
fullback
Borges
wing
Corleto
wing
Gitean
...
Mortlock
...
Mitchel
...
Wilkinson
...
Habana
...
Montgomery
...
Little
...
38
Dickinson
...
5
SELECT COUNT(*)
AS NbPlayer
FROM Player
GROUP BY Team-ID;
4
NbPlayer
4
3
1
2
1
1
5
4
4
3
1
2
1
1
Partitioning relations (contd.)
GROUP BY
make a partition of the result, based on an
attribute; aggregates are computed for each
group in the partition
Ex.: For each player list the sum of scored points
SELECT LastName,SUM(NbPoint) AS Points
FROM Has-Played,Player
WHERE Has-Played.Player-ID = Player.Player-ID
GROUP BY Player-ID;
LastName
Elissalde
Clerc
Beauxis
Rokocoko
Howlett
Carter
Evans
Wilkinson
Habana
Montgomery
39
Points
45
25
24
12
15
20
25
52
30
76
Partitioning relations (contd.)
HAVING
apply a condition on aggregates
Ex.: List total score for players having played more than 2
matches (and order by points scored)
SELECT LastName,SUM(NbPoint) AS Points
FROM Has-Played,Player
WHERE Has-Played.Player-ID = Player.Player-ID
GROUP BY Player-ID
LastName
HAVING COUNT(*) > 2
Elissalde
ORDER BY Points;
Clerc
Wilkinson
Habana
Montgomery
40
Points
45
25
52
30
76
Updating a database
Inserting a row:
INSERT INTO <tableName>[(<field1>,<field2>, … )]
VALUES (<value1>,<value2>, … );
Ex.: Insert new player Paterson
INSERT INTO Player(Player-ID, Team-ID, FirstName,
LastName, Position)
VALUES (22,'SCO','Chris','Paterson','wing');
41
Updating a database
Inserting a row:
INSERT INTO <tableName>[(<columnName>,<columnName>, … )]
VALUES (<value1>,<value2>, … );
Ex.: Insert new player Paterson
INSERT INTO Player(Player-ID, Team-ID, FirstName,
LastName, Position)
VALUES (22,'SCO','Chris','Paterson','wing');
Updating values of fields in an existing row:
UPDATE <tableName>
SET <columnName> = <value1>, <columnName> = <value2>, …
WHERE <condition>;
Ex.: Paterson changed position
UPDATE Player
SET Position = 'fullback'
WHERE Player-ID = 22;
42
Updating a database (contd.)
Deleting a row:
DELETE FROM <tableName>
WHERE <condition>;
Ex.: Chabal retired from rugby
DELETE FROM Player
WHERE Player-ID = 5;
43
Tables Management
Creating tables:
CREATE TABLE <tableName>
(<column1> <description>, <column2> <description>, …);
Ex.: table Match
CREATE TABLE Match
(Match-ID INTEGER PRIMARY KEY,
Team1-ID TEXT FOREIGN KEY REFERENCES Team(Team-ID),
Team2-ID TEXT FOREIGN KEY REFERENCES Team(Team-ID),
Date DATETIME,
Score1 INTEGER,
Score2 INTEGER,
Venue TEXT,
NbSpectators INTEGER);
44
Tables Management
Deleting tables:
DROP <tableName>;
Creating/deleting an index,
adding integrity constraints,
manageing multiple databases,
setting default values
and much more...
CREATE [UNIQUE] INDEX <indexName>
ON <tableName> (<columnName>, … );
DROP INDEX <indexName>;
(column_definition) CONSTRAINT
<constraintName> (some_other_stuff)
( CREATE | DROP ) SCHEMA <schemaName>;
(column_definition) DEFAULT <defaultValue>
VIEW | TRANSACTION | DOMAIN | CHECK | …
SQL is a complete programming language!
45

Similar documents