在插入'年之前我需要检查一下。 (学术报告)栏目,它不低于报名日期' (学生)栏目
我的桌子:
CREATE TABLE STUDENT
(
BCN INT (10) ,
Enrollment_Date timestamp not null default CURRENT_TIMESTAMP,
primary key (BCN),
);
CREATE TABLE ACADEMIC_REPORT
(
Stud_Num INT(10) ,
Year year ,
primary key (Stud_Num , Year ),
foreign key (Stud_Num ) references STUDENT( BCN )
ON DELETE restrict ON UPDATE CASCADE
);
我试过这个:
/* ACADEMIC_REPORT._Year Constraint "BEFORE INSERT" */
delimiter //
CREATE TRIGGER ReportYearBIN BEFORE INSERT ON ACADEMIC_REPORT
FOR EACH ROW
BEGIN
declare num int(10);
SET num= (SELECT BCN
FROM STUDENT , ACADEMIC_REPORT
WHERE BCN = ACADEMIC_REPORT.Stud_Num );
IF (ACADEMIC_REPORT.Year < YEAR(num.Enrollment_Date))
THEN
SIGNAL SQLSTATE '10000'
SET MESSAGE_TEXT = 'Error....!';
END IF;
END;
// delimiter ;
但是在插入
期间会导致此错误Error Code: 1054. Unknown column 'ACADEMIC_REPORT.Year' in 'field list'
我该怎样做这个约束?
使用MySql工作台6.3
提前谢谢。
答案 0 :(得分:0)
好的,我为你想要生成错误的条件创建了一个select语句。如果该报告的学生存在并且报告年度小于学生注册日期,则它将为选择查询生成一行或多行。然后,进程FOUND_ROWS()将大于0,这将触发您的错误消息。
CREATE TRIGGER ReportYearBIN BEFORE INSERT ON ACADEMIC_REPORT
FOR EACH ROW
BEGIN
SELECT * FROM STUDENT WHERE BCN=new.Stud_Num AND new._year < Enrollment_Date
IF (SELECT FOUND_ROWS() > 0)
THEN
SIGNAL SQLSTATE '10000'
SET MESSAGE_TEXT = 'Error....!';
END IF;
END;
答案 1 :(得分:0)
我已经找到了问题:)
这句话完全错了,
YEAR(num.Enrollment_Date)
因为编写attribute.attribute不正确,所以只能使用Table.Attribute。
所以,这是解决方案
/* ACADEMIC_REPORT._Year Constraint "BEFORE INSERT" */
delimiter //
CREATE TRIGGER ReportYearBIN BEFORE INSERT ON ACADEMIC_REPORT
FOR EACH ROW
BEGIN
declare Enroll timestamp;
SET Enroll = (SELECT Enrollment_Date
FROM STUDENT
WHERE BCN = new.Stud_Num);
IF ( new._year < YEAR(Enroll))
THEN
SIGNAL SQLSTATE '10000'
SET MESSAGE_TEXT = 'Error..The Report year cannot be less than the enrollment year..!';
END IF;
END;
// delimiter ;