Home Database Mysql Tutorial 数据库实验报告

数据库实验报告

Jun 07, 2016 pm 03:57 PM
http author address experiment Report database

作者 : 卿笃军 原文地址:http://blog.csdn.net/qingdujun/article/details/29028363 使用SQL Server 开发服务器端应用程序 一、实验类别 综合型实验 二、实验目的 熟练掌握后台服务器端应用程序的开发。 三、实验环境 SQL Server 系列的数据库管理系统 四

作者 : 卿笃军

原文地址:http://blog.csdn.net/qingdujun/article/details/29028363

使用SQL Server 开发服务器端应用程序 一、实验类别
综合型实验
二、实验目的
熟练掌握后台服务器端应用程序的开发。
三、实验环境
SQL Server 系列的数据库管理系统
四、实验内容
对学生-课程数据库,编写存储过程,完成下面功能:
1.逐条(使用游标)浏览某个系的学生记录;
2.统计任意一门课程的成绩分布情况,即按照各分数段统计人数;
3.统计每个学生的平均成绩及排名;
4.将学生选课成绩从百分制改为等级制(即A、B、C、D、E)显示。
五、实验要求
提交源程序并标识必要的注释。保证程序能正确编译和运行,认真撰写实验报告。
六、实验过程
1.创建数据库及数据表……具体代码及创建后的结果如下图1 所示。
注:以下的3 个表,全部是可视化创建,表初始化内容如下(仿照课本输入的数值),关系
的主码加下划线表示。

学生表:Student(Sno,Sname,Ssex,Sage,Sdept)

\

课程表:Course(Cno,Cname,Ccredit)

\


学生课程表:SC(Sno,Cno,Grade)

\


2.创建存储过程1 完成……功能,具体代码及运行结果如下图R1 所示。

具体代码1:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

--1.逐条(使用游标)浏览某个系的学生记录;

--DROP PROCEDURE lookDept --删除存储过程

-----------存储过程:如下----------------

CREATE PROCEDURE lookDept @dept nchar(20) --dept为参数

AS

DECLARE @sno nchar(15),@sname nchar(20),@sex nchar(2),@age int; --定义

DECLARE sp CURSOR FOR --说明游标

SELECT Sno,Sname,Ssex ,Sage FROM Student WHERE Sdept = @dept; --查询结果推入缓冲,此时不执行SELECT语句

OPEN sp; --打开游标,此时游标指向第一条记录

FETCH NEXT FROM sp INTO @sno,@sname ,@sex ,@age; --推进游标

WHILE @@fetch_status = 0 --注:= 0 表示执行成功

BEGIN

PRINT @sno+@sname+@sex+convert(nchar,@age)+@dept; --显示

FETCH NEXT FROM sp INTO @sno,@sname ,@sex ,@age; --推进游标

END

CLOSE sp; --关闭游标

DEALLOCATE sp; --删除游标

------------执行操作:如下----------------

EXEC lookDept 'CS'; --查询计算机系学生记录

Copy after login
执行结果如图R1:
\

图:R1

3.创建存储过程2 完成……功能,具体代码及运行结果如下图R2 所示。
具体代码2:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

--2.统计任意一门课程的成绩分布情况,即按照各分数段统计人数;

--DROP PROCEDURE ScoreSec --删除存储过程

-----------存储过程:如下----------------

CREATE PROCEDURE ScoreSec @cname nchar(20) --cname为参数

AS

DECLARE @cno nchar(15); --定义变量

SELECT @cno=Cno --通过课程名,查找课程号

FROM Course

WHERE Cname=@cname

SELECT @cname 课程名,COUNT(CASE WHEN Grade<60 THEN 1 END) &#39;60分以下&#39;,

COUNT(CASE WHEN Grade>=60 AND Grade<70 THEN 1 END) &#39;60分-70分&#39;,

COUNT(CASE WHEN Grade>=70 AND Grade<80 THEN 1 END) &#39;70分-80分&#39;,

COUNT(CASE WHEN Grade>=80 AND Grade<90 THEN 1 END) &#39;80分-90分&#39;,

COUNT(CASE WHEN Grade>=90 THEN 1 END) &#39;90分以上&#39;

FROM SC

WHERE Cno = @cno

GROUP BY Grade;

------------执行操作:如下----------------

--EXEC ScoreSec &#39;数据库&#39;

Copy after login
执行结果如图R2:
\

图:R2

4.创建存储过程3 完成……功能,具体代码及运行结果如下图R3 所示。
具体代码3:

1

2

3

4

5

6

7

8

9

10

11

12

--3.统计每个学生的平均成绩及排名

--DROP PROCEDURE AvgRank --删除存储过程

-----------存储过程:如下----------------

CREATE PROCEDURE AvgRank

AS

SELECT Sname 姓名,Student.Sno 学号,AVG(Grade) 平均成绩

FROM SC,Student

WHERE SC.Sno = Student.Sno --名字查询

GROUP BY SC.Sno,Sname,Student.Sno --分组

ORDER BY 平均成绩DESC --降排名

------------执行操作:如下----------------

