大二数据库实验报告

实验要求:

实验一 熟悉数据库管理系统环境
实验二SQL定义语言
实验三 使用SQL语言进行简单查询
实验四 使用SQL语言进行复杂查询
实验五 SQL常用数据更新操作
实验六 综合应用\

实验一:熟悉数据库管理系统环境

实验过程及分析:
首先创建一个数据库和需要的表:

create database XSGL
GO
use XSGL
GO
Create table student      --创建学生表
(sno char(8) primary key,        --(实体完整性)学生姓名
 sname char(8) not null unique, --学生姓名
 ssex char(2) default '男' check(ssex='男' or ssex='女'),  --性别给定默认值为'男',取值只能取‘男’或‘女’
 sage tinyint check(sage>13 and sage<50),
 sdept char(20))


create table course       --创建课程表 
(cno char(2) PRimary key,        --课程编号
 cname varchar(50),  --课程名称
 cpno char(2),       --先修课号
 ccredit tinyint)  --课程名

create table sc        --创建成绩表
(sno char(8),           --学生学号
 cno char(2),         --课程编号
 grade tinyint,           --成绩
 constraint pk_grade primary key(sno,cno),
 constraint fk_stuid foreign key(sno) references student(sno),
 constraint fk_course foreign key(cno) references course(cno),
 constraint ck_grade check(grade>=0 and grade<=100) )
go
insert into student(sno,sname, ssex,sage,sdept) values('95001', '李勇', '男', 20, 'CS')
insert into student(sno,sname, ssex,sage,sdept) values('95002', '刘晨', '女', 19, 'IS')
insert into student(sno,sname, ssex,sage,sdept) values('95003', '王敏', '女', 18, 'MA')
insert into student(sno,sname, ssex,sage,sdept) values('95004', '张立', '男', 19, 'IS')
insert into student(sno,sname, ssex,sage,sdept) values('95005', '刘云', '女', 18, 'CS ')
insert into course(cno, cname,ccredit,cpno) values('1', '数据库', 4, '5')
insert into course(cno, cname,ccredit,cpno) values('2', '数学', 6, null)
insert into course(cno, cname,ccredit,cpno) values('3', '信息系统', 3, '1')
insert into course(cno, cname,ccredit,cpno) values('4', '操作系统', 4, '6')
insert into course(cno, cname,ccredit,cpno) values('5', '数据结构', 4, '7')
insert into course(cno, cname,ccredit,cpno) values('6', '数据处理', 3, null)
insert into course(cno, cname,ccredit,cpno) values('7', 'PASCAL语言', 4, '6')
insert into sc(sno,cno,grade) values('95001', '1' ,92)
insert into sc(sno,cno,grade) values('95001', '2' ,85)
insert into sc(sno,cno,grade) values('95001', '3' ,88)
insert into sc(sno,cno,grade) values('95002', '2' ,90)
insert into sc(sno,cno,grade) values('95002', '3' ,80)
insert into sc(sno,cno,grade) values('95003', '2' ,85)
insert into sc(sno,cno,grade) values('95004', '1' ,58)
insert into sc(sno,cno,grade) values('95004', '2' ,85)

1)STUDENT表中增加一个字段入学时间scome,

alter table student 
add scome date

2)删除STUDENT表中sdept字段;

alter table student
drop column sdept

3)删除创建的SC表中cno字段和COURSE表cno字段之间的外键约束;

alter table sc
DROP fk_course

4)重建3)中删除的约束

alter table sc
add constraint fk_course foreign key(cno) references course(cno)

5、重新定义一个简单表,然后用SQL语言DROP语句删除该表结构;

drop table sc

6、用SQL语言CREATE INDEX语句定义表STUDENT的SNAME字段的降序唯一索引;

create index index_sname
on student(sname desc)

7、用SQL语言DROP语句删除索引;

drop index index_sname on student

8.我觉得没有错误……

实验总结:
1. 创建表的时候可以添加约束
2. 可以添加主键唯一标识 用primary key
3. 可以使用默认值 是 default
4. 可以使用外键来限制取值范围、
5. 使用alter添加,修改列;还可以删除表中约束如索引 index
6. 使用DROP 可以直接删除表 删除的时候先要删除外键表后才可以删除主键表

实验二: SQL定义语言

实验过程及分析:
1.首先建立服务器连接 如果连接无法连接到本地local可以参考
sqlserver2008无法连接到local解决方案
2.然后打开新建查询选择数据库
3.创建表用SQL语言,具体的过程实验一已经有了

