代码说删除的表不存在:
create database trigger_test;
use trigger_test;
CREATE TABLE STUDENTS(STUDENT_ID INT(25) PRIMARY KEY NOT NULL, FIRST_NAME VARCHAR(50) NOT NULL, LAST_NAME VARCHAR(50) NOT NULL, CONTACT_NUMBER INT(35),
ADDRESS VARCHAR(50) NOT NULL, ZIP_CODE_ID INT(25) NOT NULL, DEPARTMENT_ID INT(25) NOT NULL);
CREATE TABLE STUDENTS_BACKUP(STUDENT_ID INT(25) PRIMARY KEY NOT NULL, FIRST_NAME VARCHAR(50) NOT NULL, LAST_NAME VARCHAR(50) NOT NULL, CONTACT_NUMBER INT(35),
ADDRESS VARCHAR(50) NOT NULL, ZIP_CODE_ID INT(25) NOT NULL, DEPARTMENT_ID INT(25) NOT NULL);
INSERT INTO STUDENTS (Student_ID,FIRST_NAME,LAST_NAME,CONTACT_NUMBER,ADDRESS,ZIP_CODE_ID,DEPARTMENT_ID)
VALUES(090322,'MOHSIN','ALVI',5197690,'HOUSE 385 STREET 13 SHAHZAD TOWN',10,0900);
INSERT INTO STUDENTS (Student_ID,FIRST_NAME,LAST_NAME,CONTACT_NUMBER,ADDRESS,ZIP_CODE_ID,DEPARTMENT_ID)
VALUES(090329,'HAFIZ','ATA',3969839,'HOUSE 15 STREET 15 JOHAR TOWN',2,0900);
INSERT INTO STUDENTS (Student_ID,FIRST_NAME,LAST_NAME,CONTACT_NUMBER,ADDRESS,ZIP_CODE_ID,DEPARTMENT_ID)
VALUES(100258,'ABDUL','QADEER',5157869,'HOUSE 38 STREET 03 GULSHAN ABAD',2,1000);
INSERT INTO STUDENTS (Student_ID,FIRST_NAME,LAST_NAME,CONTACT_NUMBER,ADDRESS,ZIP_CODE_ID,DEPARTMENT_ID)
VALUES(100239,'NASEEM','GULZAR',56989745,'HOUSE 3 STREET 33 QASIMABAD',8,1000);
INSERT INTO STUDENTS (Student_ID,FIRST_NAME,LAST_NAME,ADDRESS,ZIP_CODE_ID,DEPARTMENT_ID)
VALUES(110369,'FAHEEM','AFFANDI','HOUSE 336 STREET 15 SHAHZAD TOWN',10,1100);
INSERT INTO STUDENTS (Student_ID,FIRST_NAME,LAST_NAME,CONTACT_NUMBER,ADDRESS,ZIP_CODE_ID,DEPARTMENT_ID)
VALUES(110879,'QASIM','SARFARAZ',9635247,'HOUSE 69 STREET 44 JUNAIDABAD',9,1100);
INSERT INTO STUDENTS (Student_ID,FIRST_NAME,LAST_NAME,CONTACT_NUMBER,ADDRESS,ZIP_CODE_ID,DEPARTMENT_ID)
VALUES(090222,'TALHA ','SALAM',5647892,'HOUSE 58 STREET 69 F-8/4',3,0900);
INSERT INTO STUDENTS (Student_ID,FIRST_NAME,LAST_NAME,ADDRESS,ZIP_CODE_ID,DEPARTMENT_ID)
VALUES(100369,'FAHAD','ALI','HOUSE 89 STREET 06 MOHRA NOOR',3,1000);
INSERT INTO STUDENTS (Student_ID,FIRST_NAME,LAST_NAME,CONTACT_NUMBER,ADDRESS,ZIP_CODE_ID,DEPARTMENT_ID)
VALUES(110888,'QALEEM','KHAN',5858969,'HOUSE 05 STREET 03 MAJAZI TOWN',5,1100);
INSERT INTO STUDENTS (Student_ID,FIRST_NAME,LAST_NAME,CONTACT_NUMBER,ADDRESS,ZIP_CODE_ID,DEPARTMENT_ID)
VALUES(125878,'JAVAID','AZEEM',8521473,'HOUSE 20 STREET 20 MALIK PLAZA',5,1200);
INSERT INTO STUDENTS (Student_ID,FIRST_NAME,LAST_NAME,CONTACT_NUMBER,ADDRESS,ZIP_CODE_ID,DEPARTMENT_ID)
VALUES(127777,'MUJAHID','BILAL',5874561,'HOUSE 15 STREET 396 TARLAI',6,1200);
DELIMITER $$
CREATE TRIGGER `DELETED_BACKUP_TRIG` AFTER DELETE ON `STUDENTS` FOR EACH ROW
begin
INSERT INTO STUDENTS_BACKUP(STUDENT_ID,FIRST_NAME,LAST_NAME,CONTACT_NUMBER,ADDRESS,ZIP_CODE_ID,DEPARTMENT_ID)
SELECT (STUDENT_ID,FIRST_NAME,LAST_NAME,CONTACT_NUMBER,ADDRESS,ZIP_CODE_ID,DEPARTMENT_ID) FROM DELETED;
END $$
答案 0 :(得分:1)
尝试以下方法。专注于OLD.
你很亲密。
drop trigger if exists `DELETED_BACKUP_TRIG`; -- added for the heck of it
DELIMITER $$
CREATE TRIGGER `DELETED_BACKUP_TRIG` AFTER DELETE ON `STUDENTS` FOR EACH ROW
begin
INSERT INTO STUDENTS_BACKUP(STUDENT_ID,FIRST_NAME,LAST_NAME,CONTACT_NUMBER,ADDRESS,ZIP_CODE_ID,DEPARTMENT_ID)
values (OLD.STUDENT_ID,OLD.FIRST_NAME,OLD.LAST_NAME,OLD.CONTACT_NUMBER,OLD.ADDRESS,OLD.ZIP_CODE_ID,OLD.DEPARTMENT_ID);
END $$
delete from students where student_id=100369;
select * from students_backup;
-- trust me the row is there
所以,没有from DELETED
答案 1 :(得分:0)
错误消息说明了一切 - 您的触发器代码包含语句
SELECT (STUDENT_ID, FIRST_NAME, LAST_NAME, CONTACT_NUMBER,
ADDRESS, ZIP_CODE_ID, DEPARTMENT_ID) FROM DELETED
并且您没有名为DELETED
的表。
我希望您阅读using triggers上的文档。简短版本是您使用可伪造名称OLD
来引用已删除的记录。您的触发器代码应为
DELIMITER $$
CREATE TRIGGER deleted_backup_trig
AFTER DELETE ON students
FOR EACH ROW
BEGIN
INSERT INTO students_backup (STUDENT_ID, FIRST_NAME, LAST_NAME, CONTACT_NUMBER, ADDRESS,ZIP_CODE_ID, DEPARTMENT_ID)
VALUES (OLD.STUDENT_ID, OLD.FIRST_NAME, OLD.LAST_NAME, OLD.CONTACT_NUMBER, OLD.ADDRESS, OLD.ZIP_CODE_ID, OLD.DEPARTMENT_ID);
END $$