-1

問題如何加入部分表ERDigram

帶班表或與課程表或與指導員表加入塊表時,這是正確的。

詳細

部是學生分類爲(AA,BB,CC)的基團可以採取一個過程或多個課程。

部分可以授課一個或多個班級(實驗室或課堂)。

教師可以教給更多的部分和部分可以有更多的教練 raltion是多對多,並在第三個表中進行Inst_Course

我的ER圖如下:

section table join

數據庫模式以下:

CREATE TABLE [dbo].[Instructor](
    [InstructorID] [int] NOT NULL, 
    [InstructorName] [nvarchar](50) NULL, 
CONSTRAINT [PK_Instructor] PRIMARY KEY CLUSTERED 
(
    [InstructorID] ASC 
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] 
) ON [PRIMARY] 

CREATE TABLE [dbo].[Course](
    [CourseID] [int] NOT NULL, 
    [CourseName] [nvarchar](50) NULL, 
CONSTRAINT [PK_Course] PRIMARY KEY CLUSTERED 
(
    [CourseID] ASC 
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] 
) ON [PRIMARY] 

CREATE TABLE [dbo].[Class](
    [ClassID] [int] NOT NULL, 
    [ClassName] [nvarchar](50) NULL, 
CONSTRAINT [PK_Class] PRIMARY KEY CLUSTERED 
(
    [ClassID] ASC 
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] 
) ON [PRIMARY] 

CREATE TABLE [dbo].[Section](
    [SectionID] [int] NOT NULL, 
    [SectionName] [nvarchar](50) NULL, 
CONSTRAINT [PK_Section] PRIMARY KEY CLUSTERED 
(
    [SectionID] ASC 
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] 
) ON [PRIMARY] 

CREATE TABLE [dbo].[Inst_Course](
    [InstID] [int] NOT NULL, 
    [CourseID] [int] NOT NULL, 
CONSTRAINT [PK_Inst_Course] PRIMARY KEY CLUSTERED 
(
    [InstID] ASC, 
    [CourseID] ASC 
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] 
) ON [PRIMARY] 

CREATE TABLE [dbo].[Course_Class](
    [ClassID] [int] NOT NULL, 
    [CourseID] [int] NOT NULL, 
    [Fromtime] [int] NULL, 
    [Totime] [int] NULL, 
    [day] [nvarchar](50) NULL, 
CONSTRAINT [PK_Course_Class] PRIMARY KEY CLUSTERED 
(
    [ClassID] ASC, 
    [CourseID] ASC 
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] 
) ON [PRIMARY] 

R表格之間的興趣如下:

班表和課程表在表CourseCourse_Class中有許多關係。

教練桌和課程表有很多關係在表 Inst_Course。

科有很多很多與教師表和課程表,班級表 這是與教師或課程或類加入部分正確

注:此圖中沒有學生的課程表,因爲圖表的目標是指導員的設計時間表。表之間

樣本數據

Sample data to table Course_Class for instructor schedule

加入如下:

SELECT  dbo.Class.ClassName, dbo.Course_Class.CourseID, dbo.Course_Class.Fromtime, dbo.Course_Class.Totime, dbo.Course_Class.day, dbo.Course.CourseName, 
         dbo.Inst_Course.InstID, dbo.Inst_Course.CourseID AS Expr3, dbo.Instructor.InstructorID, dbo.Instructor.InstructorName 
FROM   dbo.Class INNER JOIN 
         dbo.Course_Class ON dbo.Class.ClassID = dbo.Course_Class.ClassID INNER JOIN 
         dbo.Course ON dbo.Course_Class.CourseID = dbo.Course.CourseID INNER JOIN 
         dbo.Inst_Course ON dbo.Course.CourseID = dbo.Inst_Course.CourseID INNER JOIN 
         dbo.Instructor ON dbo.Inst_Course.InstID = dbo.Instructor.InstructorID 
WHERE  (dbo.Inst_Course.InstID = 1) 

的問題是:其實我需要的是哪個表必須與部分表類加盟或課程或教練表

