sqlite触发器检查新元组值与子查询的相等性

时间:2013-04-14 02:48:10

标签: sql database sqlite triggers

我有一个代表学校的数据库,其中包括课程,学生和注册等表格。他们有以下模式:

CREATE TABLE Students(
id INT CHECK(id > 100000),
lName CHAR(20),
fName CHAR(10),
gender CHAR(1) CHECK(gender IN('F','M')),
dob DATE,
address CHAR(100),
phone INT CHECK(phone > 1000000000),
grade CHAR(1) CHECK(grade IN('K','1','2','3','4','5')),
PRIMARY KEY(id));

CREATE TABLE Courses(
id INT CHECK(id > 1000),
name CHAR(40),
grade CHAR(1) CHECK(grade IN('K','1','2','3','4','5')),
PRIMARY KEY(id)
);

CREATE TABLE Enrollment(
studentID INT REFERENCES Students(id),
courseID INT REFERENCES Courses(id),
semester CHAR(6) CHECK(semester IN('Fall','Spring')),
year INT,
letterGrade CHAR(1) CHECK(letterGrade IN('A','B','C','D','E')),
PRIMARY KEY(studentID,courseID,semester,year));

我想为注册表创建一个触发器,该触发器将在插入元组之前检查具有给定学生ID的学生的年级和具有courseID的课程的成绩水平。我一直在研究这个问题,似乎无法让它发挥作用。有人能指出我正确的方向吗? 这就是我想出来的:

CREATE TRIGGER appropriateLevel
   ...> BEFORE INSERT ON Enrollment
   ...> FOR EACH ROW
   ...> BEGIN
   ...> SELECT RAISE(ABORT,'The student's grade does not match the grade level of the course.')
   ...> WHERE EXISTS(SELECT * FROM Students, Courses WHERE ((Students.grade = Courses.grade) AND (Students.id = NEW.studentID) AND (Courses.id = NEW.courseID)));
   ...> END;

谢谢!

1 个答案:

答案 0 :(得分:2)

当成绩匹配时,您的触发器会中止。

无论如何,您不需要使用EXISTS,因为您只想比较每个父表中的一个特定值:

CREATE TRIGGER appropriateLevel
BEFORE INSERT ON Enrollment
FOR EACH ROW
BEGIN
    SELECT RAISE(ABORT, 'The student''s grade does not match the course''s grade level.')
    WHERE (SELECT grade FROM Students WHERE id = NEW.studentID) <>
          (SELECT grade FROM Courses  WHERE id = NEW.courseID);
END;