用SQL语言ALTER语句修改表结构;
(1)STUDENT表中增加一个字段入学时间scome,
(2)删除STUDENT表中sdept字段;
(3)删除创建的SC表中cno字段和COURSE表cno字段之间的外键约束;
(4)重建(3)中删除的约束
5、重新定义一个简单表,然后用SQL语言DROP语句删除该表结构;
6、用SQL语言CREATE INDEX语句定义表STUDENT的SNAME字段的降序唯一索引;
7、用SQL语言DROP语句删除索引;

有没有发现 和实验一重复了…………
没错 所以为了凑字数 我再抄了一遍……

实验总结:
1. 删除外键只能用alter 指定表 而不能用on来选择表……原因不明
2. 删除索引不能用alter 直接用DROP INDEX 索引 ON 表 原因不明

实验三:使用SQL语言进行简单查询

实验过程及分析:
还是使用实验一的表
(1)查询全体学生的学号和姓名

select sno,sname 
from student;

(2)查询全体学生的详细记录

select * 
from student;

(3)查询软件学院的学生姓名、年龄、系别

select sname,sage,sdept
from student where sdept='MA';

(4)查询所有选修过课程的学生学号(不重复)

select distinct sno 
from sc;

(5)查询考试不及格的学生学号(不重复)

select distinct sno
from sc
where grade<60;

(6)查询不是软件学院、计算机系的学生性别、年龄、系别

select ssex,sage,sdept
from student 
where sdept not in('CS','MA');

(7)查询年龄18-20岁的学生学号、姓名、系别、年龄;

select sno,sname,sdept,sage 
from student
where sage>=18 and sage<=20;

(8)查询姓刘的学生情况

select * 
from student
where sname like '刘%';

(9)查询姓刘或姓李的学生情况

select * from student where sname like '刘%' or sname like '李%'

(10)查询姓刘且名字为两个字的学生情况

select * 
from student
where sname like '刘_';

(11)查询1983年以后出生的学生姓名。

select sname from student where sage < 2018-1983

(12)创建表 studentgrad(sno,mathgrade,englishigrade,chinesegrade)
计算学生各科总成绩并赋予别名

Create table studentgrad(
    Sno char(8) ,
    mathgradeint,
    englishigradeint,
    chinesegradeint
)
Select sum(mathgrade+chinesegrade+englishigrade) '学生总成绩' from studentgrad 

(13)利用内部函数 year()查找软件学院学生的出生年份

select (year(getdate())-student.sage+1) 
from student 
where sdept='MA';

(14)利用字符转换函数实现字符联接。

select sname + '年龄为'+cast(sage as char(2))+'岁'
from student;
Select sname + ‘年龄为’+cast(sage as char(2))+’岁’
From student

(15)查询全体学生情况,查询结果按所在系升序排列,对同一系中的学生按年龄降序排列。

select*
from student order by sdept,sage desc;

(16)查询学生总人数。

select count(*) 
from student;

(17)查询选修了课程的学生人数。

select count(distinct sno) 
from sc;

(18)查询选修了7号课程的学生总人数和平均成绩

select count(*),avg(grade)as avggrade 
from student ,sc 
where student.sno=sc.sno and sc.cno='1';

(19)查询选修6号课程学生的最好成绩

select max(grade) as maxgrade 
from sc
where cno='2';

(20)查询每个系的系名及学生人数。

select sdept,count(*) 
from student group by sdept;

(21)查找每门课的选修人数及平均成绩

select cno,count(*),avg(grade) as avggrade 
from sc group by cno;

(22)查找没有先修课的课程情况

select * 
from course 
where cpno is null;

实验总结:
1. 函数year(),count(),max()可以方便查询
2. 模糊查询法要% 如like ‘刘%’
3. group by 可以分组查询

实验四:使用SQL语言进行复杂查询

实验过程及分析:
1、实验一中的数据为基础
2、对各表中的数据进行不同条件的连接查询和嵌套查询;
(1)查询每个学生及其选课情况;

select student.sno,sname,ssex,sage,sdept,cno,grade
from student,sc
where student.sno=sc.sno

(2)查询每门课的间接先修课

select first.cno,second.cpno
from course first,course second
where first.cpno=second.cno 

(3)将STUDENT,SC进行右连接

select student.sno,sname,ssex,sage,sdept,cno,grade
from student right outer join sc on student.sno=sc.sno

(4)查询既选修了2号课程又选修了3号课程的学生姓名、学号;

