我有一组关于表演者,他们表演的活动以及他们执行这些活动的音乐会的表格。我想写一个触发器来防止从PERFORMERS表中删除一行,当有问题的表演者是他/她正在执行的特定活动的最后一个表演者(按日期)时。请注意,即使单个表演者只表演 一个独特的活动,一个特定的活动可以由许多表演者执行。
CREATE TABLE Activities (
ActivityID int NOT NULL,
ActivityName char(30) NOT NULL default 'charity',
PRIMARY KEY (ActivityID)
);
CREATE TABLE Performers (
PerformerID int NOT NULL default '0',
PerformerName char(20) NOT NULL default '',
Street char(20) NOT NULL default '',
City char(15) NOT NULL default '',
State char(2) NOT NULL default '',
Zip int NOT NULL default '0',
ActivityID int default '0',
PRIMARY KEY (PerformerID),
FOREIGN KEY(ActivityID) REFERENCES Activities(ActivityID) ON DELETE NO ACTION ON UPDATE NO ACTION);
CREATE TABLE Concerts (
PerformerID int NOT NULL default '0',
ArenaID int NOT NULL default '0',
ConcertDate datetime NOT NULL default '2006-01-01',
TicketPrice int NOT NULL,
PRIMARY KEY (PerformerID,ArenaID),
FOREIGN KEY (PerformerID) REFERENCES Performers(PerformerID) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (ArenaID) REFERENCES Arenas(ArenaID) ON DELETE NO ACTION ON UPDATE NO ACTION
);
我已设置了以下触发器,但无法想到如何将逻辑放入代码中。也就是说,我如何检查执行删除的执行者是否是执行其执行活动的最后一个执行者?
DELIMETER //
CREATE TRIGGER deletePerformer
BEFORE DELETE
ON PERFORMERS FOR EACH ROW
BEGIN
/*
check whether this performer is the last to perform his activity
if not allow delete
else prevent delete
*/
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Delete not allowed';
END;//
DELIMETER;