-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimportation.sql
More file actions
167 lines (124 loc) · 3.68 KB
/
Copy pathimportation.sql
File metadata and controls
167 lines (124 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
--EX2--
-- Supression de la table import existante
Drop Table import;
-- Creation de la table import
Create Table import(
ID int,
Name text,
Sex char(1),
Age int,
Heigth int ,
Weight float,
Team text,
NOC char(3),
Games text,
Year int,
Season text,
City text,
Sport text,
Event text,
Medal text
);
-- Importation des données du fichier athlete_events.csv dans la table import
\echo ''Importation des données du fichier athlete_events.csv dans la table import''
\Copy import from 'athlete_events.csv' with csv delimiter ',' header null as 'NA';
-- Affichage du nombre de ligne initial dans la table import
Select Count(*) From import;
-- Supression des données antérieur à 1920
Delete From import Where year < 1920;
Delete From import Where Sport = 'Art Competitions';
-- Affichage du nombre de ligne dans la table import
Select Count(*) From import;
-- Supression de la table noc existante
Drop table noc Cascade;
-- Creation de la table noc
Create Table noc(
NOC char(3),
region text,
notes text Default NULL,
Constraint pk_noc Primary Key (NOC)
);
-- Importation des données du fichier noc_regions.csv dans la table noc
\echo ''Importation des données du fichier noc_regions.csv dans la table noc''
\Copy noc from 'noc_regions.csv' with csv delimiter ',' header;
--EX4--
--Q1
--Supression des tables à créer, correspondant au MLD
Drop Table athlete Cascade;
Drop Table edition Cascade;
Drop Table event Cascade;
Drop Table resultat Cascade;
--Création des tables correspondant au MLD
Create Table athlete(
ano serial,
Name text,
Sex char(1),
Constraint pk_athlete Primary Key (ano)
);
Create Table edition(
edno serial,
year int,
season text,
city text,
Constraint pk_edition Primary Key (edno)
);
Create Table event(
evno serial,
sport text,
event text,
Constraint pk_event Primary Key (evno)
);
Create Table resultat(
ano int,
noc char(3),
evno int,
edno int,
age int,
heigth int,
weight int,
medal text,
Constraint pk_resultat Primary Key (ano,noc,evno,edno,age,heigth,weight),
Constraint fk_ano Foreign Key (ano)
References athlete(ano)
On Update Cascade,
Constraint fk_noc Foreign Key (noc)
References noc(NOC)
On Update Cascade,
Constraint fk_evno Foreign Key (evno)
References event(evno)
On Update Cascade,
Constraint fk_edno Foreign Key (edno)
References edition(edno)
On Update Cascade
);
--Insertion des données dans les tables créées précedemment
Insert Into athlete (Name,Sex)
Select Distinct Name,Sex
From import
Where Name is not NULL
And Sex is not NULL;
Insert Into edition (year,season,city)
Select Distinct Year,Season,City
From import
Where Year is not NULL
And Season is not NULL
And City is not NULL;
Insert Into event (sport,event)
Select Distinct Sport,Event
From import
Where Sport is not NULL
And Event is not NULL;
Insert Into resultat (ano,noc,evno,edno,age,heigth,weight,medal)
Select ano,noc.noc,evno,edno,age,heigth,weight,medal
From athlete as a, edition as ed, event as ev, noc, import as i
Where a.name = i.Name
And a.sex = i.Sex
And ed.year = i.Year
And ed.season = i.Season
And ed.city = i.City
And ev.sport = i.Sport
And ev.event = i.Event
And noc.noc = i.NOC
And age is not null
And heigth is not null
And weight is not null;