select student.sno,sname
from student inner join sc on student.sno=sc.sno
where cno='3' and sc.sno in
(select sno
from sc
where cno='2')

(5)查询和刘晨同一年龄的学生

select student.sno,sname
from student
where sname!='刘晨' and sage=
(select sage 
from student
where sname='刘晨')

(6)选修了课程名为“数据库”的学生姓名和年龄

select sname,sage
from student
where sno in
(select sno
from sc
where cno in
(select cno
from course 
where cname='数据库'))

(7)查询其他系比IS系任一学生年龄小的学生名单

select student.sno,sname
from student
where sdept<>'IS' and
sage<any
(select sage 
from student
where sdept='IS')

(8)查询其他系中比IS系所有学生年龄都小的学生名单

select student.sno,sname
from student
where sdept<>'IS' and 
sage<all
(select sage 
from student 
where sdept='IS')

(9)查询选修了全部课程的学生姓名

select sname
from student
where Sno in
(select Sno from SC
group by Sno
having count(*) = (select count(*) from course ))

(10)查询计算机系学生及其性别是男的学生

select student.sno,sname
from student
where sdept='IS' and ssex='男'

(11)查询选修课程1的学生集合和选修2号课程学生集合的差集

select sno
from sc 
where cno='1' except 
select sno
from sc
where cno='2'

(12)查询李丽同学不学的课程的课程号

select cno
from course
where cno not in
(select cno
from sc
where sno in
(select sno
from student
where sname='李丽'))

(13)查询选修了3号课程的学生平均年龄

select AVG(sage) as avgsage
from student inner join sc on student.sno=sc.sno
where cno='3'

(14)求每门课程学生的平均成绩

select cno,AVG(grade) as avggrade
from sc
group by cno

(15)统计每门课程的学生选修人数(超过3人的才统计)。要求输出课程号和选修人数,结果按人数降序排列,若人数相同,按课程号升序排列

select course.cno '课程号', count(sc.sno) '人数'
from course,sc 
where course.cno=sc.cno 
group by course.cno having count(sc.sno)>3 order by COUNT(sc.sno) desc,course.cno asc

(16)查询学号比刘晨大,而年龄比他小的学生姓名。

select sname
from student
where sno>
(select sno from student where sname='刘晨')and
sage<(select sage from student where sname='刘晨')

(17)求年龄大于所有女同学年龄的男同学姓名和年龄

select sname,sage
from student
where ssex='男'and sage>
(select MAX(sage) from student where ssex='女')

实验总结:
1. 求总数可以用COUNT()函数
2. 分组group by 要用having来限制条件
3. order by是排序要求 desc是降序 ,asc是升序
4. any()函数是任意的意思,all()是所有

实验五:SQL的常用数据更新操作

实验过程及分析:
1、应用INSERT,UPDATE,DELETE语句进行更新操作;
(1)插入如下学生记录(学号:95030,姓名:李莉,年龄:18)

insert into student(sno,sname,sage)
values ('95030','李莉',18)

(2)插入如下选课记录(95030,1)

insert into sc(sno,cno)
values('95030',1)

(3)计算机系学生年龄改成20

update student
set sage=20
where sdept='CS'

(4)把数学系所有学生成绩改成0

update sc
set grade=0
where 'MA'=
(select sdept
from student
where student.sno=sc.sno)

(5)把低于总平均成绩的女同学成绩提高5分

update sc 
set grade+=5
where grade<
(select avg(grade) 
from sc inner join student
on student.sno=sc.sno
where ssex='女')

(6)删除95030学生信息

delete
from student
where sno='95030'

(7)删除SC表中无成绩的记录

delete 
from sc
where grade is null;

(8)删除张娜的选课记录

delete
from sc
where sno=(select sno from student 
where sname='张娜')

(9)删除不及格的学生选课记录

delete
from sc
where grade<60

(10)删除数学系所有学生选课记录

delete
from sc
where sno in (select sno from student where sdept='MA')

(11)删除所有未被选修的课程

delete
from course
where cno not in (select cno from sc)

(12)查询每一门课程成绩都大于等于80分的学生学号、姓名和性别,把值送往另一个已经存在的基本表STU(SNO,SNAME,SSEX)中

Create table STU 
(sno char(8), 
sname char(8), 
ssex char(2) 
)

insert into STU(sno,sname,ssex)
select distinct student.sno,sname,ssex
from student,sc 
where student.sno not in
(select sno from sc where grade<80) and student.sno=sc.sno