更新

類在我的情況下,代表教室或實驗室意思等級是地方教它 科課程:(一羣學生)代表誰教。

我可以採取課程c#在1級意義實驗室1或實驗室2或實驗室3 和lab1我可以得到課程c#或c + +或java在我的情況。

在這裏,我以節代表一羣學生。

該部分可以教授更多課程c#和C++和java。

c#當然可以有更多節aa,bb,cc。

UPDATE2

學生只參加一個部分,不能多段意關係一對多。部和類之間

關係是多對多因爲截面AA可以採取當然C#在類A和類BB

和類BB可以具有當然C#和C++

如果你的意思是當然你是對的。

課程教授不同時間從9-11,11-1,1-3,3-4.30 不同的班級在不同的班級。

課程包括多個部分,每個部分可以有更多的課程

+1

是什麼問題?請閱讀[** How-to-Ask **](http://stackoverflow.com/help/how-to-ask) \t \t這裏是[** START **](http ://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/),瞭解如何提高您的問題質量並獲得更好的答案。 –

+1

您沒有提供足夠的信息,也沒有提出明確的問題。 1)這些表格如何相互關聯?例如,你的'InstructorCourses'表在哪裏說明哪些教師講授哪些課程?你的'StudentCourses'表格記錄哪個學生在哪個課程?在這一點上,你可能會在所有3張桌子之間進行'交叉連接'(所以,笛卡爾產品)和'外部連接'到這3張桌子來填充空白。 –

+0

根據表之間的關係,它發現在上圖中 –

回答

0

OK,根據您的更新而更新,我建議你有以下結構:

create table dbo.Instructors (
    InstructorID int identity(1,1) not null , 
     constraint pkc_Instructors 
      primary key clustered (InstructorID) , 
    InstructorName nvarchar(48) not null , 
     constraint [email protected] 
      unique nonclustered (InstructorName) 
    ) 

create table dbo.Courses (
    CourseID int identity(1,1) not null , 
     constraint pkc_Courses 
      primary key clustered (CourseID) , 
    CourseName nvarchar(48) not null , 
     constraint [email protected] 
      unique nonclustered (CourseName) 
    ) 

create table dbo.ClassRooms (
    ClassRoomID int identity(1,1) not null , 
     constraint pkc_ClassRooms 
      primary key clustered (ClassRoomID) , 
    ClassRoomName nvarchar(48) not null , 
     constraint [email protected] 
      unique nonclustered (ClassRoomName) 
    ) 

create table dbo.Sections (
    SectionID int identity(1,1) not null , 
     constraint pkc_Sections 
      primary key clustered (SectionID) , 
    CourseID int not null , 
     constraint [email protected] 
      foreign key (CourseID) 
      references dbo.Courses (CourseID) , 
    SectionName nvarchar(48) not null , 
     constraint [email protected] 
      unique nonclustered (SectionName) 
    ) 

create table dbo.StudentSections (
    StudentSectionID int identity(1,1) not null , 
     constraint pkn_StudentSections 
      primary key nonclustered (StudentSectionID) , 
    StudentID int not null , 
     constraint [email protected] 
      foreign key (StudentID) 
      references dbo.Students (StudentID) , 
    SectionID int not null , 
     constraint [email protected] 
      foreign key (SectionID) 
      references dbo.Sections (SectionID) , 
     constraint [email protected] 
      unique clustered (StudentID , SectionID) 
    ) 

create table dbo.SectionClassRooms (
    SectionClassRoomID int identity(1,1) not null , 
     constraint pkn_SectionClassRooms 
      primary key nonclustered (SectionClassRoomID) , 
    SectionID int not null , 
     constraint [email protected] 
      foreign key (SectionID) 
      references dbo.Sections (SectionID) , 
    ClassRoomID int not null , 
     constraint [email protected] 
      foreign key (ClassRoomID) 
      references dbo.ClassRooms (ClassRoomID) , 
     constraint [email protected] 
      unique clustered (SectionID , ClassRoomID) 
    ) 

