I'm using Microsoft SQL Server 2014.
Let's say I have a university database with 3 tables. One listing the courses, one the years, and one the coursework that's available to do.
CREATE TABLE Courses (
Course_ID varchar(5) PRIMARY KEY NOT NULL,
Name varchar(255) NOT NULL
)
CREATE TABLE Years (
Year_ID tinyint
)
CREATE TABLE Coursework (
CW_ID varchar(5) PRIMARY KEY NOT NULL,
Name varchar(255) NOT NULL,
Allowed varchar(255) NOT NULL
)
Now, a coursework might be allowed to any combination of courses/years. So, I could have a coursework that allows specifically students in year 1 of course X, another that allows students in courses X, Y and Z of years 2 and 3, one that allows students in course X of all years, one that allows students in years 1 and 2 regardless of course, or even one that allows all courses and all years. (I hope this gets my point through.)
"Allowed" would verify that the combination is valid, maybe by referencing the foreign keys of each table (courses and years).
But how would I do that? I've thought of creating a new table using a composite primary key, but that would require manual input and it would be limited to combinations in pairs.
So my guess is I would need to generate the possible combinations somehow, list them in a table with a unique primary key for each one, and reference that as a foreign key in the "Coursework" table. However, I'm a bit stuck. Any tips?
EDIT:
To clarify what I want:
Imagine the following Courses table:
Course_ID Name
A Arithmetics
B Biology
C Chemistry
And the following years table:
Year_ID
1
2
3
And now, a teacher creates a new coursework. They want to make it available to students in years 2 or 3, studying Biology or Chemistry. So it would be available to someone studying Biology on year 3, someone Studying Chemistry on year 2, but not for someone studying Biology on year 1 or someone studying Arithmetics on year 2.
They would insert the Coursework into the table.
CW_ID Name Allowed
SA Soil analysis B,C,2,3
This would be fine, but how would I check that they're entering values that exist, to stop them from entering "X,5" for example, as such course/year doesn't exist?
EDIT2:
I was testing something, but besides it definitely not being the best solution, I'm not even sure of how to implement it.
CREATE TABLE YesOrNo (
YesOrNo_ID bit PRIMARY KEY NOT NULL
)
CREATE TABLE Allowed (
Allowed_ID int IDENTITY(1,1),
Arithmetics bit FOREIGN KEY REFERENCES YesOrNo(YesOrNo_ID) NOT NULL,
Biology bit FOREIGN KEY REFERENCES YesOrNo(YesOrNo_ID) NOT NULL,
Chemistry bit FOREIGN KEY REFERENCES YesOrNo(YesOrNo_ID) NOT NULL,
First bit FOREIGN KEY REFERENCES YesOrNo(YesOrNo_ID) NOT NULL,
Second bit FOREIGN KEY REFERENCES YesOrNo(YesOrNo_ID) NOT NULL,
Third bit FOREIGN KEY REFERENCES YesOrNo(YesOrNo_ID) NOT NULL
)
By populating the Allowed table with all the combinations of 1s and 0s (true or false) in all the courses/years, the teacher would only have to insert the Allowed_ID in the Coursework table and then it would be known which courses/years the coursework was allowed to.
However, it is impractical to manually add all the courses and years as attributes, and even then, I don't know how to populate the table automatically with all the possibilities.
Taking from the example, one of the table's rows would look like this:
Allowed_ID Arithmetics Biology Chemistry First Second Third
42 0 1 1 0 1 1
And the teacher would just input "42" into the allowed field in the Coursework table.
CROSS JOIN