2015-10-14 34 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) 
); 
相关问题