2

我正在使用 MySQL 5.0.95。我有一个网络表单,客户可以在其中为他们的活动创建一个客人名单,客人名单将由办公室经理批准,然后前台会从中获得当天批准的访客。

我已将问题归结为节省空间。我有两张桌子,_events 和 _guests。我还有两个匹配的存档表。

CREATE TABLE IF NOT EXISTS visitor._events (
eventId int( 11 ) NOT NULL AUTO_INCREMENT,
eventName varchar( 200 ) NOT NULL,
PRIMARY KEY ( eventId ) 
);

CREATE TABLE IF NOT EXISTS  visitor._archived_events (
eventId int( 11 ) NOT NULL,
eventName varchar( 200 ) NOT NULL,
PRIMARY KEY ( eventId ) 
);

CREATE TABLE IF NOT EXISTS  visitor._guests (
guestId int( 11 ) NOT NULL AUTO_INCREMENT,
eventId int( 11 ) NOT NULL,
guestName varchar( 200 ) NOT NULL,
PRIMARY KEY ( guestId ) 
);

CREATE TABLE IF NOT EXISTS  visitor._archived_guests (
guestId int( 11 ) NOT NULL,
eventId int( 11 ) NOT NULL,
guestName varchar( 200 ) NOT NULL,
PRIMARY KEY ( guestId ) 
);

我希望办公室经理能够在活动结束后从数据库中删除活动(及其客人记录),但我希望将这些活动存档一年以进行记录管理。

我创建了一个 TRIGGER,它将从 _guests 表中删除的任何记录复制到 _archive_guests 表中。

DELIMITER $$

DROP TRIGGER IF EXISTS TR_A_DEL_guests $$

CREATE TRIGGER TR_A_DEL_guests DELETE ON _guests FOR EACH ROW BEGIN

 INSERT IGNORE INTO _archived_guests (
   guestId,
   guestName
 ) VALUES (
   OLD.guestId,
   guestName
 );

END $$

DELIMITER ;

我还有一个 TRIGGER,它将删除 _guests 表中特定 eventID 的所有访客记录,然后将 _events 表中的任何 DELETED 记录复制到 _archived_events 表中。

DELIMITER $$

DROP TRIGGER IF EXISTS TR_A_DEL_events $$

CREATE TRIGGER TR_A_DEL_events AFTER DELETE ON _events FOR EACH ROW BEGIN

 DELETE FROM _guests WHERE eventId = OLD.eventId;

 INSERT IGNORE INTO _archived_events (
   eventId,
   eventName
 ) VALUES (
   OLD.idEventId,
   OLD.eventName
 );

END $$

DELIMITER ;

结果是,当从 _events 中删除一条记录时,它会被移动到 _archived_events 并且该事件的所有 _guests 记录都将复制到 _archived_guests 表中。

我遇到的问题是,现在,当客户正在编辑他们的客人名单时,如果他们删除客人记录,它就会被存档。他们可以编辑/删除客人,但不能完全删除活动。

我的问题:有没有办法在已删除的事件上只使用一个 TRIGGER,首先将每个适当的 _guest 记录 (eventId = OLD.eventId) 复制到 _archived_guests,然后将 _event 记录复制到 _archived_events?

4

1 回答 1

2

如果我理解正确,请尝试

DELIMITER $$

DROP TRIGGER IF EXISTS TR_A_DEL_events_guests $$
CREATE TRIGGER TR_A_DEL_events_guests 
BEFORE DELETE ON _events 
FOR EACH ROW 
BEGIN
    -- Archive guests
    INSERT IGNORE INTO _archived_guests (guestId, eventId, guestName)
    SELECT guestId, eventId, guestName
      FROM _guests
     WHERE eventId = OLD.eventId;
    -- Delete guests 
    DELETE 
      FROM _guests 
     WHERE eventId = OLD.eventId;
    -- Archive event before deleting
    INSERT IGNORE INTO _archived_events (eventId, eventName)
    VALUES (OLD.eventId, OLD.eventName);
END $$

DELIMITER ;

这是SQLFiddle演示

于 2013-05-23T21:32:11.163 回答