一、實(shí)驗(yàn)內(nèi)容
1、用SQL語(yǔ)句表示下列操作,在學(xué)生庫(kù)中實(shí)現(xiàn)數(shù)據(jù)查詢。
(1)求數(shù)學(xué)系學(xué)生的學(xué)號(hào)和姓名。
(2)求選修了課程的學(xué)生學(xué)號(hào)。
(3)求選修001號(hào)課程的學(xué)生學(xué)號(hào)和成績(jī),并要求對(duì)查詢結(jié)果按成績(jī)降序排列,如果成績(jī)相同則按學(xué)號(hào)升序排列。
(4)求選修課程001且成績(jī)?cè)?0~90分之間的學(xué)生學(xué)號(hào)和成績(jī),并將成績(jī)乘以系數(shù)0.8 輸出。
(5)求數(shù)學(xué)系或計(jì)算機(jī)系姓張的學(xué)生的信息。
(6)查看選修了課程、但沒(méi)有成績(jī)學(xué)生的學(xué)號(hào)、姓名、課程號(hào)和所在是系部。
(7)查詢學(xué)生的學(xué)號(hào)、姓名、課程名和成績(jī)。
(8)分別實(shí)現(xiàn)學(xué)生和系的交叉連接、內(nèi)連接、外連接。
2、在SQL Server查詢分析器中使用IN、比較符、ANY或ALL和EXISTS操作符進(jìn)行嵌套查詢操作。具體內(nèi)容如下:
用SQL語(yǔ)句表示,在學(xué)生選課庫(kù)中實(shí)現(xiàn)其數(shù)據(jù)嵌套查詢操作。
(1)求選修了高等數(shù)學(xué)的學(xué)生學(xué)號(hào)和姓名。
(2)求001課程的成績(jī)高于張力的學(xué)生學(xué)號(hào)和成績(jī)。
(3)求其他系中年齡小于計(jì)算機(jī)系年齡最大者的學(xué)生。
(4)求其他系中比計(jì)算機(jī)系學(xué)生年齡都小的學(xué)生。
(5)求選修了001課程的學(xué)生姓名。
(6)求沒(méi)有選修001課程的學(xué)生姓名。
(7)查詢選修了全部課程的學(xué)生的姓名。
二、實(shí)驗(yàn)代碼
-- theme 數(shù)據(jù)庫(kù)實(shí)驗(yàn)二
--author dahua
-- data by 2022.6.11
--創(chuàng)建一個(gè)學(xué)生數(shù)據(jù)庫(kù)
create database Student;
--1.創(chuàng)建第一個(gè)表:學(xué)生表
create table student1(
sno char(9) primary key, --將學(xué)號(hào)設(shè)置為主碼
sname char(20),
ssex char(2),
sage int,
sdept char(20)
);
--插入元組
insert
into student1(sno,sname,ssex,sage,sdept)
values('01','張力','男',20,'計(jì)算機(jī)系'),
('02','雷杰多','男',19,'體育系'),
('03','賽迦','男',19,'電子工程系'),
('04','諾亞','男',19,'計(jì)算機(jī)系'),
('05','瑪麗','女',19,'數(shù)學(xué)系'),
('06','蓋亞','男',20,'化工化學(xué)系'),
('07','張三','男',20,'計(jì)算機(jī)系'),
('08','賽羅','男',19,'數(shù)學(xué)系'),
('09','雷歐','男',20,'數(shù)學(xué)系'),
('10','張四','男',18,'數(shù)學(xué)系');
--修改表中數(shù)據(jù)
update student1 set sage=18 where sno='09';
--創(chuàng)建索引
create unique index stusno1 on student1(sno);
--2.創(chuàng)建第二個(gè)表:課程表
create table course1(
cno char(9) primary key, --將課程號(hào)設(shè)置為主碼
cname char(30),
cpno char(30), --先行課
cpoint int --學(xué)分
);
--插入元組
insert
into course1(cno,cname,cpno,cpoint)
values('001','高等數(shù)學(xué)A1','無(wú)',4.5),
('002','大學(xué)英語(yǔ)1','無(wú)',3),
('003','C++程序設(shè)計(jì)A1','無(wú)',3),
('004','計(jì)算機(jī)導(dǎo)論','無(wú)',1.5),
('005','軍事理論','無(wú)',1),
('006','思想道德修養(yǎng)','無(wú)',3),
('007','高等數(shù)學(xué)A2','001',4),
('008','大學(xué)英語(yǔ)2','002',2),
('009','大學(xué)物理C','無(wú)',4),
('010','C++程序設(shè)計(jì)A2','003',2);
--創(chuàng)建索引
create unique index coucno1 on course1(cno);
--3.創(chuàng)建第三個(gè)表:選課表
create table sc1(
sno char(9),
cno char(9),
grade int
primary key(sno,cno),
foreign key(sno) references student1(sno), --外碼
foreign key(cno) references course1(cno), --外碼
);
--插入元組
insert
into sc1(sno,cno,grade)
values('01','001',92),
('01','002',95),
('01','003',90),
('01','004',92),
('01','005',94),
('01','006',90),
('01','007',92),
('01','009',96),
('01','010',91),
('02','001',86),
('02','002',97),
('02','003',86),
('02','004',99),
('02','005',97),
('02','006',92),
('02','007',86),
('02','008',89),
('02','009',90),
('02','010',86),
('03','001',96),
('04','001',96),
('04','004',97),
('04','007',85),
('04','008',86),
('04','003',96),
('05','001',84),
('06','001',84),
('06','003',null),
('06','007',97);
--4.創(chuàng)建第四個(gè)表:系部表
create table department1(
dno char(9) primary key,
dname char(20),
manager char(20)
);
--插入元組
insert
into department1(dno,dname,manager)
values('202001','機(jī)械工程系','蕭峰'),
('202002','電子工程系','段譽(yù)'),
('202003','自動(dòng)化系','虛竹'),
('202004','化學(xué)與化工系','王語(yǔ)嫣'),
('202005','計(jì)算機(jī)系','阿朱'),
('202006','環(huán)境與安全系','阿紫'),
('202007','材料工程系','木婉清'),
('202008','理學(xué)系','瑛姑'),
('202009','外語(yǔ)系','慕容復(fù)'),
('202010','設(shè)計(jì)藝術(shù)系','游坦之');
--創(chuàng)建索引
create unique index departmentdno1 on department1(dno);
--1.(1)求數(shù)學(xué)系學(xué)生的學(xué)號(hào)和姓名。
select sno,sname from student1 where sdept='數(shù)學(xué)系';
--1.(2)求選修了課程的學(xué)生學(xué)號(hào)。
select distinct sno from sc1;
--1.(3)求選修001號(hào)課程的學(xué)生學(xué)號(hào)和成績(jī),并要求對(duì)查詢結(jié)果按成績(jī)降序排列,如果成績(jī)相同則按學(xué)號(hào)升序排列。
select sno,grade from sc1 where cno='001' order by grade desc,sno asc;
--1.(4)求選修課程001且成績(jī)?cè)?span id="n5n3t3z" class="token number">80~90分之間的學(xué)生學(xué)號(hào)和成績(jī),并將成績(jī)乘以系數(shù)0.8 輸出。
select sno,0.8*grade from sc1 where cno='001' and grade between 80 and 90;
--1.(5)求數(shù)學(xué)系或計(jì)算機(jī)系姓張的學(xué)生的信息。
select * from student1 where sdept in('數(shù)學(xué)系','計(jì)算機(jī)系') or sname like '張_%';
--1.(6)查看選修了課程、但沒(méi)有成績(jī)學(xué)生的學(xué)號(hào)、姓名、課程號(hào)和所在是系部。
select sc1.sno,student1.sname,cno,student1.sdept from student1,sc1 where sc1.cno is not null and grade is null and sc1.sno=student1.sno;
--1.(7)查詢學(xué)生的學(xué)號(hào)、姓名、課程名和成績(jī)。
select sc1.sno,sname,cname,sc1.grade from student1,sc1,course1 where sc1.sno=student1.sno and sc1.cno=course1.cno;
--1.(8)分別實(shí)現(xiàn)學(xué)生和系的交叉連接、內(nèi)連接、外連接。
select student1.*,department1.* from department1 cross join student1;
select student1.*,department1.* from student1 inner join department1 on student1.sdept = department1.dno;
select student1.*,department1.* from student1 left outer join department1 on student1.sdept = department1.dno;
--2.(1)求選修了高等數(shù)學(xué)A1的學(xué)生學(xué)號(hào)和姓名。
select sno,sname from student1 where sno in(select sno from sc1 where cno=(select cno from course1 where cname='高等數(shù)學(xué)A1'));
--2.(2)求001課程的成績(jī)高于張力的學(xué)生學(xué)號(hào)和成績(jī)。
select a.sno,a.grade from sc1 a,sc1 b where a.cno=001 and b.cno=001 and a.grade>b.grade and a.sno!=b.sno and b.sno=(select sno from student1 where sname='張力');
--2.(3)求其他系中年齡小于計(jì)算機(jī)系年齡最大者的學(xué)生。
select sname,sage,sdept from student1 where Sage < any(select max(sage) from student1 where sdept='計(jì)算機(jī)系' ) and sdept <> '計(jì)算機(jī)系';
--2.(4)求其他系中比計(jì)算機(jī)系學(xué)生年齡都小的學(xué)生。
select sname,sage,sdept from student1 where Sage < all(select sage from student1 where sdept='計(jì)算機(jī)系' ) and sdept <> '計(jì)算機(jī)系';
--2.(5)求選修了001課程的學(xué)生姓名。
select sname from student1,sc1 where student1.sno = sc1.sno and cno='001';
--2.(6)求沒(méi)有選修001課程的學(xué)生姓名。
select sname from student1 where not exists (select * from sc1 where sno=student1.sno and cno='1');
--2.(7)查詢選修了全部課程的學(xué)生的姓名。
select sname from student1 where not exists(select * from course1 where not exists(select * from sc1 where sno=student1.sno and CNo = course1.cno));
三、運(yùn)行結(jié)果
3.1問(wèn)題一的運(yùn)行結(jié)果圖
3.1.1
3.1.2
3.1.3
3.1.4
3.1.5
3.1.6
3.1.7
3.1.8
3.2問(wèn)題二的運(yùn)行結(jié)果圖
3.2.1
3.2.2
3.2.3
3.2.4
3.2.5
3.2.6文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-422685.html
3.2.7文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-422685.html
到了這里,關(guān)于數(shù)據(jù)庫(kù) 實(shí)驗(yàn)二 查詢的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!