我最近正在研究触发器,但我无法立即理解这一点:
查询:仅当电影的制作年份小于在投影中插入的年份时,才插入电影的放映画面。
USE cin_film; // database
drop trigger if exists vincoloPro;
DELIMITER $$
CREATE TRIGGER vincoloPro
before INSERT ON projection
for each row
begin
DECLARE C INTEGER;
SET @c = (SELECT year from film where id_film = new.id_film);
// insert in c the year corresponding to the film inserted in the tuple
IF (CAST (YEAR (NEW.giorno) AS UNSIGNED) <c) THEN
// convert to integer the year entered on projection day
SIGNAL sqlstate '01000' SET MESSAGE_TEXT = 'You have missed the year';
ELSE
SIGNAL sqlstate '01000' SET MESSAGE_TEXT = 'ADDIOS!';
END IF;
END;
$$
DELIMITER;
但是,尽管电影的制作年份大于放映电影的年份,但它使我安静地进入了新的元组: 例如: 电影:
+ --------- + ------------ + -------------------------- -------- + ------------ + ------ +
| id_film | id_regista | title | gender | year |
+ --------- + ------------ + -------------------------- -------- + ------------ + ------ +
| 1 | 15 | Crash | Drama | 1996 |
| 2 | 15 | False Sembianze | Comedy | 1988 |
| 3 | 14 | Pulp Fiction | Police | 1994 |
| 4 | 13 | Breaking the waves | Drama | 1996 |
| 5 | 13 | Dogville | Drama | 2002 |
| 6 | 12 | Alamo | Western | 1960 |
| 7 | 18 | Dangerously in three | Spying | 1985 |
| 8 | 19 | White horse, black heart | Drama | 1989 |
| 9 | 19 | In the garden of good and evil | Police | NULL |
| 10 | 21 | American Beauty | Drama | 1999 |
| 11 | NULL | The exchange | NULL | 2008 |
+ --------- + ------------ + -------------------------- -------- + ------------ + ------ +
mysql> INSERT INTO projection (id_cinema, id_film, day)
-> VALUES ('1', '1', '1877-04-03');
Query OK, 1 row affected (0.08 sec)
(尽管id_film 1->崩溃发生在1996年> 1877年)
答案 0 :(得分:0)
giorno
列不在投影表中所以,您在这里:
CREATE TRIGGER `vincoloPro` BEFORE INSERT ON `projection` FOR EACH ROW BEGIN
SELECT `year` INTO @c FROM film WHERE id_film = NEW.id_film;
IF (YEAR(NEW.`day`) < @c) THEN
SIGNAL SQLSTATE '03000' SET MESSAGE_TEXT = 'You have missed the year';
ELSE
SIGNAL SQLSTATE '01000' SET MESSAGE_TEXT = 'ADDIOS!';
END IF;
END