插入触发器不工作后的MySQL

时间:2013-02-22 23:17:42

标签: mysql triggers insert phpmyadmin

我有以下两个表,以及一个触发器,当一个记录插入到UserID表中且在StudentID中具有非空值时,该记录应该将记录插入到Student表中。 MySQL报告触发器没有语法错误,并且插入到User表中按预期工作,但不会触发插入Student表。
我试过使用BEFORE和AFTER INSERT没有效果。我使用的是phpMyAdmin版本3.5.2.2。任何帮助将不胜感激。

DROP TABLE IF EXISTS `User` ;

CREATE  TABLE IF NOT EXISTS `User` (
  `UserID` INT NOT NULL AUTO_INCREMENT ,
  `FirstName` VARCHAR(45) NOT NULL ,
  `LastName` VARCHAR(45) NOT NULL ,
  `eMail` VARCHAR(60) NOT NULL ,
  `StudentID` INT NULL ,
  KEY ix_User_StudentID (StudentID),
  PRIMARY KEY (`UserID`)
)
ENGINE = InnoDB;  

DROP TABLE IF EXISTS `Student` ;

CREATE  TABLE IF NOT EXISTS `Student` (
  `StudentID` INT NOT NULL ,
  `UserID` INT NOT NULL ,
  PRIMARY KEY (`StudentID`) ,
  CONSTRAINT `fk_Student_User1`
    FOREIGN KEY (`StudentID` )
    REFERENCES `User` (`StudentID` )
    ON DELETE CASCADE
    ON UPDATE CASCADE, 
  CONSTRAINT `fk_Student_User2`
    FOREIGN KEY (`UserID`)
    REFERENCES `User` (`UserID`)
    ON DELETE CASCADE
    ON UPDATE CASCADE
)
ENGINE = InnoDB;

DROP TRIGGER IF EXISTS after_insert_user;

DELIMITER //
CREATE TRIGGER after_insert_user 
AFTER INSERT ON `User`
FOR EACH ROW BEGIN
    IF (NEW.StudentID <> NULL) THEN
        INSERT INTO Student VALUES (NEW.StudentID, NEW.UserID); 
    END IF;
END;
//

DELIMITER ;

1 个答案:

答案 0 :(得分:3)

需要是AFTER触发器,但是

IF (NEW.StudentID <> NULL) THEN

不会给你你想要的东西。它总是会返回false。相反,使用:

IF (NEW.StudentID IS NOT NULL) THEN

或更神秘:

IF (NOT IFNULL(NEW.StudentID)) THEN

或更深奥:

IF (NOT NEW.StudentID <=> NULL) THEN

这是实际行动:

ross@pv1:~$ mysql -vvv < 15034839.sql 
--------------
DROP TABLE IF EXISTS `Student`
--------------

Query OK, 0 rows affected (0.00 sec)

--------------
DROP TABLE IF EXISTS `User`
--------------

Query OK, 0 rows affected (0.01 sec)

--------------
CREATE  TABLE IF NOT EXISTS `User` (
  `UserID` INT NOT NULL AUTO_INCREMENT ,
  `FirstName` VARCHAR(45) NOT NULL ,
  `LastName` VARCHAR(45) NOT NULL ,
  `eMail` VARCHAR(60) NOT NULL ,
  `StudentID` INT NULL ,
  KEY ix_User_StudentID (StudentID),
  PRIMARY KEY (`UserID`)
)
ENGINE = InnoDB
--------------

Query OK, 0 rows affected (0.00 sec)

--------------
CREATE  TABLE IF NOT EXISTS `Student` (
  `StudentID` INT NOT NULL ,
  `UserID` INT NOT NULL ,
  PRIMARY KEY (`StudentID`) ,
  CONSTRAINT `fk_Student_User1`
    FOREIGN KEY (`StudentID` )
    REFERENCES `User` (`StudentID` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_Student_User2`
    FOREIGN KEY (`UserID`)
    REFERENCES `User` (`UserID`)
    ON DELETE CASCADE
    ON UPDATE CASCADE
)
ENGINE = InnoDB
--------------

Query OK, 0 rows affected (0.00 sec)

--------------
DROP TRIGGER IF EXISTS after_insert_user
--------------

Query OK, 0 rows affected, 1 warning (0.00 sec)

--------------
CREATE TRIGGER after_insert_user
AFTER INSERT ON `User`
FOR EACH ROW BEGIN
    IF (NEW.StudentID IS NOT NULL) THEN
        INSERT INTO Student VALUES (NEW.StudentID, NEW.UserID);
    END IF;
END;
--------------

Query OK, 0 rows affected (0.00 sec)

--------------
INSERT INTO User VALUES (NULL, 'first', 'last', 'email', 123)
--------------

Query OK, 1 row affected (0.01 sec)

--------------
SELECT * FROM Student
--------------

+-----------+--------+
| StudentID | UserID |
+-----------+--------+
|       123 |      1 |
+-----------+--------+
1 row in set (0.00 sec)

Bye
ross@pv1:~$