It does not recognise the tables that I created above the procedure and it says: unknown table "tourstats"
in field list.
The table tourstats contains no data intentionally because I insert data with the call of the procedure and when I execute the call of the procedure it says: unknown table "tourstats"
in field list and also the same for table "participates"
.
My goal is to write a procedure that its input is a tour_id
. If this tour_id
is in the table tourstats
in column tourID
, I either delete it or update it and if it is not, I insert it. These 3 last actions are done using 3 triggers calling the procedure but that is the easy part.
Here is my code:
CREATE DATABASE IF NOT EXISTS TourDB;
USE TourDB;
CREATE TABLE if not exists PARTICIPATE (
CID SMALLINT NOT NULL,
TOURID VARCHAR(5) NOT NULL,
PAYMENT DECIMAL(9 , 2 ) NOT NULL,
PRIMARY KEY (CID , TOURID),
CHECK (PAYMENT > 0),
FOREIGN KEY (CID)
REFERENCES CUSTOMERS (CID)
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (TourID)
REFERENCES TOURS (TourID)
ON DELETE RESTRICT ON UPDATE CASCADE
);
LOAD DATA LOCAL INFILE 'C:\\.........\\participate.csv'
INTO TABLE participate
COLUMNS TERMINATED BY ';'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;
CREATE TABLE TourStats (
tourID VARCHAR(5) NOT NULL,
numCustomers INT NOT NULL,
revenue DECIMAL(9 , 2 ) NOT NULL,
PRIMARY KEY (tourID),
FOREIGN KEY (tourID)
REFERENCES Tours (tourID)
ON DELETE CASCADE ON UPDATE CASCADE
);
#PROCEDURE
delimiter //
create procedure UpdateTourStats (IN tour_id varchar(10))
begin
if (tour_id) in (tourstats.tourID) and (tour_id) = (participate.TOURID) then
#update
begin
update TourStats
set tourID = tour_id, numCustomers = count(participate.CID), revenue = sum(participate.PAYMENT);
end;
#insert
elseif (tour_id) not in (tourstats.tourID) and (tour_id) = (participate.TOURID) then
begin
insert into TourStats
values (tourid, count(participate.CID), sum(participate.PAYMENT));
end;
#delete
else
begin
delete from tourstats
where tourstats.tourID = tour_id;
end;
end if;
end//
delimiter ;
call UpdateTourStats ('tour2');