I'm creating a SQL table to record information on subjects taken by students. I have a constraint which is the students are allowed to take multiple subjects in the same year, but are not allowed to same subjects over different years. For example, one can take Calculus I, Calculus II, and Calculus III in year 2021, but he/she cannot take Calculus I in the year 2019,2020, and 2021.
how do I put such a constraint in my SQL table?
I'm thinking of :
CREATE TABLE STUDENT(
STUDENT_ID NUMBER PRIMARY KEY,
COURSES VARCHAR (1000) NOT NULL,
YEAR NUMBER NOT NULL,
UNIQUE(COURSES,YEAR)
);
But I realized that the unique just prevents the same tuple of (courses, year) from appearing, but if the year changes, it will still be allowed, which is not what I want.
Since I've only learnt how to code SQL this week, is there other keywords that I can use to address my questions ? Thank you very much.