354 lines
9.9 KiB
Markdown
354 lines
9.9 KiB
Markdown
|
# 一、填空题
|
|||
|
|
|||
|
1.DBMS的中文含义: **数据库管理系统** 。
|
|||
|
|
|||
|
2.数据库系统中常用的三种数据模型有: **层次模型,关系模型,网状模型** 。
|
|||
|
|
|||
|
3.图书与读者之间是 **多对多** 关系。
|
|||
|
|
|||
|
4.在 MySql 命令中用于显示xs表表结构的命令是 **desc xs;** 。
|
|||
|
|
|||
|
5.Select查询条件可以通过 **where** 控制。
|
|||
|
|
|||
|
6.数据表中字段的替代键约束是通过 **unique** 关键字定义的。
|
|||
|
|
|||
|
7.创建索引的三种方式: **创建表时创建** 、 **在已有表上创建索引** 、 **使用 alter table 语句创建索引** 。
|
|||
|
|
|||
|
8.“LIMIT 5,5”表示从第 **6** 条记录开始,获取5条记录。
|
|||
|
|
|||
|
9.在ORDER BY子句中 **asc** 关键字表示升序,是默认值, **desc** 关键字表示降序。
|
|||
|
|
|||
|
10.SQL语言中用 **commit** 语句提交事务,用 **rollback** 语句回滚事务。
|
|||
|
|
|||
|
|
|||
|
|
|||
|
# 二、解答题
|
|||
|
|
|||
|
### 1.简述什么是数据模型?关系模型的特点是什么?
|
|||
|
|
|||
|
> 数据模型是数据库管理系统对数据库中数据进行存储和管理所依据的逻辑模型。
|
|||
|
>
|
|||
|
> 关系模型的特点是以记录组或二维数据表的形式组织数据。
|
|||
|
|
|||
|
### 2.什么是数据库管理系统?
|
|||
|
|
|||
|
> 数据库管理系统简称DBMS,是指数据库系统中对数据进行管理的软件系统。
|
|||
|
|
|||
|
### 3.实体间的联系有哪些?请举例说明。
|
|||
|
|
|||
|
> 一对一、一对多、多对多;
|
|||
|
>
|
|||
|
> 乘客和座位、班主任和学生、学生和课程。
|
|||
|
|
|||
|
### 4.举例说明为什么需要视图。
|
|||
|
|
|||
|
> 提高了安全性能。可以对不同的用户,设定不同的视图。
|
|||
|
>
|
|||
|
> 例如:某用户只能获取user表的name和age数据,不能获取sex数据。则可以这样创建视图。
|
|||
|
|
|||
|
### 5.简述索引的概念与作用。
|
|||
|
|
|||
|
> 索引是一种使记录有序化的技术,它从逻辑上而不是物理上对记录进行排序。
|
|||
|
>
|
|||
|
> 索引的主要作用是加快数据查找速度。
|
|||
|
>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
# 三、设计题
|
|||
|
|
|||
|
假设有一个学生成绩管理的数据库,数据库名为XSCJ,包含三个数据表:xs(学生表)、kc(课程表)、cj (成绩表)。三个数据表的表结构如表1.1、表1.2、表1.3所示,数据如表2.1、表2.2、表2.3所示。
|
|||
|
|
|||
|
用SQL语句完成如下题目。
|
|||
|
|
|||
|
|
|||
|
|
|||
|
表1.1:xs(学生表)表结构
|
|||
|
|
|||
|
| 字段名 | 类型(长度) | 是否允许为空值 | 描述 |
|
|||
|
| -------- | -------------- | -------------- | ---- |
|
|||
|
| 学生编号 | char(3) | × | 主键 |
|
|||
|
| 姓名 | char(10) | × | |
|
|||
|
| 性别 | tinyint(1) | × | |
|
|||
|
| 出生日期 | date(系统默认) | × | |
|
|||
|
|
|||
|
表1.2:kc(课程表)表结构
|
|||
|
|
|||
|
| 字段名 | 类型(长度) | 是否允许为空值 | 描述 |
|
|||
|
| -------- | ------------ | -------------- | ---- |
|
|||
|
| 课程编号 | char(3) | × | 主键 |
|
|||
|
| 课程名 | char(20) | × | |
|
|||
|
|
|||
|
表1.3:cj (成绩表)表结构
|
|||
|
|
|||
|
| 字段名 | 类型(长度) | 是否允许为空值 | 描述 |
|
|||
|
| -------- | ------------ | -------------- | ---- |
|
|||
|
| 学生编号 | char(3) | × | 主键 |
|
|||
|
| 课程编号 | char(3) | × | 主键 |
|
|||
|
| 分数 | float(8) | × | |
|
|||
|
|
|||
|
表2.1:xs (学生表)表数据
|
|||
|
|
|||
|
| 学生编号 | 姓名 | 性别 | 出生日期 |
|
|||
|
| -------- | ---- | ---- | ---------- |
|
|||
|
| 01 | 赵雷 | 1 | 2000-01-01 |
|
|||
|
| 02 | 钱电 | 1 | 2000-12-01 |
|
|||
|
| 03 | 孙风 | 1 | 1999-08-06 |
|
|||
|
| 04 | 李云 | 1 | 1999-03-01 |
|
|||
|
| 05 | 周梅 | 0 | 2001-05-20 |
|
|||
|
| 06 | 吴兰 | 0 | 1998-07-01 |
|
|||
|
| 07 | 郑竹 | 0 | 2002-12-21 |
|
|||
|
| 08 | 王菊 | 0 | 2000-01-20 |
|
|||
|
|
|||
|
表2.2:kc(课程表)表数据
|
|||
|
|
|||
|
| 课程编号 | 课程名 | 课程编号 | 课程名 |
|
|||
|
| -------- | ------------- | -------- | ---------- |
|
|||
|
| 01 | C语言程序设计 | 05 | 操作系统 |
|
|||
|
| 02 | Web应用基础 | 06 | 数据结构 |
|
|||
|
| 03 | MySQL数据库 | 07 | 计算机原理 |
|
|||
|
| 04 | 离散数学 | 08 | 计算机网络 |
|
|||
|
|
|||
|
表2.3:cj (成绩表)表数据
|
|||
|
|
|||
|
| 学生编号 | 课程编号 | 分数 | 学生编号 | 课程编号 | 分数 |
|
|||
|
| -------- | -------- | ---- | -------- | -------- | ---- |
|
|||
|
| 01 | 01 | 90 | 05 | 02 | 87 |
|
|||
|
| 01 | 02 | 99 | 06 | 01 | 31 |
|
|||
|
| 01 | 03 | 70 | 06 | 04 | 45 |
|
|||
|
| 02 | 01 | 80 | 07 | 02 | 89 |
|
|||
|
| 02 | 02 | 60 | 07 | 04 | 67 |
|
|||
|
| 03 | 01 | 80 | 08 | 02 | 87 |
|
|||
|
| 03 | 03 | 80 | 08 | 04 | 88 |
|
|||
|
|
|||
|
|
|||
|
|
|||
|
## SQL语句题目如下
|
|||
|
|
|||
|
### 1.创建数据库XSCJ(1分)
|
|||
|
|
|||
|
```mysql
|
|||
|
create database XSCJ;
|
|||
|
```
|
|||
|
|
|||
|
### 2.选择数据库XSCJ(1分)
|
|||
|
|
|||
|
```mysql
|
|||
|
use XSCJ;
|
|||
|
```
|
|||
|
|
|||
|
### 3.创建以上三个数据表(3分)
|
|||
|
|
|||
|
```mysql
|
|||
|
create table xs(
|
|||
|
学生编号 char(3) not null,
|
|||
|
姓名 char(10) not null,
|
|||
|
性别 tinyint(1) not null,
|
|||
|
出生日期 date not null,
|
|||
|
primary key (学生编号)
|
|||
|
);
|
|||
|
|
|||
|
create table kc(
|
|||
|
课程编号 char(3) not null,
|
|||
|
课程名 char(10) not null,
|
|||
|
primary key (课程编号)
|
|||
|
);
|
|||
|
|
|||
|
create table cj(
|
|||
|
学生编号 char(3) not null,
|
|||
|
课程编号 char(3) not null,
|
|||
|
分数 float(8) not null,
|
|||
|
primary key (学生编号,课程编号)
|
|||
|
);
|
|||
|
```
|
|||
|
|
|||
|
### 4.插入表中所有记录(3分)
|
|||
|
|
|||
|
```mysql
|
|||
|
insert into xs (学生编号,姓名,性别,出生日期) values
|
|||
|
(01,'赵雷',1,'2000-01-01'),
|
|||
|
(02,'钱电',1,'2000-12-01'),
|
|||
|
(03,'孙风',1,'1999-08-06'),
|
|||
|
(04,'李云',1,'1999-03-01'),
|
|||
|
(05,'周梅',0,'2001-05-20'),
|
|||
|
(06,'吴兰',0,'1998-07-01'),
|
|||
|
(07,'郑竹',0,'2002-12-21'),
|
|||
|
(08,'王菊',0,'2000-01-20');
|
|||
|
|
|||
|
insert into kc (课程编号,课程名) values
|
|||
|
(01,'C语言程序设计'),
|
|||
|
(02,'Web应用基础'),
|
|||
|
(03,'MySQL数据库'),
|
|||
|
(04,'离散数学'),
|
|||
|
(05,'操作系统'),
|
|||
|
(06,'数据结构'),
|
|||
|
(07,'计算机原理'),
|
|||
|
(08,'计算机网络');
|
|||
|
|
|||
|
insert into cj (学生编号,课程编号,分数) values
|
|||
|
(01,01,90),
|
|||
|
(01,02,99),
|
|||
|
(01,03,70),
|
|||
|
(02,01,80),
|
|||
|
(02,02,60),
|
|||
|
(03,01,80),
|
|||
|
(03,03,80),
|
|||
|
(05,02,87),
|
|||
|
(06,01,31),
|
|||
|
(06,04,45),
|
|||
|
(07,02,89),
|
|||
|
(07,04,67),
|
|||
|
(08,02,87),
|
|||
|
(08,04,88);
|
|||
|
```
|
|||
|
|
|||
|
### 5.数据查询
|
|||
|
|
|||
|
(1)查询xs表中所有的记录。(2分)
|
|||
|
|
|||
|
```mysql
|
|||
|
select * from xs;
|
|||
|
```
|
|||
|
|
|||
|
(2)查询xs表中性别为男的学生姓名和出生日期,要求将姓名列显示为name,出生日期列显示为birthday。(2分)。
|
|||
|
|
|||
|
```mysql
|
|||
|
select 姓名 as name ,出生日期 as birthday from xs where 性别=1;
|
|||
|
```
|
|||
|
|
|||
|
(3)统计学生的总人数,显示为学生总数。(2分)
|
|||
|
|
|||
|
```mysql
|
|||
|
select count(学生编号) as 学生总数 from xs;
|
|||
|
```
|
|||
|
|
|||
|
(4)查询男生、女生人数。(2分)
|
|||
|
|
|||
|
```mysql
|
|||
|
select 性别,count(性别) from xs group by 性别;
|
|||
|
```
|
|||
|
|
|||
|
(5)求选修了01号课程的学生的最高分和最低分。(2分)
|
|||
|
|
|||
|
```mysql
|
|||
|
select max(分数) as 最高分,min(分数) as 最低分 from cj where 课程编号 = 01;
|
|||
|
```
|
|||
|
|
|||
|
(6)查询2000年出生的学生名单。(2分)
|
|||
|
|
|||
|
```mysql
|
|||
|
select 姓名,出生日期 from xs where 出生日期 < '2000-01-01';
|
|||
|
```
|
|||
|
|
|||
|
(7)查询名字中含有“风”字的学生信息。(2分)
|
|||
|
|
|||
|
```mysql
|
|||
|
select * from xs where 姓名 like '%风%';
|
|||
|
```
|
|||
|
|
|||
|
(8)在cj表中查询学生均分高于80分的学生编号。(2分)
|
|||
|
|
|||
|
```mysql
|
|||
|
select 学生编号,avg(分数) from cj group by 学生编号 having avg(分数) > 80;
|
|||
|
```
|
|||
|
|
|||
|
(9)查询cj表中学生的学生编号,平均分,对平均分按如下规则进行替换:均分低于60分的显示为不及格,在60-70分的显示为及格,在70-80分的显示为中等,在80-90分的显示为良好,90分以上的显示为优秀。(3分)
|
|||
|
|
|||
|
```mysql
|
|||
|
select 学生编号,
|
|||
|
case
|
|||
|
when avg(分数)<60 then "不及格"
|
|||
|
when avg(分数)>=60 and avg(分数)<70 then "及格"
|
|||
|
when avg(分数)>=70 and avg(分数)<80 then "中等"
|
|||
|
when avg(分数)>=80 and avg(分数)<90 then "良好"
|
|||
|
when avg(分数)>=90 then "优秀"
|
|||
|
end as "平均分"
|
|||
|
from cj group by 学生编号;
|
|||
|
```
|
|||
|
|
|||
|
(10)求每门课程的学生人数。(2分)
|
|||
|
|
|||
|
```mysql
|
|||
|
select kc.课程名,count(cj.学生编号) from kc,cj where kc.课程编号=cj.课程编号 group by cj.课程编号;
|
|||
|
```
|
|||
|
|
|||
|
(11)查找课程不同、分数相同的学生编号、课程号和分数。(3分)
|
|||
|
|
|||
|
```mysql
|
|||
|
select c1.* from cj as c1 join cj as c2 on c1.学生编号 = c2.学生编号 and c1.课程编号 != c2.课程编号 and c1.分数 = c2.分数;
|
|||
|
|
|||
|
select distinct c1.* from cj as c1 join cj as c2 on c1.课程编号 != c2.课程编号 and c1.分数 = c2.分数 ;
|
|||
|
```
|
|||
|
|
|||
|
(12)查找选修了04号课程的学生姓名和分数。(3分)
|
|||
|
|
|||
|
```mysql
|
|||
|
select xs.姓名,cj.分数 from xs,cj where cj.课程编号=04 and cj.学生编号=xs.学生编号;
|
|||
|
```
|
|||
|
|
|||
|
(13)查找选修了课程的学生的学生编号和姓名。(3分)
|
|||
|
|
|||
|
```mysql
|
|||
|
select distinct cj.学生编号,xs.姓名 from cj,xs where cj.学生编号=xs.学生编号;
|
|||
|
```
|
|||
|
|
|||
|
### 6.更新数据
|
|||
|
|
|||
|
(1)在xs表中将周梅的出生日期改为’2000-05-20’。(2分)
|
|||
|
|
|||
|
```mysql
|
|||
|
update xs set 出生日期='2000-05-20' where 姓名='周梅';
|
|||
|
```
|
|||
|
|
|||
|
(2)向cj表中添加记录(’04’,’02’,90)。(2分)
|
|||
|
|
|||
|
```mysql
|
|||
|
insert into cj (学生编号,课程编号,分数) values (04,02,90);
|
|||
|
```
|
|||
|
|
|||
|
(3)删除kc表中课程编号为’07’和’08’的记录。(1分)
|
|||
|
|
|||
|
```mysql
|
|||
|
delete from kc where 课程编号=07 or 课程编号=08;
|
|||
|
delete from kc where 课程编号 in (07,08);
|
|||
|
```
|
|||
|
|
|||
|
### 7.创建存储过程、调用存储过程
|
|||
|
|
|||
|
(1)创建查看xs表中全部信息的存储过程。(2分)
|
|||
|
|
|||
|
```mysql
|
|||
|
delimiter //
|
|||
|
create procedure proc_xs ()
|
|||
|
begin
|
|||
|
select * from xs;
|
|||
|
end //
|
|||
|
```
|
|||
|
|
|||
|
(2)调用该存储过程。(1分)
|
|||
|
|
|||
|
```mysql
|
|||
|
call proc_xs();//
|
|||
|
```
|
|||
|
|
|||
|
### 8.创建视图
|
|||
|
|
|||
|
(1)在xs表上创建视图view_xs,该视图只查看xs表中出生日期在2000年之前的员工的记录。(2分)
|
|||
|
|
|||
|
```mysql
|
|||
|
create view view_xs as select * from xs where 出生日期<'2000-01-01';
|
|||
|
```
|
|||
|
|
|||
|
(2)查看视图view_xs的创建语句。(1分)
|
|||
|
|
|||
|
```mysql
|
|||
|
show create view view_xs;
|
|||
|
```
|
|||
|
|
|||
|
(3)删除视图view_xs。(1分)
|
|||
|
|
|||
|
```mysql
|
|||
|
drop view view_xs;
|
|||
|
```
|
|||
|
|
|||
|
|