Example of Alternative formulation as a table constraint
Example: Alternative formulation as a table constraint
ALTER TABLE EXAM_MARK
ADD CONSTRAINT Must_be_enrolled_to_take_exam_alternative2
CHECK (EXISTS (SELECT StudentId, CourseId
FROM IS_ENROLLED_ON
WHERE StudentId = EXAM_MARK.StudentId
AND CourseId = EXAM_MARK.CourseId)
) ;
In Example, note the use of the table name, EXAM_MARK, as a range variable to qualify references to columns of that table. As always, the condition given as the operand of CHECK is one that would be legal as a WHERE condition following a FROM clause specifying just the table to which the constraint applies (viz., FROM EXAM_MARK in the case at hand).