每当在无聊的生活中呆上一天半天,总想找点所谓能够“成就”自我,能够带来惬意的事情来做,无论是游戏,打球,写程序都如小孩子在玩泥——玩一会就丢一边去了。生活就如没有远大目标的航船在大海上漂泊着,每当经过某一个不知名的岛屿,都登陆上去看一看,玩一玩,玩够了,返回航船继续依旧的漂泊。
我又想用“偶发突想”这个短语来修饰我又想出了个小玩样儿,但很不情愿地用这个短语,因为我用得自己都厌烦了,而事实上也不知道那种状态是否是真的算"偶发突想"……N天以前,我想做一个投票系统,可在设计数据库的时候遇到了困难,我不知道该如何设计数据库来解决“不确定投票项目数”的情况。一个投票题目可能有2个选项,3个,4个或者更多,同时一个投票题目可能是多选。这种有点复杂(在我看来)的关系让我在我N天前放弃了做投票系统的打算。可N天后,也就是今天,我又“偶发突想”地想通了这个问题,理顺了这种“有点复杂”的关系。
先看下这张图片:


解释解释(此图是关于投票或者选择题有未知个数选项的数据库设计):
首先,在题目表中一个投票或者选择题必然有一个题目,比如:关于年龄段的调查。此处的题目ID字段标示为主键(自动编号),标题字段存储题目,类型字段存储此投票或者选择题目是多项选择还是单项选择;
其次,在题项表中每个投票或者选择题都有N个选项,比如:5~16,17~22,22~40,40以上的四个选项。此处的题项ID字段标示为主键(自动编号),题项内容字段存储选项内容,题目ID为引用自题目表的外码。
如此设计,就可解决不确定项投票或者选择题以及是否多选的问题。只是这样的设计会导致绑定数据时的难度,需要在Item_DataBind事件下判断每个投票或者选择题类型并绑定各个题项,这种绑定方法难免会影响程序的性能。
以下是Power Desiger生成Sql文件(MS SQL2000):
复制代码
/*==============================================================*/
/* DBMS name: Microsoft SQL Server 2000 */
/* Created on: 2008-9-4 20:11:26 */
/*==============================================================*/
alter table 题项
drop constraint FK_题项_REFE_题目ID
go
if exists (select 1
from sysindexes
where id = object_id('topic')
and name = 'Index_查询'
and indid > 0
and indid < 255)
drop index topic.Index_查询
go
if exists (select 1
from sysindexes
where id = object_id('topic')
and name = 'Index_连接'
and indid > 0
and indid < 255)
drop index topic.Index_连接
go
if exists (select 1
from sysindexes
where id = object_id('题项')
and name = 'Index_更新'
and indid > 0
and indid < 255)
drop index 题项.Index_更新
go
if exists (select 1
from sysindexes
where id = object_id('题项')
and name = 'Index_连接'
and indid > 0
and indid < 255)
drop index 题项.Index_连接
go
if exists (select 1
from sysobjects
where id = object_id('topic')
and type = 'U')
drop table topic
go
if exists (select 1
from sysobjects
where id = object_id('题项')
and type = 'U')
drop table 题项
go
/*==============================================================*/
/* Table: topic */
/*==============================================================*/
create table topic (
题目ID integer identity,
标题 varchar(250) null,
类型 varchar(10) null,
constraint PK_TOPIC primary key (题目ID)
)
go
declare @Cmttopic varchar(128)
select @Cmttopic = user_name()
execute sp_addextendedproperty 'MS_Description',
'每个投票OR选择题的题目内容',
'user', @Cmttopic, 'table', 'topic'
go
execute sp_addextendedproperty 'MS_Description',
'多选OR单选',
'user', '', 'table', 'topic', 'column', '类型'
go
/*==============================================================*/
/* Index: Index_连接 */
/*==============================================================*/
create unique index Index_连接 on topic (
题目ID ASC
)
go
/*==============================================================*/
/* Index: Index_查询 */
/*==============================================================*/
create unique index Index_查询 on topic (
题目ID ASC,
类型 ASC
)
go
/*==============================================================*/
/* Table: 题项 */
/*==============================================================*/
create table 题项 (
题项ID integer identity,
选项内容 varchar(250) null,
题目ID integer null,
constraint PK_题项 primary key (题项ID)
)
go
declare @Cmt题项 varchar(128)
select @Cmt题项 = user_name()
execute sp_addextendedproperty 'MS_Description',
'投票OR选择题的各个选项',
'user', @Cmt题项, 'table', '题项'
go
execute sp_addextendedproperty 'MS_Description',
'投票选项OR题目选项内容',
'user', '', 'table', '题项', 'column', '选项内容'
go
/*==============================================================*/
/* Index: Index_连接 */
/*==============================================================*/
create index Index_连接 on 题项 (
题目ID ASC
)
go
/*==============================================================*/
/* Index: Index_更新 */
/*==============================================================*/
create unique index Index_更新 on 题项 (
题项ID ASC
)
go
alter table 题项
add constraint FK_题项_REFE_题目ID foreign key (题目ID)
references topic (题目ID)
go