0

我试图用 MySQL创建一个ALBUM<- ALBUM_PHOTO->数据库模型。PHOTO

当然,我想同步更新这 3 个表,比如当我 insert into 时ALBUM_PHOTO,也插入到ALBUMPHOTO具有相应的值。但是当我添加触发器时,它们的顺序让我感到困惑,最后我得到了这个方法,如下所示:

-- --------------------------------------------------------

--
-- TABLE `user_album`
--

CREATE TABLE IF NOT EXISTS `user_album` (
  `album_id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `album_name` varchar(45) NOT NULL,
  `cover_id` int(11) NOT NULL,
  `numbers_of_photo` int(11) NOT NULL,
  `album_desc` varchar(45) DEFAULT NULL,
  `album_path` varchar(45) DEFAULT NULL,
  `addtime` datetime NOT NULL,
  PRIMARY KEY (`album_id`,`user_id`),
  KEY `fk_album_user_id_idx` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- TRIGGER `user_album`
--
DROP TRIGGER IF EXISTS `tg_album_insert`;
DELIMITER //
CREATE TRIGGER `tg_album_insert` AFTER INSERT ON `user_album`
 FOR EACH ROW BEGIN
UPDATE USER_ALBUM SET NUMBERS_OF_PHOTO = 0
WHERE ALBUM_ID = NEW.ALBUM_ID;
END
//
DELIMITER ;
DROP TRIGGER IF EXISTS `tg_album_update_then_delete`;
DELIMITER //
CREATE TRIGGER `tg_album_update_then_delete` AFTER UPDATE ON `user_album`
 FOR EACH ROW BEGIN
DELETE FROM USER_ALBUM
WHERE ALBUM_ID = NEW.ALBUM_ID AND NEW.NUMBERS_OF_PHOTO <= 0;
END
//
DELIMITER ;

-- --------------------------------------------------------

--
-- TABLE `user_photo`
--

CREATE TABLE IF NOT EXISTS `user_photo` (
  `photo_id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `thumbnail_id` int(11) NOT NULL,
  `photo_name` varchar(45) DEFAULT NULL,
  `addtime` datetime NOT NULL,
  `photo_path` varchar(45) NOT NULL,
  PRIMARY KEY (`photo_id`),
  KEY `fk_photo_user_id_idx` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;


-- --------------------------------------------------------

--
-- TABLE `user_album_photo`
--

CREATE TABLE IF NOT EXISTS `user_album_photo` (
  `photo_id` int(11) NOT NULL AUTO_INCREMENT,
  `album_id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `photo_name` varchar(45) DEFAULT NULL,
  `desc` varchar(45) DEFAULT NULL,
  `tag` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`photo_id`),
  KEY `fk_album_photo_photo_id_idx` (`photo_id`),
  KEY `fk_album_photo_album_id_idx` (`album_id`),
  KEY `fk_album_photo_user_id_idx` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

--
-- TRIGGER `user_album_photo`
--
DROP TRIGGER IF EXISTS `tg_album_photo_delete`; 
DELIMITER //
CREATE TRIGGER `tg_album_photo_delete` AFTER DELETE ON `user_album_photo`
 FOR EACH ROW BEGIN
DELETE FROM USER_PHOTO WHERE PHOTO_ID = OLD.PHOTO_ID;
UPDATE USER_ALBUM SET NUMBERS_OF_PHOTO = NUMBERS_OF_PHOTO-1
WHERE ALBUM_ID=OLD.ALBUM_ID;
END
//
DELIMITER ;
DROP TRIGGER IF EXISTS `tg_album_photo_insert`;
DELIMITER //
CREATE TRIGGER `tg_album_photo_insert` AFTER INSERT ON `user_album_photo`
 FOR EACH ROW BEGIN
IF NOT EXISTS (SELECT * FROM `user_album` WHERE ALBUM_ID = NEW.ALBUM_ID) THEN
INSERT INTO USER_ALBUM VALUES(NEW.ALBUM_ID, UEW.USER_ID);
ELSE 
UPDATE USER_ALBUM SET NUMBERS_OF_PHOTO=NUMBERS_OF_PHOTO+1
WHERE ALBUM_ID = NEW.ALBUM_ID;
END IF;
INSERT INTO USER_PHOTO VALUES(NEW.PHOTO_ID, NEW.USER_ID);
END
//
DELIMITER ;

但是现在的问题是,我在TABLE中添加了外键约束,但是在插入记录时,当其他2个表中没有相应USER_ALBUM_PHOTO的记录时,首先插入它。USER_ALBUM_PHOTO

那么,我是否需要将约束添加到其他 2 个表中,而不是USER_ALBUM_PHOTOPHOTO>- ALBUM_PHOTO<-一样ALBUM

或者必须有某事。我的触发器错了。

谢谢!

4

0 回答 0