create table dbo.InstructorSections (
    InstructorSectionID int identity(1,1) not null , 
     constraint pkn_InstructorSections 
      primary key nonclustered (InstructorSectionID) , 
    InstructorID int not null , 
     constraint [email protected] 
      foreign key (InstructorID) 
      references dbo.Instructors (InstructorID) , 
    SectionID int not null , 
     constraint [email protected] 
      foreign key (SectionID) 
      references dbo.Sections (SectionID) , 
     constraint [email protected] 
      unique clustered (InstructorID , SectionID) 
    ) 

它說的:

  • Instructors是他們自己的實體,每個instructor有一個獨特的名字。
  • Courses是相似的,它們的名字是唯一的。
  • ClassRooms,同上。
  • Sections必須是Course的一部分,不能孤立存在。
  • Students參加一個或多個Sections
  • Sections可能發生在一個或多個ClassRooms(如果這是不正確的,那麼我們需要讓每個ClassRoomIDSection的屬性,而不是把這個在自己的表SectionClassRooms)。
  • Instructor可以教許多Sessions,並且也是Session可以由多個Instructors教(如果不是這種情況,那麼InstructorID應的Sessions一個屬性)。

顯然,根據需要,您將在其中一些字段中添加更多字段。例如:會議什麼時候舉行?無論教室發生什麼,課程是否都有相同的時間?我敢肯定,這裏有細微之處。

最後,我建議您查看clusterednonclustered索引,因爲這應該針對如何檢索信息進行優化。不重要,但是我刺殺了我認爲它應該工作,不知道您的應用程序要求。

如果需要,我會再次修改。

+0

部分表格代表學生教學指定課程組歸類爲AA,BB,CC。在大學時,當學生去找我並詢問我有課程的時候,我會問他他首先跟隨哪個部分,並依賴於他告訴我,我可以告訴他哪裏是當然,當我更新哪個地方 –

+0

我原來的職位有關的點不clear.and我感謝你盡力幫助我 –

+0

@ahmedsalah更新了答案。讓我知道它是否需要改進。 –

0

這種設計似乎來自this question you recently asked中的關係/表格。 (遺憾的是這兩個問題都非常模糊)。從我的答案:

也許你想要一個表格告訴你同樣的事情,所有的教練都報道:instructorIteaches courseCin classroomCRto sectionSfor departmentDin timeslotTSon weekdayWD

我告訴你,你應該學會正常化

通過識別所有的FD(函數依賴),我們確定所有CKS(候選鍵)。然後正常化使用這些可能建議基準表的「更好」的選擇。

在此問題,在這個問題上是不可能表達的加入較小的表的大桌。

將表分解爲較小的表,即連接較小的表以獲取它的一個先決條件是,行在大表中的語句可以表示爲較小的語句的AND表。

顯然Course_Class包含從course COURSEID is taught in classroom CLASSID from FROMTIME to TOTIME on day WEEKDAY作出真實聲明的行。顯然Inst_Course包含從instructor INSTID teaches course COURSEID作出真實聲明的行。

也許你想要的行在哪裏instructor INSTID teaches course COURSEID in classroom CLASSID from FROMTIME to TOTIME on day WEEKDAY。如果每個教授課程的教師都會教授它的每一堂課,那麼你可以加入Course_ClassInst_Course來獲得這個。這似乎不太可能。所以更可能的是,這不能用較小的陳述的AND來改寫。所以你不能讓這張桌子加入更小的桌子。

也許你想要的行在哪裏instructor INSTID teaches section SECTIONID of course COURSEID in classroom CLASSID from FROMTIME to TOTIME on day WEEKDAY。然後,類似地,爲course COURSEID has section SECTIONID添加Course_Section表沒有幫助,除非每個課程的每個部分都在每個講座中講授。

單獨表格中的信息並不能共同告訴你這些大表所做的事情。 (儘管碰巧在這裏可能會從大表中生成較小的表。)

您需要了解(FKS &中正&)正常化,它通過較小的是加入回替換它們大表。