- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateScript.sql
More file actions
Latest commit
213 lines (180 loc) · 8.12 KB
/
Copy pathcreateScript.sql
File metadata and controls
213 lines (180 loc) · 8.12 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
-- Remove conflicting tables
-- remove function for removing tables and sequences
DROPFUNCTION IF EXISTS remove_all();
-- create function for removing tables and sequences
CREATE or replaceFUNCTIONremove_all() RETURNS void AS $$
DECLARE
rec RECORD;
cmd text;
BEGIN
cmd :='';
FOR rec INSELECT
'DROP SEQUENCE '|| quote_ident(n.nspname) ||'.'
|| quote_ident(c.relname) ||' CASCADE;'AS name
FROM
pg_catalog.pg_classAS c
LEFT JOIN
pg_catalog.pg_namespaceAS n
ON
n.oid=c.relnamespace
WHERE
relkind ='S'AND
n.nspname NOT IN ('pg_catalog', 'pg_toast') AND
pg_catalog.pg_table_is_visible(c.oid)
LOOP
cmd := cmd ||rec.name;
END LOOP;
FOR rec INSELECT
'DROP TABLE '|| quote_ident(n.nspname) ||'.'
|| quote_ident(c.relname) ||' CASCADE;'AS name
FROM
pg_catalog.pg_classAS c
LEFT JOIN
pg_catalog.pg_namespaceAS n
ON
n.oid=c.relnamespaceWHERE relkind ='r'AND
n.nspname NOT IN ('pg_catalog', 'pg_toast') AND
pg_catalog.pg_table_is_visible(c.oid)
LOOP
cmd := cmd ||rec.name;
END LOOP;
EXECUTE cmd;
RETURN;
END;
$$ LANGUAGE plpgsql;
select remove_all();
-- End of removing
CREATETABLEbiomass_power_plant (
id_biomass SERIALNOT NULL,
power DECIMAL(10, 2)
);
ALTERTABLE biomass_power_plant ADD CONSTRAINT pk_biomass_power_plant PRIMARY KEY (id_biomass);
CREATETABLEfuel (
id_fuel SERIALNOT NULL,
name VARCHAR(32) NOT NULL,
price REAL,
energy_density REAL
);
ALTERTABLE fuel ADD CONSTRAINT pk_fuel PRIMARY KEY (id_fuel);
CREATETABLEgrid_connection (
id_connection SERIALNOT NULL,
id_hydro INTEGER,
id_solar INTEGER,
id_wind INTEGER,
id_biomass INTEGER,
max_power DECIMAL(12, 2) NOT NULL,
start_up_time SMALLINTNOT NULL
);
ALTERTABLE grid_connection ADD CONSTRAINT pk_grid_connection PRIMARY KEY (id_connection);
ALTERTABLE grid_connection ADD CONSTRAINT u_fk_grid_connection_hydro_power UNIQUE (id_hydro);
ALTERTABLE grid_connection ADD CONSTRAINT u_fk_grid_connection_solar_power UNIQUE (id_solar);
ALTERTABLE grid_connection ADD CONSTRAINT u_fk_grid_connection_wind_power UNIQUE (id_wind);
ALTERTABLE grid_connection ADD CONSTRAINT u_fk_grid_connection_biomass_power UNIQUE (id_biomass);
CREATETABLEhydro_power_plant (
id_hydro SERIALNOT NULL,
id_river INTEGERNOT NULL,
head DECIMAL(6, 2)
);
ALTERTABLE hydro_power_plant ADD CONSTRAINT pk_hydro_power_plant PRIMARY KEY (id_hydro);
CREATETABLElocation (
id_location SERIALNOT NULL,
longitude DECIMAL(9, 6) NOT NULL,
latitude DECIMAL(8, 6) NOT NULL,
solar_constant DECIMAL(6, 2),
wind_speed DECIMAL(4, 2),
name VARCHAR(64)
);
ALTERTABLE location ADD CONSTRAINT pk_location PRIMARY KEY (id_location);
CREATETABLEreservoir (
id_reservoir SERIALNOT NULL,
id_river INTEGERNOT NULL,
capacity BIGINT
);
ALTERTABLE reservoir ADD CONSTRAINT pk_reservoir PRIMARY KEY (id_reservoir, id_river);
CREATETABLEriver (
id_river SERIALNOT NULL,
river_id_river INTEGER,
lowest_point DECIMAL(6, 2) NOT NULL,
flow_rate DECIMAL(8, 2)
);
ALTERTABLE river ADD CONSTRAINT pk_river PRIMARY KEY (id_river);
CREATETABLEsolar_panel (
panel_number SERIALNOT NULL,
id_solar INTEGERNOT NULL,
panel_power DECIMAL(6, 2),
panel_size DECIMAL(8, 4)
);
ALTERTABLE solar_panel ADD CONSTRAINT pk_solar_panel PRIMARY KEY (panel_number, id_solar);
CREATETABLEsolar_power_plant (
id_solar SERIALNOT NULL,
id_location INTEGERNOT NULL,
solar_type VARCHAR(32) NOT NULL,
total_area DECIMAL(6, 2) NOT NULL
);
ALTERTABLE solar_power_plant ADD CONSTRAINT pk_solar_power_plant PRIMARY KEY (id_solar);
ALTERTABLE solar_power_plant ADD CONSTRAINT u_fk_solar_power_plant_location UNIQUE (id_location);
CREATETABLEturbine (
hydro_turbine_number SERIALNOT NULL,
id_hydro INTEGERNOT NULL,
hydro_turbine_power DECIMAL(8, 2),
turbine_type VARCHAR(32)
);
ALTERTABLE turbine ADD CONSTRAINT pk_turbine PRIMARY KEY (hydro_turbine_number, id_hydro);
CREATETABLEwind_power_plant (
id_wind SERIALNOT NULL,
id_location INTEGERNOT NULL,
total_area DECIMAL(6, 2)
);
ALTERTABLE wind_power_plant ADD CONSTRAINT pk_wind_power_plant PRIMARY KEY (id_wind);
ALTERTABLE wind_power_plant ADD CONSTRAINT u_fk_wind_power_plant_location UNIQUE (id_location);
CREATETABLEwind_turbine (
wind_turbine_number SERIALNOT NULL,
id_wind INTEGERNOT NULL,
wind_turbine_power DECIMAL(6, 2),
height DECIMAL(5, 2)
);
ALTERTABLE wind_turbine ADD CONSTRAINT pk_wind_turbine PRIMARY KEY (wind_turbine_number, id_wind);
CREATETABLEfuel_biomass_power_plant (
id_fuel INTEGERNOT NULL,
id_biomass INTEGERNOT NULL
);
ALTERTABLE fuel_biomass_power_plant ADD CONSTRAINT pk_fuel_biomass_power_plant PRIMARY KEY (id_fuel, id_biomass);
ALTERTABLE grid_connection ADD CONSTRAINT fk_grid_connection_hydro_power_plant FOREIGN KEY (id_hydro) REFERENCES hydro_power_plant (id_hydro) ON DELETE CASCADE;
ALTERTABLE grid_connection ADD CONSTRAINT fk_grid_connection_solar_power_plant FOREIGN KEY (id_solar) REFERENCES solar_power_plant (id_solar) ON DELETE CASCADE;
ALTERTABLE grid_connection ADD CONSTRAINT fk_grid_connection_wind_power_plant FOREIGN KEY (id_wind) REFERENCES wind_power_plant (id_wind) ON DELETE CASCADE;
ALTERTABLE grid_connection ADD CONSTRAINT fk_grid_connection_biomass_power_plant FOREIGN KEY (id_biomass) REFERENCES biomass_power_plant (id_biomass) ON DELETE CASCADE;
ALTERTABLE hydro_power_plant ADD CONSTRAINT fk_hydro_power_plant_river FOREIGN KEY (id_river) REFERENCES river (id_river) ON DELETE CASCADE;
ALTERTABLE reservoir ADD CONSTRAINT fk_reservoir_river FOREIGN KEY (id_river) REFERENCES river (id_river) ON DELETE CASCADE;
ALTERTABLE river ADD CONSTRAINT fk_river_river FOREIGN KEY (river_id_river) REFERENCES river (id_river) ON DELETE CASCADE;
ALTERTABLE solar_panel ADD CONSTRAINT fk_solar_panel_solar_power_plant FOREIGN KEY (id_solar) REFERENCES solar_power_plant (id_solar) ON DELETE CASCADE;
ALTERTABLE solar_power_plant ADD CONSTRAINT fk_solar_power_plant_location FOREIGN KEY (id_location) REFERENCES location (id_location) ON DELETE CASCADE;
ALTERTABLE turbine ADD CONSTRAINT fk_turbine_hydro_power_plant FOREIGN KEY (id_hydro) REFERENCES hydro_power_plant (id_hydro) ON DELETE CASCADE;
ALTERTABLE wind_power_plant ADD CONSTRAINT fk_wind_power_plant_location FOREIGN KEY (id_location) REFERENCES location (id_location) ON DELETE CASCADE;
ALTERTABLE wind_turbine ADD CONSTRAINT fk_wind_turbine_wind_power_plant FOREIGN KEY (id_wind) REFERENCES wind_power_plant (id_wind) ON DELETE CASCADE;
ALTERTABLE fuel_biomass_power_plant ADD CONSTRAINT fk_fuel_biomass_power_plant_fuel FOREIGN KEY (id_fuel) REFERENCES fuel (id_fuel) ON DELETE CASCADE;
ALTERTABLE fuel_biomass_power_plant ADD CONSTRAINT fk_fuel_biomass_power_plant_biomass FOREIGN KEY (id_biomass) REFERENCES biomass_power_plant (id_biomass) ON DELETE CASCADE;
ALTERTABLE grid_connection ADD CONSTRAINT xc_grid_connection_id_hydro_id_
CHECK ((id_hydro IS NOT NULLAND id_solar IS NULLAND id_wind IS NULLAND id_biomass IS NULL)
OR
(id_hydro IS NULLAND id_solar IS NOT NULLAND id_wind IS NULLAND id_biomass IS NULL)
OR
(id_hydro IS NULLAND id_solar IS NULLAND id_wind IS NOT NULLAND id_biomass IS NULL)
OR
(id_hydro IS NULLAND id_solar IS NULLAND id_wind IS NULLAND id_biomass IS NOT NULL));
create or replacefunctioncheckRivers() returns trigger
language plpgsql as
$$begin
if ( selectcount(*)
from river source join river sink on ( source.river_id_river=sink.id_river )
wheresource.lowest_point<=sink.lowest_point ) >0
then
raise exception 'River IO violated.';
end if;
return null;
end;$$;
DROPTRIGGER IF EXISTS river_low_river ON river;
create constraint trigger river_low_river
after insert ordeleteorupdate of river_id_river, lowest_point
on river
for each row
execute procedure checkRivers();