广州大学学生实验报告
开课学院及实验室:计算机科学与工程实验室418B室 2018年06月 14 日
学院 |
计算机科学与教育软件 |
年级、专业、班 |
网络*** |
姓名 |
卟咚君 |
学号 |
1606100*** |
|
实验课程名称 |
数据库原理实验 |
成绩 |
|
|||||
实验项目名称 |
数据库系统设计综合实验 |
指导老师 |
*** |
|||||
预备知识
本实验的任务是设计并实现一个数据库系统。数据库设计的一般步骤包括:需求分析、概念结构设计、逻辑结构设计、物理结构设计、数据库实施、数据库运行和维护。
(1) 概念结构设计
了解概念结构设计的基本方法,根据需求分析的结果或实验题目给出的要求,能够准确地用实体联系图来描述实体和实体之间的联系。
(2) 逻辑结构设计
理解逻辑结构设计的基本方法,根据实体联系图的设计,转换成合理的关系模式,每个关系模式至少应该满足第三范式的要求。
(3) 物理结构设计
理解物理结构设计的基本方法,选择合理的索引结构和存储结构,优化数据库的存取。
(4) 数据库实施
选择一门熟悉的面向对象程序设计语言,完成应用程序的开发。
实验目的
通过实验,使学生掌握数据库系统设计和开发的一般方法,能够设计并实现简单的数据库系统。
实验要求
熟悉实验室实验环境,掌握实验预备知识,了解实验中故障排除的基本方法。实验中根据实验要求完成相应的任务,并独立完成实验报告。
实验环境
Oracle 10g,windows 2003;
实验内容和步骤
假设有“教师”、“学生”、“课程”三个实体,教师的基本信息包括:工号、姓名、职称、工资,课程的基本信息包括:课程号、课程名、学分数,学生的基本信息包括:学号、姓名、性别、年龄。系统必须满足以下要求:
(1) 一门课程只能有一个教师任课,一个教师可以上多门课程;
(2) 一个学生可以选修多门课程,一门课程可以由多个学生来选修,记录不同学生选修不同课程的成绩;
(3) 设置一个管理员,用于维护(添加、删除和修改等基本任务)学生基本信息、教师基本信息和教师所授课程等工作,此外,管理员添加学生时,为其设置初始密码;当学生选修了某门课程,课程成绩由管理员录入;
(4) 学生可以利用学号和密码登录系统,登陆系统后,可以进行选课、修改密码和个人基本信息、查询自己的选课及总学分等操作;
(5) 能够统计不同职称的教师的数量、不同职称的教师的平均工资,可以统计每门课程的平均成绩、最高分、最低分,统计每个学生选修课程的总学分;
根据上述描述,解答下列问题:
关系模型为:
teacher(tno,tname,tposition,tsalary,pwd),其中,tno为teacher表的主码
course(cno,cname,ccredit,tno),其中,cno为course表的主码
student(sno,sname,ssex,sage,pwd),其中,sno为student表的主码
sc(sno,con,grade),其中,(sno,cno)为sc表的主码,sno为外码,参照student(sno),cno为外码,参照course(cno)
创建用户byd003,密码为byd123
create user byd003 identified by byd123;
grant connect,resource,unlimited tablespace to byd003;
grant create view to byd003;
在用户byd003下创建表sutudent,teacher,course,sc,视图studentinfo,courseinfo,
drop table teacher cascade constraints;
drop table student cascade constraints;
drop table course cascade constraints;
drop table sc cascade constraints;
create table Student
(
sno varchar(10) primary key,/*列级完整性约束条件*/
sname varchar(20) not null unique,
ssex char(3) check(ssex in('男','女')),
sage smallint not null,
pwd varchar(20) not null
);
select *from student;
create table teacher
(
tno varchar(7) primary key,/*列级完整性约束条件*/
tname varchar(20) not null unique,
tposition varchar(20) not null,
tsalary smallint not null,
pwd varchar(20) not null
);
select *from teacher;
create table course
(
cno varchar(4) primary key,
cname varchar(40) not null unique,
ccredit smallint not null,
tno varchar(7) not null,
foreign key (tno) references teacher(tno)
);
select *from course;
create table sc
(
sno varchar(10) not null,
cno varchar(4) not null,
grade smallint not null,
primary key(sno,cno),
foreign key (sno) references student(sno),
foreign key (cno) references course(cno)
);
select *from sc;
insert into student(sname,ssex,sno, sage, pwd) values('李勇','男','201215121',20,'123456');
insert into student(sname,ssex,sno, sage, pwd) values('刘晨','女','201215122',19,'123456');
insert into student(sname,ssex,sno, sage, pwd) values('王敏','女','201215123',18,'123456');
insert into student(sname,ssex,sno, sage, pwd) values('张立','男','201215125',19,'123456');
insert into student(sname,ssex,sno, sage, pwd) values('卟咚君','男','1606100236',19,'123456');
select *from student;
insert into teacher(tno,tname, tposition, tsalary, pwd) values('t001','张三','教授',30000,'123456');
insert into teacher(tno,tname, tposition, tsalary, pwd) values('t002','李四','副教授',35000,'123456');
insert into teacher(tno,tname, tposition, tsalary, pwd) values('t003','熊猫','教授',45000,'123456');
insert into teacher(tno,tname, tposition, tsalary, pwd) values('t004','王小二','副教授',38000,'123456');
insert into teacher(tno,tname, tposition, tsalary, pwd) values('t005','李小小','副教授',42000,'123456');
insert into teacher(tno,tname, tposition, tsalary, pwd) values('t006','李小龙','教授',60000,'123456');
select *from teacher;
insert into course values('6','数据处理',2,'t001');
insert into course values('2','数学',2,'t002');
insert into course values('7','PASCAL语言',4,'t003');
insert into course values('5','数据结构',4,'t002');
insert into course values('1','数据库',4,'t003');
insert into course values('3','信息系统',4,'t004');
insert into course values('4','操作系统',3,'t005');
select *from course;
insert into sc values('201215121','1',92);
insert into sc values('201215121','2',85);
insert into sc values('201215121','3',88);
insert into sc values('201215122','2',90);
insert into sc values('201215122','3',80);
select *from sc;
drop view courseinfo;
drop view studentinfo;
create view courseinfo as select course.cno 课程编号,course.cname 课程名称,teacher.tname 任课老师,course.ccredit 课程学分,countnum 选修人数,avg_grade 平均分,max_grade 最高分,min_grade 最低分 from teacher,course left outer join (select cname, count(*) countnum,avg(grade) avg_grade,max(grade) max_grade,min(grade) min_grade from sc,course where course.cno=sc.cno group by cname)a1 on (course.cname=a1.cname) where teacher.tno=course.tno;
select *from courseinfo;
create view studentinfo as select student.sno 学号,student.sname 姓名,(select sum(grade) from sc where sno=student.sno) 总分 ,(select sum(ccredit) from course where cno in (select cno from sc where sno=student.sno)) 总学分 ,(select max(grade) from sc where sc.sno in (student.sno)) 最高分,(select min(grade) from sc where sc.sno in (student.sno)) 最低分 from student;
select *from studentinfo;
create view teacherinfo1 as select tposition 职位,count(tno) 在任人数,avg(tsalary) 平均工资 from teacher group by tposition;
create view teacherinfo2 as select tno 教师工号,tname 教师名称,(select count(student.sno) from student,sc,course where student.sno=sc.sno and sc.cno=course.cno and course.tno=teacher.tno) 授课学生总人数 from teacher;
select * from studentinfo where 姓名 like '卟咚君';
insert into sc values('1606100236','7',80);
insert into sc values('1606100236','2',80);
insert into sc values('201215121','2',90);
insert into sc values('201215122','2',60);
insert into sc values('201215123','2',80);
insert into sc values('201215125','2',50);
commit;
部分C#代码:
定义的学生类:
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace byd001_app
{
class student
{
public string sno { get; set; }
public string sname { get; set; }
public string ssex { get; set; }
public int sage { get; set; }
public string pwd { get; set; }
public static List Selectstudent(string sname)
{
string sql = "select sno,sname,ssex,sage,pwd from student where sname like :sname";
OracleParameter[] para = new OracleParameter[] { new OracleParameter(":sname", OracleDbType.Char, 20) };
para[0].Value = sname + "%";
//MessageBox.Show(tname+"%");
List list = new List();
//创建链接,打开连接,创建命令对象,执行命令,关闭连接
OracleConnection con = new OracleConnection(ConfigurationManager.ConnectionStrings["strCon"].ConnectionString);
try
{
con.Open();
OracleCommand cmd = new OracleCommand(sql, con);
cmd.Parameters.AddRange(para);
OracleDataReader odr = cmd.ExecuteReader();
while (odr.Read())
{
student s = new student();
//c.Cno = odr.GetString(0); //ord["cno'].
s.sno = odr["sno"].ToString();
//t.tno = odr.GetString(0);
s.sname = odr.GetString(1);
s.ssex= odr.GetString(2);
s.sage = odr.GetInt32(3);
if (common.ID == "0")
s.pwd = odr.GetString(4);
else
s.pwd = "******";
list.Add(s);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
return list;
}
public static int Deletestudent(string sno)
{
int result = 0;
string sql = "delete from student where sno=:sno";
OracleParameter[] para = new OracleParameter[] { new OracleParameter(":sno", OracleDbType.Char, 10) };
para[0].Value = sno;
OracleConnection con = new OracleConnection(ConfigurationManager.ConnectionStrings["strCon"].ConnectionString);
try
{
con.Open();
OracleCommand cmd = new OracleCommand(sql, con);
cmd.Parameters.AddRange(para);
result = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
return result;
}
public static int Insertstudent(student s)
{
int result = 0;
string sql = "insert into student(sno,sname,ssex,sage,pwd) values(:sno,:sname,:ssex,:sage,:pwd)";
OracleParameter[] para = new OracleParameter[] {new OracleParameter(":sno",OracleDbType.Char,10),
new OracleParameter(":sname",OracleDbType.Char,20),
new OracleParameter(":ssex",OracleDbType.Char,3),
new OracleParameter(":sage",OracleDbType.Int32),
new OracleParameter(":pwd",OracleDbType.Char,20)};
para[0].Value = s.sno;
para[1].Value = s.sname;
para[2].Value = s.ssex;
para[3].Value = s.sage;
para[4].Value = s.pwd;
OracleConnection con = new OracleConnection(ConfigurationManager.ConnectionStrings["strCon"].ConnectionString);
try
{
con.Open();
OracleCommand cmd = new OracleCommand(sql, con);
cmd.Parameters.AddRange(para);
result = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
return result;
}
public static int Updatestudent(student s)
{
int result = 0;
string sql = "update student set sname=:sname,ssex=:ssex,sage=:sage,pwd=:pwd where sno=:sno";
OracleParameter[] para = new OracleParameter[] {new OracleParameter(":sname",OracleDbType.Char,20),
new OracleParameter(":ssex",OracleDbType.Char,20),
new OracleParameter(":sage",OracleDbType.Int32),
new OracleParameter(":pwd",OracleDbType.Char,20),
new OracleParameter(":sno",OracleDbType.Char,10)};
para[0].Value = s.sname;
para[1].Value = s.ssex;
para[2].Value = s.sage;
para[3].Value = s.pwd;
para[4].Value = s.sno;
OracleConnection con = new OracleConnection(ConfigurationManager.ConnectionStrings["strCon"].ConnectionString);
try
{
con.Open();
OracleCommand cmd = new OracleCommand(sql, con);
cmd.Parameters.AddRange(para);
result = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
return result;
}
}
}
学生查询窗口:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace byd001_app
{
public partial class Form_studentQuery : Form
{
public Form_studentQuery()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.dataGridView1.DataSource = student.Selectstudent(this.textBox1.Text);
}
private void Form_studentQuery_Load(object sender, EventArgs e)
{
this.dataGridView1.DataSource = student.Selectstudent(this.textBox1.Text);
}
}
}
添加学生:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace byd001_app
{
public partial class Form_studentInsert : Form
{
public Form_studentInsert()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
student s = new student();
s.sno = tbsno.Text;
s.sname = tbsname.Text;
s.ssex = tbssex.Text;
s.sage= Convert.ToInt32(tbsage.Text);
s.pwd = tbpwd.Text;
//把课程c放入数据库
if (student.Insertstudent(s) == 1)
{
MessageBox.Show("insert success");
}
}
}
}
修改学生1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace byd001_app
{
public partial class Form_studentUpdate : Form
{
public Form_studentUpdate()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.dataGridView1.DataSource = student.Selectstudent(this.textBox1.Text);
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (this.dataGridView1.Rows.Count == 0) return;
if (e.RowIndex < 0) return;
DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
if (e.ColumnIndex == 5)
{
//表示用户点击了删除按钮
string sno = row.Cells[0].Value.ToString();
if (student.Deletestudent(sno) == 1)
{
MessageBox.Show("delete success");
//this.dataGridView1.Rows.Remove(row);//出问题
}
else
MessageBox.Show("没有找到数据");
}
else
if (e.ColumnIndex == 6)
{
//表示用户点击了修改按钮
student s = (student)row.DataBoundItem;
Form_studentUpdate2 frm = new Form_studentUpdate2();
frm.tbsno.Text = s.sno;
frm.tbsname.Text = s.sname;
frm.tbssex.Text = s.ssex;
frm.tbsage.Text = s.sage.ToString();
frm.tbpwd.Text = s.pwd;
frm.ShowDialog();
}
}
private void Form_studentUpdate_Load(object sender, EventArgs e)
{
this.dataGridView1.DataSource = student.Selectstudent(this.textBox1.Text);
}
}
}
修改学生2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace byd001_app
{
public partial class Form_studentUpdate2 : Form
{
public Form_studentUpdate2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
student s = new student();
s.sno = tbsno.Text;//保持不变的
s.sname = tbsname.Text;
s.ssex = tbssex.Text;
s.sage = Convert.ToInt32(tbsage.Text);
s.pwd = tbpwd.Text;
if (student.Updatestudent(s) == 1)
MessageBox.Show("update success");
else
MessageBox.Show("可能没有找到记录");
}
}
}
关于教师信息统计的类:
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace byd001_app
{
class teacherinfo
{
public string 职位 { get; set; }
public int 在任人数 { get; set; }
public double 平均工资 { get; set; }
public string 教师工号 { get; set; }
public string 教师姓名 { get; set; }
public int 授课学生总人数 { get; set; }
public static List Selectteacherinfo1()
{
string sql = "select *from teacherinfo1 where 职位 like :职位";
OracleParameter[] para = new OracleParameter[] { new OracleParameter(":职位", OracleDbType.Char, 7) };
para[0].Value = "%";
//MessageBox.Show(tname+"%");
List list = new List();
//创建链接,打开连接,创建命令对象,执行命令,关闭连接
OracleConnection con = new OracleConnection(ConfigurationManager.ConnectionStrings["strCon"].ConnectionString);
try
{
con.Open();
OracleCommand cmd = new OracleCommand(sql, con);
cmd.Parameters.AddRange(para);
OracleDataReader odr = cmd.ExecuteReader();
while (odr.Read())
{
teacherinfo s_infon = new teacherinfo();
//c.Cno = odr.GetString(0); //ord["cno'].
s_infon.职位 = odr["职位"].ToString();
//t.tno = odr.GetString(0);
s_infon.在任人数 = odr.GetInt32(1);
if (odr.IsDBNull(2))
s_infon.平均工资 = 0;
else
s_infon.平均工资 = odr.GetDouble(2);
list.Add(s_infon);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
return list;
}
public static List Selectteacherinfo2(string tno)
{
string sql = "select *from teacherinfo2 where 教师工号 like :教师工号";
OracleParameter[] para = new OracleParameter[] { new OracleParameter(":教师工号", OracleDbType.Char, 7) };
para[0].Value = tno + "%";
//MessageBox.Show(tname+"%");
List list = new List();
//创建链接,打开连接,创建命令对象,执行命令,关闭连接
OracleConnection con = new OracleConnection(ConfigurationManager.ConnectionStrings["strCon"].ConnectionString);
try
{
con.Open();
OracleCommand cmd = new OracleCommand(sql, con);
cmd.Parameters.AddRange(para);
OracleDataReader odr = cmd.ExecuteReader();
while (odr.Read())
{
teacherinfo s_infon = new teacherinfo();
//c.Cno = odr.GetString(0); //ord["cno'].
s_infon.教师工号 = odr["教师工号"].ToString();
//t.tno = odr.GetString(0);
s_infon.教师姓名 = odr.GetString(1);
if (odr.IsDBNull(2))
s_infon.授课学生总人数 = 0;
else
s_infon.授课学生总人数 = odr.GetInt32(2);
list.Add(s_infon);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
return list;
}
}
}
教师统计信息:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace byd001_app
{
public partial class Form_Statistics_teacher : Form
{
public Form_Statistics_teacher()
{
InitializeComponent();
}
private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.dataGridView2.DataSource = teacherinfo.Selectteacherinfo2(this.textBox1.Text);
}
private void Form_Statistics_teacher_Load(object sender, EventArgs e)
{
this.dataGridView1.DataSource = teacherinfo.Selectteacherinfo1();
this.dataGridView2.DataSource = teacherinfo.Selectteacherinfo2(this.textBox1.Text);
}
}
}
登录界面:
管理员登录后的界面:
根据不同的权限屏蔽控件,比如说管理员不需要选课的控件
管理员具有更新学生的信息的权限:
管理员具有删除老师信息的权限:
管理员具有添加课程的权限:
管理员具有更新学生选修课程的信息的权限:
统计学生成绩信息:
统计课程信息:
统计在校教师的职位和教学信息:
用学生账号登录选课:
用教师账号查询教师:
用教师账号登录查看自己的个人信息:
实验总结
总结实验过程中涉及到的知识点、实验过程中遇到的问题及解决方法。
在本次实验中,使用了C#语言,运用VS软件进行可视化编程开发一个学生选课信息管理数据库系统,需要先安装VS2013的ODT软件,然后在VS2013的扩展中将oracle的服务添加进来,这样才可以用VS连接数据库。在帮助同学用VS连接数据库的时候,有部分同学用’C##’开头的用户是无法用VS连接数据库的,但是用其他的用户是可以连接数据库,一个比较奇怪的问题。在本次实验中,有4个表,分别定义了teacher,course,student,sc类,方便查询,插入,删除,修改数据。在统计方面,定义了四个视图,分别为studentinfo,teacherinfo1,teacherinfo2,courseinfo,每一个视图也分别定义了相关的类,为了方便查询和统计。在统计的过程中,不定义类,直接将视图的内容输出的时候,会出现转换的列无效的情况,可能是在第一行的数据中有的统计出来是空值,导致列的属性无法正常输出,在将这些视图全部转换成类之后,然后将这些类的对象导入dataView1中,就可以直接输出统计的信息了,当然在从数据库读出的时候,还是需要先判断一下是否为空值。在主程序运行之前,我们可以加入一个登录界面,相同的方法,不是按一下查询键才进行查询,可以在每一个窗口弹出后,在Load事件中添加刷新窗口的功能,使得打开一个新的用户界面时就可以直接看到全部的相应的信息,当然也支持单个的查询。在登录界面中,有两个radios的按钮,一个是学生的,另外一个是老师的,学生账号需要选择学生的按钮,再用学生的账号登录,老师的也一样,这样可以有效的防止学生和老师的账号搞混,在登录的时候只需要查询一个表就可以了。由于在数据库中没有把管理员定义成一个表,所以默认管理员只有一个账号,在登录的时候特判。在登录后,会有一个全局变量common记录登录用户的信息和相应的权限。一开始主程序的所有的控件都是屏蔽的,登录成功后,会对应不同的用户开发不同的控件,另外有一些类似密码的信息对一些权限较低的用户也是用“****”显示的,只有管理员才有最高的权限可以查看所有的信息。在教师信息的统计中界面中,使用了两个dataview1的显示,界面不太好看,不知道该如何把两个信息表的内容统计到一起。另外还增加了一个个人中心的控件,在控件拉下来后,可以选择个人信息修改,可以查询到自己的个人信息以及修改相应的信息。选择重新登录界面后,可以重新进入登录界面,并更新全局变量记录的权限信息。
通过本次实验,了解概念结构设计的基本方法,理解逻辑结构设计的基本方法,理解物理结构设计的基本方法,对数据库的设计有了初步的认识。在老师教学视频的帮助下,成功的运用在VS2013的编译环境下用C#语言编写了一个学生选课信息管理数据库系统,该系统在查询,添加,修改,删除信息的时候会连接数据库,提高了数据的存储和查询的效率。在完成实验报告的基本要求后,为了管理系统的界面友好,又添加控件管理以及个人中心等功能。在完成本次综合实验的过程中,感谢老师对我们的认真指导和教诲。