
-- update the corresponding vehicle data (in table vehicle)
-- after a vehicle use (in table useOfVehicle) has been inserted.

-- Consider the following table, which contains a new derived
-- column for the average gas consumption:
create table Car
(id   integer,
 kilometers     numeric(10,3),
 gasconsumption numeric(10,3),
 avggas         numeric(5,2));
insert into V2 select id, kilometers, gasconsumption, 0 from Vehicle;

-- Now define a trigger that updates the avggas column whenever
-- kilometers and/or gasconsumption are modified
create function ...
create trigger ...

-- useful commands for testing:
update Car set avgGas = 1 where id = 1000;
select kilometers, gasconsumption, avgGas from Car where id = 1000;
update Car
   set kilometers = 133444.000,
       gasConsumption = 41014.00
 where id = 1000;


-- now change your first trigger as follows: Instead of changing the
-- row in Vehicle, change the corresponding row in Car. What should
-- happen now?

-- useful commands for testing:
update Car set avgGas = 1 where id = 1000;
select kilometers, gasconsumption, avgGas from Car where id = 1000;
delete from useOfVehicle where id = 1086;
insert into UseOfVehicle
values(1086, 1086, 8, 30, 100, 0, 1092, null);
update Car
   set kilometers = 133444.000,
       gasConsumption = 41014.00
 where id = 1000;


-- consider the VanV that you defined in the session on views.
-- Define triggers that implement inserts into, updates of, and
-- deletes from this view.
