具有多种关系的表

时间:2014-07-21 11:12:34

标签: sql sql-server tsql

我有两张表AdmissionExams和PeriodicalExams:

create table dbo.AdmissionExams
(
  Id int identity not null 
  AnalysisId int null,            FK to Analyses Table
  ClassificationId int not null,  FK to Classifications Table
  ProfessorId int not null,       FK to Professors Table    
  StudentId int not null,         FK to Students Table  
  -- Other admission exam columns
)

create table dbo.PeriodicalExams
(
  Id int identity not null 
  AnalysisId int null,            FK to Analyses Table
  ClassificationId int not null,  FK to Classifications Table
  ProfessorId int not null,       FK to Professors Table    
  StudentId int not null,         FK to Students Table  
  -- Other periodical exam columns
)

一次考试没有Analisys或Analisys。一个Analisys只有一个考试。 一次考试总是只有一次分类。一个分类只有一个考试。

一次考试只有一名学生,只有一名教授。 但是一个学生和一个教授可以参加多个考试。

我的商业模式好吗?我不确定FK。

两种类型的考试的分析和分类表都是相同的。

2 个答案:

答案 0 :(得分:0)

我建议你在表格中使用每种类型格式的继承表。

create table dbo.Exams
(
  Id int identity not null 
  AnalysisId int null,            FK to Analyses Table
  ClassificationId int not null,  FK to Classifications Table
  ProfessorId int not null,       FK to Professors Table    
  StudentId int not null,         FK to Students Table  
)

create table dbo.AdmissionExams
(
  Id int not null 
  -- Other admission exam columns
)

create table dbo.PeriodicalExams
(
  Id int not null 
  -- Other periodical exam columns
)

ALTER TABLE dbo.AdmissionExams ADD CONSTRAINT [FK_AdmissionExams_Exam] FOREIGN KEY(Id) REFERENCES dbo.exam (Id)
ALTER TABLE dbo.PeriodicalExams ADD CONSTRAINT [FK_PeriodicalExams_Exam] FOREIGN KEY(Id) REFERENCES dbo.exam (Id)

答案 1 :(得分:0)

相反,您应该有一个检查表和一个列来定义其类型。根据您必须为分类和分析捕获的信息量,最好将它们直接添加到检查表中(给定1对1的关系)。

create table dbo.Exams
(
  Id int identity not null 
  ExamType varchar(20) null,
  AnalysisInfo varchar(20) null,            
  ClassificationInfo varchar(20) null,  
  ProfessorId int not null,       FK to Professors Table    
  StudentId int not null,         FK to Students Table  
)