I am trying to create inheritance as in a C# object using SQL Server and I have:
create table dbo.Evaluations
(
Id int not null constraint primary key clustered (Id),
Created datetime not null
);
create table dbo.Exams
(
Id int not null,
Value int not null
// Some other fields
);
create table dbo.Tests
(
Id int not null,
Order int not null
// Some other fields
);
alter table dbo.Exams
add constraint FK_Exams_Id foreign key (Id) references dbo.Evaluations(Id);
alter table dbo.Tests
add constraint FK_Tests_Id foreign key (Id) references dbo.Evaluations(Id);
Which would translate to:
public class Evaluation {}
public class Exam : Evaluation {}
public class Test : Evaluation {}
I think this is the way to go but I have a problem:
How to force that an
Evaluation
has only oneTest
or oneExam
but not both?To find which type of evaluation I have I can check exam or test for null. But should I have an
EvaluationType
inEvaluations
table instead?
NOTE: In reality I have 4 subtypes each one with around 40 to 60 different columns. And in Evaluations table I have around 20 common columns which are also the ones which i use more often to query so I get lists.