sql >> Database teknologi >  >> RDS >> Mysql

Databasedesign/normaliseringsstruktur skal indeholde AND'er, OR'er, valgfrie elementer og deres relationer

En meget enkel, første tilgang ville være at bruge kun 4 tabeller:

TABLE Course 
( CourseId 
, Title 
, TotalCredits 
, ... other stuff
, PRIMARY KEY (CourseId)
) ;

TABLE Module 
( ModuleId 
, Description 
, Hours
, Credits
, ... other stuff
, PRIMARY KEY (ModuleId)
) ;
 

og kombinationerne tilladt gennem disse 2:

TABLE Course_Module ( CourseID --- for this course , ModuleID --- this module is allowed (optional or mandatory) , PRIMARY KEY (CourseID, ModuleId) , FOREIGN KEY (CourseId) REFERENCES Course (CourseId) , FOREIGN KEY (ModuleId) REFERENCES Module (ModuleId) ) ; TABLE Course_MandatoryModule ( CourseID --- for this course , ModuleID --- this module is mandatory , PRIMARY KEY (CourseID, ModuleId) , FOREIGN KEY (CourseID, ModuleId) REFERENCES Course_Module (CourseID, ModuleId) ) ;

Hvis nu dine tilladte kombinationer af moduler og kurser er mere komplicerede, som din beskrivelse antyder, i stedet for Course_Module og Course_MandatoryModule tabeller kan du definere en kompleks hierarkisk model:

Kurser:

TABLE Course                        --- same as previous model
( CourseId 
, Title 
, TotalCredits 
, ... other stuff
, PRIMARY KEY (CourseId)
) ;
 

Moduler og grupper af (moduler):

TABLE ModuleEntity                  --- the supertype for both
( ModuleEntityId                    --- modules and group of modules
, PRIMARY KEY (ModuleEntityId)
) ;

TABLE Module                        --- subtype
( ModuleId 
, Description 
, Hours
, Credits
, ... other stuff
, PRIMARY KEY (ModuleId)
, FOREIGN KEY (ModuleId) 
    REFERENCES ModuleEntity (ModuleEntityId)
) ;

TABLE ModuleGroup                  --- group of modules
( ModuleGroupId                    --- subtype of the supertype (entity)
, GroupDescription        
, PRIMARY KEY (ModuleGroupId)
, FOREIGN KEY (ModuleGroupId) 
    REFERENCES ModuleEntity (ModuleEntityId)
) ;
 

og relation (modulet tilhører gruppen):

TABLE Module_in_Group ( ModuleEntityId --- this module or group , ModuleGroupId --- is in this group , PRIMARY KEY (ModuleEntityId, ModuleGroupID) , FOREIGN KEY (ModuleEntityId) REFERENCES ModuleEntity (ModuleEntityId) , FOREIGN KEY (ModuleGroupId) REFERENCES ModuleGroup (ModuleGroupId) ) ;

og (endelig) kursus kan have gruppe af moduler:

TABLE Course_ModuleGroup ( CourseId --- for this course , ModuleGroupId --- this module group is allowed , PRIMARY KEY (CourseID, ModuleGroupId) , FOREIGN KEY (CourseId) REFERENCES Course (CourseId) , FOREIGN KEY (ModuleGroupId) REFERENCES ModuleGroup (ModuleGroupId) ) ;

  1. MySql Stored Procedure Loop Cursor - Syntaksfejl

  2. PHP/MySQL:Bedste pengeoperationer/lagringspraksis?

  3. Sådan fungerer implicitte transaktioner i SQL Server

  4. LoadError forsøger at bruge MySQL med Ruby on Rails i Windows, RubyMine IDE