Professional Documents
Culture Documents
Exercise 1
In every stage, each maillot can be worn by only one cyclist. Therefore we need to create a trigger before
inserting on the Wear table to check that situation. An error must be thrown showing an error message
when trying to insert more than one cyclist with the same maillot on one stage. For example, on stage 1,
Miguel Indurain is wearing the yellow maillot, so, nobody else can wear it. An error must be shown if you
try to insert another rider with the yellow maillot on this stage. But there is no problem if you try to insert
some cyclist wearing the 'violet stars' maillot on the stage 5.
DELIMITER //
BEGIN
SET count_wear = (SELECT COUNT(*) FROM Wear WHERE stage = NEW.stage AND maillot =
NEW.maillot);
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Error: This maillot is already being worn by another
cyclist in this stage';
END IF;
END;
Exercise 2
We want to keep track every time a team changes its director. For this reason, we will create a table to
save the former directors. The table has the following structure:
At the time_stamp field we will register the current time (when the row is inserted). Use as primary key
the combination of id_team and time_stamp.
Create a trigger (every time an update is done on Team table ) to fill this table. Take into account that it
doesn't matter if the team name is changed, we only need to know when the 'director' field is changed.
id_team TINYINT,
time_stamp DATETIME,
director VARCHAR(50),
);
—---
DELIMITER //
BEGIN
END IF;
END;
Exercise 3
A rider cannot stay on database without a team if he has won a stage, or if he has won a pass or he has
worn a maillot. We want to check that the team field of riders cannot be updated to null in none of
previous situations. Give a customized error in every case. Example of customized errors:
In order to get different errors for each case, we need to create different triggers for each possibility:
1. Trigger to prevent updating a rider's team to null if they have won a stage:
DELIMITER //
BEGIN
SET MESSAGE_TEXT = 'Cannot set rider team to NULL. Rider has won a stage.';
END IF;
END;
2. Trigger to prevent updating a rider's team to null if they have won a pass:
DELIMITER //
BEGIN
SET MESSAGE_TEXT = 'Cannot set rider team to NULL. Rider has won a pass.';
END IF;
END;
3. Trigger to prevent updating a rider's team to null if they have worn a maillot:
DELIMITER //
BEGIN
SET MESSAGE_TEXT = 'Cannot set rider team to NULL. Rider has worn a maillot.';
END IF;
END;
Exercise 4
Create a procedure to remove a team. For avoiding to get an error about foreign key constraints fails,
previously we must transfer its riders to other team. So this procedure needs two parameters, one is the
team to delete and another is the team where the riders will be transfered. Use team identifiers as
parameters. Customize errors for any team identifier that doesn't exist.
DELIMITER $$
BEGIN
IF no_team_riders = 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Error: Team to delete does not exist.';
END IF;
IF team_to_transfer_riders = 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Error: Team to transfer riders does not exist.';
END IF;
END $$
DELIMITER ;
Exercise 5
Create a procedure with two parameters. The first one represents the name of a country. The procedure
must show all the cyclists (and their dorsal between brackets) from this country and return the result
through an output parameter (the second one).
Execution example:
DELIMITER //
CREATE PROCEDURE CC (
IN CountryName VARCHAR(50),
BEGIN
SELECT GROUP_CONCAT(CONCAT(`name`, ' (', dorsal, ')') SEPARATOR ', ') INTO CyclistList
FROM Rider
SET OutputMessage = CONCAT('List of cyclists from ', CountryName, ': ', CyclistList);
ELSE
END IF;
END;
SELECT @OutputMessage;
Exercise 6
Create a function that takes rider id as a parameter and shows information about his awards, that is the
number of stages and mountain passes he has won. This information must be shown as an string.
Example execution:
DELIMITER $$
DETERMINISTIC
BEGIN
SELECT `name`, dorsal, age INTO rider_name, rider_dorsal, rider_age FROM Rider WHERE dorsal =
rider_id;
RETURN CONCAT(rider_dorsal, ' - ', rider_name, ' (', rider_age, ' years old) has won ', stage_count, ' stages
and ', pass_count, ' passes.');
END$$
DELIMITER ;
—---
SELECT rider_awards(1);