--EXEC AvgRank

Copy after login
执行结果如图R3:
\

图:R3

5.创建存储过程4 完成……功能,具体代码及运行结果如下图R4 所示
具体代码4:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

--4.将学生选课成绩从百分制改为等级制(即A、B、C、D、E)显示。

--DROP PROCEDURE Rank100 --删除存储过程

-----------存储过程:如下----------------

CREATE PROCEDURE Rank100

AS

SELECT Sname 姓名,SC.Sno 学号,Cname 课程名,(CASE

WHEN Grade<60 THEN &#39;E&#39;

WHEN Grade>=60 AND Grade<70 THEN &#39;D&#39;

WHEN Grade>=70 AND Grade<80 THEN &#39;C&#39;

WHEN Grade>=80 AND Grade<90 THEN &#39;B&#39;

WHEN Grade>=90 THEN &#39;A&#39; END) 等级

FROM SC,Student,Course

WHERE SC.Sno = Student.Sno AND SC.Cno = Course.Cno --姓名,课程名查询

------------执行操作:如下----------------

--EXEC Rank100

Copy after login
执行结果如图R4:
\

图:R4

七、实验总结

这2 周来,一直忙忙碌碌于各种事物,数据库作业一直拖到今天早上才开始写。按照作业要求,踏踏实实,一步一步的,将功能全部实现了。功能实现期间,查阅了不少互联网资料,当然课本也来来回回的翻阅了好几遍,没办法,自己实在是弱的可以。期间,主要查阅的关键字SQL ,CASE, WHEN, AVG 当然还有游标。完成期间,主要遇到的问题主要是以下几个:
存储结构1:Print 不显示任何东西
解决:原来是我游标说明处的SELECT 语句写错了。
错误:

1

2

DECLARE sp CURSOR FOR --说明游标

SELECT @Sno,@Sname,@Ssex ,@Sage FROM Student WHERE Sdept = @dept; --查询结果推入缓冲,此时不执行SELECT 语句

Copy after login
正确:

1

2

DECLARE sp CURSOR FOR --说明游标

SELECT Sno,Sname,Ssex ,Sage FROM Student WHERE Sdept = @dept; --查询结果推入缓冲,此时不执行SELECT 语句

Copy after login
存储结构2:主要遇到的问题就是代码如何简化问题。我上网搜索了CASE WHEN等的用法。
主要学习到的内容:(注:以下参考eshizhan的博客园)
CASE 主要有2种表达形式
1)简单CASE函数:

1

2

3

4

CASE sex

WHEN &#39;1&#39; THEN &#39;男&#39;

WHEN &#39;2&#39; THEN &#39;女&#39;

ELSE &#39;其他&#39; END

Copy after login
2)CASE搜索函数:

1

2

3

4

CASE

WHEN sex = &#39;1&#39; THEN &#39;男&#39;

WHEN sex = &#39;2&#39; THEN &#39;女&#39;

ELSE &#39;其他&#39; END

Copy after login
存储结构3:主要是聚集函数,分组的问题。
由于我想显示姓名,学号,平均分,于是以下代码就出现了:

1

SELECT Sname 姓名,Student.Sno 学号,AVG(Grade) 平均成绩

Copy after login
问题出现了:分析语句没问题,但是执行语句的时候,老是提示这样的错误“选择列表中的列'Student.Sname' 无效,因为该列没有包含在聚合函数或GROUP BY 子句中。”
解决方法:
于是我分组的时候,将其全部包含进去了,机智啊。

1

GROUP BY SC.Sno,Sname,Student.Sno --分组

Copy after login
存储结构4:这个基本上没遇到什么问题,很容易。

设想与建议:其实,如果有时间,我想做一个界面版的查询~~~还是时间紧得很啊!!!只能等到课设的时候了。另外就是,由于我电脑上面安装的是wps,为了避免用office打开的时候,格式改变影响阅读,我将文档导成.pdf格式,便于阅读。

本报告SQL Server数据库下载:http://pan.baidu.com/s/1gdABS4N 密码:14io

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1661
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

How to implement HTTP streaming using C++? How to implement HTTP streaming using C++? May 31, 2024 am 11:06 AM

How to implement HTTP streaming in C++? Create an SSL stream socket using Boost.Asio and the asiohttps client library. Connect to the server and send an HTTP request. Receive HTTP response headers and print them. Receives the HTTP response body and prints it.

How to use database callback functions in Golang? How to use database callback functions in Golang? Jun 03, 2024 pm 02:20 PM

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

How to connect to remote database using Golang? How to connect to remote database using Golang? Jun 01, 2024 pm 08:31 PM

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

How to handle database connections and operations using C++? How to handle database connections and operations using C++? Jun 01, 2024 pm 07:24 PM

Use the DataAccessObjects (DAO) library in C++ to connect and operate the database, including establishing database connections, executing SQL queries, inserting new records and updating existing records. The specific steps are: 1. Include necessary library statements; 2. Open the database file; 3. Create a Recordset object to execute SQL queries or manipulate data; 4. Traverse the results or update records according to specific needs.

See all articles