I have a table which looks like
MyTable (
Student ID,
Exam)
I want to place a trigger/constraint which describes:
A student ID can appear many times and does not have to be unique. However, the pair (Student ID, "French") can only appear once. So each student can only have one "French" entry. French is hardcoded
ID Exam 0001 German 0001 History 0001 French 0001 French <-- This insert should fail.
Attemting to update the "German" field to French should also fail
So far I've tried
CREATE OR REPLACE TRIGGER MyTrigger BEFORE INSERT OR UPDATE ON MyTable
FOR EACH ROW
DECLARE
rowsCount INTEGER;
BEGIN
select count(*)
INTO rowsCount
from MyTable sc
where SC.SC_TYPE = 'FRENCH' and :new.StudentID = sc.StudentID;
IF rowsCount > 1
THEN
raise_application_error('-20098','You cannot have more than one French record per student.');
END IF;
end;
This is throwing a Mutate error though. Anyway, this is related to integrity, so I'm sure a constraint would be better, but would appreciate some advice on how to accomplish this.