各个表的关系 截图如下 http://115.com/file/be96t8it# (咱论坛啥时候能开放上传图片呢??大家辛苦下到网盘下吧,就一张图)首先,这个数据库管理教室中的座位其中 table表,只有一个字段,就是桌子编号,存1-95这95个编号。no自然为主键student表记录学生信息,分别是姓名(因能保证肯定不会有重名的,所以设置主键),性别,联系方式等等access表的4个字段是,桌子编号、姓名、预约开始日期,预约结束日期。(桌子编号和姓名共同为主键)表与表之间的参照关系,图中可以看出——————————————————————————————我这个数据库的作用是:要将学生预订座位的信息记录在access表中。但要保证:不允许出现同一人预订一张桌子的信息出现多次(目前我这样设计,可以避免这样.因为no和name共同为主码,不允许全相同)
如
no name srart end
10 张三 2012-5-1 2012-9-1
10 张三 2012-3-2 2012-3-9而且
不允许在access表中插入这样的非法数据(即,同一张桌子,在某一时刻被多人同时预订)比如no name srart end
10 张三 2012-5-1 2012-9-1
10 李四 2012-6-1 2012-6-3
但目前,我这样设计,完全没法控制这种非法情况的出现另外,我也不知道怎么约束,结束时间必须晚于开始日期
大家可以看看如何设计这几个表,或者提供其他的解决方案也可以。多谢啦
如
no name srart end
10 张三 2012-5-1 2012-9-1
10 张三 2012-3-2 2012-3-9而且
不允许在access表中插入这样的非法数据(即,同一张桌子,在某一时刻被多人同时预订)比如no name srart end
10 张三 2012-5-1 2012-9-1
10 李四 2012-6-1 2012-6-3
但目前,我这样设计,完全没法控制这种非法情况的出现另外,我也不知道怎么约束,结束时间必须晚于开始日期
大家可以看看如何设计这几个表,或者提供其他的解决方案也可以。多谢啦
10 张三 2012-5-1 2012-9-1
10 李四 2012-10-1 2012-10-3
create table [table](
[no] int primary key
)
create table student(
id int primary key,
name varchar(10),
.....
)create table access(
id int foreign key references student(id),
[no] int foreign key references [table]([no]),
name varchar(20),
srartdate datetime ,
enddate datetime,
--比如说要求开始时间和结束时间差必须是大于五小时
constraint no_name_access primary key([no],name)
)--这样来吧,至于那个enddate必须大于startdate,你可以写个触发器,或者把enddate作为一个计算列
alter table access drop constraint [FK_access_no]
go
if exists(select 1 from sysobjects where name='FK_access_name')
alter table access drop constraint [FK_access_name]
go
if object_id('[table]') is not null drop table [table]
if object_id('student') is not null drop table student
if object_id('access') is not null drop table access
create table [table](
[no] int not null primary key
)
go
create table student(
[name] nvarchar(20) not null primary key,
[tel] nvarchar(50)
)
go
create table access(
[no] int not null,
[name] nvarchar(20) not null,
start datetime not null,
[end] datetime
)
go
alter table access add constraint [pk_access] primary key clustered([no],[name],[start]) on [PRIMARY]
go
alter table access add constraint [CK_access_be_Date] check (start>[end])
go
alter table access add constraint [FK_access_no] foreign key([no])
references [table]([no])
go
alter table access add constraint [FK_access_name] foreign key([name])
references [student]([name])
go
if object_id('access_ins_upd','IsTrigger')=1
drop trigger access_ins_upd
go
create trigger access_ins_upd
on access
for insert,update
as
if exists(select 1 from inserted i,access a
where ((a.start between i.[start] and i.[end])
or (a.[end] between i.[start] and i.[end])
or (i.[start] between a.[start] and a.[end]))
and (a.[no]<>i.[no] or a.[name]<> i.[name] or a.[start]<>i.[start]))
begin
rollback tran
raiserror('数据不合法',11,1)
return
end
go