08年的数据库课程设计
《公交查询系统》设计报告
姓名: 学号: 班级:计科
1.概述
1.1 系统需求
对公交查询进行一定程度的实现
1.2 总体功能
实现简单的公交查询,查询一条线路经过的所有站点,经过站点的所有线路,(相对于查询站牌内容),以及站点到站点的查询,这里仅支持直达查询和转乘1次的查询
1.3 模块划分
实现连接,线路查询,站点查询,站点到站点查询
1.4 开发平台
操作系统:Windows XP , 数据库:SQL Server2000 , 编程工具:VC6.0
2.概念设计(E_R图)
线路信息表 站点表 途经 站点名称 站点编号 站点编号 线路编号 顺序号 m n
在概念设计中,为了将信息提取出来,很自然的想到了先建立一个站点表,用来存储站点的数据,以便于实现站点名称的修改;
接下来是如何对线路信息进行表示,如5路车的线路:孝陵卫à 卫岗 à 卫桥 à 中山门 à 明故宫 à …
由于每条线路经过的的站点数不同,难以直接表示,所以就采用下表所示的方式
线路 |
站点号 |
标识序号 |
5 |
1 |
1 |
5 |
2 |
2 |
5 |
3 |
3 |
5 |
4 |
4 |
5 |
5 |
5 |
用标识序号对各线路经过站点的顺序进行标记
站点表(站点编号,站点名称) 主键:站点编号
线路表(线路编号,站点编号,标识序号) 主键:(线路编号,标识序号)
3.逻辑与物理设计
(规范化理论、数据库与工作表设计)
站点表(站点编号,站点名称)
stop_infor(stop_no, stop_name)
stop_no –> stop_name
线路表(线路编号,站点编号,标识序号)
rout_infor(rout, stop_no, order_no)
(rout, stop_no) à order_no
(rout, order_no)à stop_no
关系模式
stop_infor(stop_no, stop_name, stop_no –> stop_name)
rout_infor(rout, stop_no, order_no,(rout, stop_no) à order_no,(rout, order_no)à stop_no)
关系模式stop_infor(stop_no, stop_name) 中,stop_no是站点编号, stop_name是站点名称
属于BCNF范式
关系模式rout_infor(rout, stop_no, order_no)中,(rout, stop_no) à order_no,(rout, order_no)à stop_no)
(rout, stop_no)与(rout, order_no)都可以作为候选码,每一个非主属性完全依赖于码,所以属于2NF,关系模式中没有属性对码传递依赖或部分依赖,所以rout_infor属于3NF,并且除(rout, stop_no)与(rout, order_no)外没有其他决定因素,所以rout_infor属于BCNF
查询设计
查询1条线路经过的站点, 如查询线路 9 经过的站点
select s.stop_name, b.order_no
from stop_infor s, bus_infor b
where
s.stop_no = b.stop_no and b.rout = '9'
order by b.order_no
转乘一次
select
distinct a.stop_no v from bus_infor a where
--a.stop_no = b.stop_no and
(select rout from bus_infor
where stop_no = '1')
in
(select rout
from bus_infor where stop_no = a.stop_no )
and
(select rout from bus_infor
where stop_no = '9')
in (select rout from bus_infor
where stop_no = a.stop_no)
查找直达的线路,如
select a.rout from bus_infor a, bus_infor b where a.rout = b.rout and a.stop_no =
(select stop_no from stop_infor where stop_name = '卫岗')
and b.stop_no =(select stop_no from stop_infor where stop_name = '汉中门')
4.程序代码
1. 用ODBC实现VC和SQL Server数据库的连接
1. 创建ODBC数据源
控制面板->管理工具->数据源
用户DSN选项卡 添加 选择SQL Server 完成
数据源名:MyDataBase
选择服务器:USASUN (机器名)
还要设置要连接的数据源
设置登录用户名u1,口令123
如果没有,可以先在SQL Server中建个用户
这样,在ODBC数据源中可以看到新建的数据源
2. 在VC中连接,使用CDatabase类,要添加头文件#include <afxdb.h>
CDatabase m_dataBase;
m_dataBase.Open(_T("MyDataBase"), FALSE, FALSE, _T("ODBC;UID=u1;PSW=123"), FALSE);
m_dataBase.ExecuteSQL(sql);
这样就可以对数据源中的数据进行操作了
2. 程序实现查询功能
详细内容见程序代码
5.本人所做工作
(工作内容,工作量大小、比重,收获与体会,今后努力方向)
工作内容 |
用ODBC实现VC 和数据库的连接 , 概念设计,把系统的功能用程序来实现 |
工作量大小 |
60% |
收获与体会 |
通过查找资料知道了如何实现数据库和编程工具的连接,在学习中要敢于尝试,会有一种豁然开朗的感觉;在概念设计中,加深了对理论知识的认识,切实体会到好的设计的重要性; |
今后努力方向 |
概念设计以及使用sql语句进行查询都需要提高 |
考核评价与成绩:
评阅教师(签字): 年 月 日 |
6.参考文献
[1] 梁普选.Visual C++程序设计与实践. 清华大学出版社. 2003
[2] 王珊, 萨师煊. 数据库系统概论. 高等教育出版社. 2005