(13)建立一个sdeptgrade 表,包含(sdept,avggrade)字段,对每一个系,求学生的成绩,并把结果存入sdeptgrade

Create table sdeptgrade 
(sdept char(8) primary key, 
avggrade int; ) 

insert into sdeptgrade 
select student.sdept, avg(sc.grade) 
from student inner join SC on 
(student.sno = SC.sno) group by student.sdept;

实验总结:
1. 删除主键表数据如果有外键约束就会报错
2. 插入数据用insert into 表直接+表
3. 更新用update
4. 删除直接用delete 可以直接删除一行数据

实验六:综合应用**

实验过程及分析:
建立一个数据库和五张表的表结构
这里写图片描述
这里写图片描述
首先创建表:

create database Person
GO 
use person
GO
Create table employee
(
emp_no char(5) primary key,
emp_name char(10) not null,
Sex char(1) not null,
Dept char(4) not null,
Title char(6) not null,
data_hired datetime not null,
birthday datetime null,
salary int not null,
Addr char(50) null,
Mod_date datetime Default(getdate())
)
create table customer
(
cust_id char(5) primary key,
cust_name char(20) not null,
Addr char(40) not null,
tel_no char(10) not null,
Zip char(6) null
)
create table sales
(
order_no int primary key,
cust_id char(5) not null,
sale_id char(5) not null,
tot_amt numeric(9,2) not null,
order_date datetime not null,
ship_date datetime not null,
incoice_no char(10) not null
)
create table sale_item
(
order_no int not null,
prod_id char(5) not null,
Qty int not null,
unit_price numeric(9,2) not null,
order_date datetime null
constraint primary_sale primary key(order_no,prod_id)
)
create table product
(
prod_id char(5) not null primary key,
prod_naem char(20) not null
)

3、录入数据并实现实现如下查询
(1)查找定单金额高于20000的客户编号;

select cust_id from sales where tot_amt>2000

(2)选取销售数量最多的前5条订单订单号、数量;

select top 5 order_no,Qty from sale_item order by Qty DESC

(3)显示sale_item表中每种个别产品的订购金额总和,并且依据销售金额由大到小排
来显示出每一种产品的排行榜;

select prod_id, sum(Qty*unit_price) '金额' from sale_item group by prod_id order by '金额' DESC

(5)计算每一产品每月的销售金额总和,并将结果按销售(月份,产品编号)排序;

select "s2".月份,SUM("s2".tot_amt) '销售金额总和',"s1".prod_id '产品编号'
from sale_item "s1"
join (select MONTH(order_date) '月份',order_no,tot_amt from sales) "s2"
on "s1".order_no="s2".order_no
group by "s2".月份,"s1".prod_id
order by "s2".月份,"s1".prod_id

(6)检索单价高于2400元的的产品编号、产品名称、数量、单价及所在订单号;

select s.prod_id '产品编号',product.prod_name '产品名称',s.Qty '数量',s.unit_price '单价',s.order_no 
from product INNER JOIN 
(select order_no,prod_id,Qty,unit_price from sale_item where unit_price>2400) s
on product.prod_id=s.prod_id

(7)计算每一产品销售数量总和与平均销售单价;

select "s1".销售总额,"s2".unit_price '平均销售单价'
from(select SUM(Qty*unit_price) '销售总额',prod_id from sale_item group by prod_id) "s1"
join sale_item "s2"
on "s1".prod_id="s2" .prod_id

(8)创建一个视图,该视图只含上海客户信息,即客户号、客户姓名、住址。

CREATE VIEW view_name AS
select cust_id,cust_name,Addr from customer where Addr='上海'

实验总结:
1. 设置主键,自动为 not null
2. unique和主键区别:
unique:唯一并且 一张表可设置多个unique 可空 但是只能有一行数据空
主键: 唯一并且 一张表只能有一个主键
何时用到主键?
设置外键的时候需要主键 还有唯一标识一列的时候 比如身份证
3. 主键可通过 constraint 主键名 primary key(列,列)来设置*组合键*
4. 给表取别名的时候 不能用单引号,要用双引号或者不用引号 而给列取别名的时候可以选择单引号 或者 as 连接词 或者不用引号
5. where之类的范围时 列=单引号内容时值 双引号为列名
6. top 5表示 取前5名
7. 视图是为了保存一张表 下次查找该表可直接 使用 如本实验中:

select * from view_name

即可查看 视图

猜你喜欢

转载自blog.csdn.net/qq_38409944/article/details/80424093