当我尝试运行此INSERT INTO查询时,我收到此错误:
INSERT INTO Rush (firstName, lastName, schoolYearID, credits, gpa, age, phone, email, statusID, legacy, password)
VALUES ('Greg','Johnson', 1, 22, 2.4, 18, '1235368965', 'gjohnson@umd.edu', 1, 'NR', 'gjohns');
我不完全理解为什么我收到此错误,因为此INSERT INTO查询过去已成功运行。我一直在对我的数据库中的其他表进行一些更改,但是我不认为这应该是一个问题,因为信用列不是外键。此数据库位于Amazon AWS上,并且正在运行MySQL 5.6.13。谁能发现我的错误?架构如下:
CREATE TABLE Rush (
rushID INT AUTO_INCREMENT NOT NULL,
firstName VARCHAR (25) NOT NULL,
lastName VARCHAR (25) NOT NULL,
schoolYearID INT NOT NULL,
hometown VARCHAR (25),
homestate VARCHAR (25),
highschool VARCHAR (50),
credits INT UNSIGNED NOT NULL,
gpa DECIMAL (4,3) UNSIGNED NOT NULL,
major VARCHAR (25),
biography VARCHAR (500),
onlinepf VARCHAR (100),
profilePictureURL VARCHAR (200), -- User uploads profile picture to another site, the url will be used in an img tag
age INT NOT NULL,
phone VARCHAR(10) NOT NULL,
email VARCHAR (50) NOT NULL,
statusID INT NOT NULL,
legacy VARCHAR(6) DEFAULT 'NR',
legacyInfo VARCHAR (50),
password VARCHAR (50) NOT NULL,
PRIMARY KEY (rushID),
CONSTRAINT fk_Rush_schoolYearID
FOREIGN KEY (schoolYearID)
REFERENCES School_Year (schoolYearID)
ON UPDATE CASCADE ON DELETE RESTRICT,
CONSTRAINT fk_Rush_statusID
FOREIGN KEY (statusID)
REFERENCES Rush_Status (statusID)
ON UPDATE CASCADE ON DELETE RESTRICT
);
DELIMITER $$
CREATE TRIGGER Rush_Insert BEFORE INSERT ON Rush FOR EACH ROW
BEGIN
IF (NEW.firstName = '') THEN SET NEW.firstName = null; END IF;
IF (NEW.lastName = '') THEN SET NEW.lastName = null; END IF;
IF (NEW.phone = '') THEN SET NEW.phone = null; END IF;
IF (CHAR_LENGTH(NEW.phone) != 10 OR NEW.phone NOT REGEXP '[0-9]') THEN SET NEW.phone = null; END IF;
IF (NEW.email = '') THEN SET NEW.email = null; END IF;
IF (NEW.gpa = '') THEN SET NEW.gpa = null; END IF;
IF (NEW.password = '') THEN SET NEW.password = null; END IF;
IF (NEW.credits = '') THEN SET NEW.credits = null; END IF;
IF (NEW.gpa < 2.5 AND credits < 12) THEN SET NEW.statusID = 0;
ELSE SET NEW.statusID = 1; END IF;
END$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER Rush_Update BEFORE UPDATE ON Rush FOR EACH ROW
BEGIN
IF (NEW.firstName = '') THEN SET NEW.firstName = null; END IF;
IF (NEW.lastName = '') THEN SET NEW.lastName = null; END IF;
IF (NEW.phone = '') THEN SET NEW.phone = null; END IF;
IF (NEW.email = '') THEN SET NEW.email = null; END IF;
IF (NEW.gpa = '') THEN SET NEW.gpa = null; END IF;
IF (NEW.password = '') THEN SET NEW.password = null; END IF;
IF (NEW.credits = '') THEN SET NEW.credits = null; END IF;
IF (OLD.statusID = 0 AND NEW.gpa >= 2.5 AND credits >= 12) THEN SET NEW.statusID = 1; END IF;
IF (NEW.gpa < 2.5 AND NEW.credits < 12 AND statusID < 2) THEN SET NEW.statusID = 0; END IF;
IF (NEW.gpa > 4) THEN SET NEW.gpa = null; END IF;
END$$
DELIMITER ;
答案 0 :(得分:1)
我猜它在触发器中抛出错误
IF (NEW.gpa < 2.5 AND credits < 12) THEN SET NEW.statusID = 0;
应该是
IF (NEW.gpa < 2.5 AND NEW.credits < 12) THEN SET NEW.statusID = 0;