2015-10-14 76 views
0

我有一個這些表:我們可以添加主鍵破錶

Emp_Tutor: Tutor table. 
Emp_Tu_De :Tutor details table. 
Emp_Tu_St: Tutor status table. 
Emp_School: School table. 

正如我們所知道的一所學校有很多導師,每個導師在一所學校或一個又一個也許是兩或三所學校也許工作。
所以導師表我打它作爲學校和教師的細節之間的虛表>

導師狀態表,我們創造它插入就像一個導師的教學狀態(課程,課程,教學時數,學分)

所以我的問題:
我可以添加一個主鍵到輔導員表來建立關係(輔導員表和輔導員狀態表)?
不要忘記家教表是一種破碎的關係。
look at image attachment.

+0

感謝Zohar Peld先生糾正我的文章,我很快就寫了,因爲我的筆記本電池電量不足。 –

回答

0

我發現,嘗試存儲像狀態事情通常是一個錯誤。例如,「當前」,「前任」,「重新僱傭」的就業狀態通常更好地實施爲具有開始日期和結束日期的就業表。

碎表斷開關係在數據庫設計中不是正常的英文術語。我不確定你在這裏的意思。

PostgreSQL代碼如下。 SQL Server將使用日期時間數據類型來代替標準SQL的時間戳數據類型。可能還有其他小的差異。

-- Nothing surprising here. 
create table schools (
    school_id integer primary key, 
    school_name varchar(20) not null unique 
    -- other columns go here 
); 

-- Nothing surprising here. 
create table tutors (
    tutor_id integer primary key, 
    tutor_name varchar(20) not null 
    -- other columns go here 
); 

-- Nothing surprising here. 
create table tutor_details (
    tutor_id integer primary key references tutors (tutor_id), 
    tutor_phone varchar(15) 
    -- other columns go here 
); 

-- Predicate: School <school_id> employed <tutor_id> 
-- starting on <start_date> and ending on <end_date>. 
-- Allows multiple periods of employment. 
create table school_tutors (
    school_id integer not null references schools (school_id), 
    tutor_id integer not null references tutors (tutor_id), 
    start_date date not null default current_date, 
    end_date date not null default '9999-12-31', 
    -- You can make a good, practical argument for including end_date 
    -- in the primary key, but that's a different issue. 
    primary key (school_id, tutor_id, start_date) 
); 


-- Only makes sense in the context of employment, so a composite 
-- foreign key references school_tutors. In production, I'd try 
-- to use more check constraints on the timestamps. 
create table tutor_office_hours (
    school_id integer not null, 
    tutor_id integer not null, 
    start_date date not null, 
    foreign key (school_id, tutor_id, start_date) 
    references school_tutors (school_id, tutor_id, start_date), 
    office_hours_start_time timestamp not null, 
    office_hours_end_time timestamp not null 
    check (office_hours_end_time > office_hours_start_time), 
    primary key (school_id, tutor_id, office_hours_start_time) 
); 
相關問題