MySQL練習3
來源網路,整理後分享
student為學生表,course為課程表,score為成績表,其中sno欄位關聯student的sno欄位,cno欄位關聯course的cno欄位
執行的sql語句:
DROP DATABASE IF EXISTS test; CREATE DATABASE test; use test; CREATE TABLE STUDENT (SNO CHAR(7) NOT NULL primary key, SNAME VARCHAR(4) NOT NULL, SSEX VARCHAR(2) NOT NULL, sage int(3) NOT NULL, SDEPT VARCHAR(5) NOT NULL) default charset=utf8; CREATE TABLE COURSE (CNO CHAR(3) NOT NULL primary key, CNAME varchar(15) NOT NULL, hours int(3) ) default charset=utf8; CREATE TABLE SCORE (SNO CHAR(7) NOT NULL, CNO CHAR(3) NOT NULL, Grade int(3), primary key(SNO, CNO), foreign key(SNO) references STUDENT(SNO), foreign key(CNO) references COURSE(CNO)) default charset=utf8; INSERT INTO STUDENT (SNO,SNAME,SSEX,SAGE,SDEPT) VALUES ('9512101' ,'李勇' ,'男' ,19,'計算機系'); INSERT INTO STUDENT (SNO,SNAME,SSEX,SAGE,SDEPT) VALUES ('9512102' ,'劉晨' ,'男' ,20,'計算機系'); INSERT INTO STUDENT (SNO,SNAME,SSEX,SAGE,SDEPT) VALUES ('9512103' ,'王敏' ,'女' ,19,'計算機系'); INSERT INTO STUDENT (SNO,SNAME,SSEX,SAGE,SDEPT) VALUES ('9521101' ,'張立' ,'男' ,22,'資訊系'); INSERT INTO STUDENT (SNO,SNAME,SSEX,SAGE,SDEPT) VALUES ('9521102' ,'吳賓' ,'女' ,21,'資訊系'); INSERT INTO STUDENT (SNO,SNAME,SSEX,SAGE,SDEPT) VALUES ('9521103' ,'張海' ,'男' ,20,'資訊系'); INSERT INTO STUDENT (SNO,SNAME,SSEX,SAGE,SDEPT) VALUES ('9531101' ,'錢小力' ,'女' ,18,'數學系'); INSERT INTO STUDENT (SNO,SNAME,SSEX,SAGE,SDEPT) VALUES ('9531102' ,'王大力' ,'男' ,19,'數學系'); INSERT INTO COURSE(CNO,CNAME,hours)VALUES ('C01' ,'計算機文化學',70); INSERT INTO COURSE(CNO,CNAME,hours)VALUES ('C02' ,'VB',90); INSERT INTO COURSE(CNO,CNAME,hours)VALUES ('C03' ,'計算機網路',80); INSERT INTO COURSE(CNO,CNAME,hours)VALUES ('C04' ,'資料庫基礎',108); INSERT INTO COURSE(CNO,CNAME,hours)VALUES ('C05' ,'高等數學',180); INSERT INTO COURSE(CNO,CNAME,hours)VALUES ('C06' ,'資料結構',72); INSERT INTO SCORE(SNO,CNO,GRADE)VALUES ('9512101','C01',90); INSERT INTO SCORE(SNO,CNO,GRADE)VALUES ('9512101','C02',86); INSERT INTO SCORE(SNO,CNO,GRADE)VALUES ('9512101','C06', null); INSERT INTO SCORE(SNO,CNO,GRADE)VALUES ('9512102','C02',78); INSERT INTO SCORE(SNO,CNO,GRADE)VALUES ('9512102','C04',66); INSERT INTO SCORE(SNO,CNO,GRADE)VALUES ('9521102','C01',82); INSERT INTO SCORE(SNO,CNO,GRADE)VALUES ('9521102','C02',75); INSERT INTO SCORE(SNO,CNO,GRADE)VALUES ('9521102','C04',92); INSERT INTO SCORE(SNO,CNO,GRADE)VALUES ('9521102','C05',50); INSERT INTO SCORE(SNO,CNO,GRADE)VALUES ('9521103','C02',68); INSERT INTO SCORE(SNO,CNO,GRADE)VALUES ('9521103','C06',null); INSERT INTO SCORE(SNO,CNO,GRADE)VALUES ('9531101','C01',80); INSERT INTO SCORE(SNO,CNO,GRADE)VALUES ('9531101','C05',95); INSERT INTO SCORE(SNO,CNO,GRADE)VALUES ('9531102','C05',85);
練習題目:
1.分別查詢學生表和學生修課表中的全部資料。
學生表: select * from student;
學生修課表: select * from course;
2.查詢成績在70到80分之間的學生的學號、課程號和成績。
select sno, cno, grade from score where grade between 70 and 90;
3.查詢C01號課程成績最高的分數, 考出該分數的學生編號,課程編號
select sno, cno, max(grade) from score where cno = 'C01';
4.查詢學生都選修了哪些課程,要求列出學生姓名,課程名稱,課程號。
select st.sname, co.cname, sc.cno from student st, score sc, course co where
st.sno = sc.sno and co.cno = sc.cno;
5.查詢修了C02號課程的所有學生的平均成績、最高成績和最低成績。
select avg(grade), max(grade), min(grade) from score where cno = 'C02';
6.統計每個系的學生人數。
select sdept, count(sno) from student group by sdept;
7.統計每門課程的修課人數和考試最高分。
select cno, count(sno), max(grade) from score group by cno;
8.統計每個學生的選課門數,列出學生編號和選課門數,並按選課門數的遞增順序顯示結果。
select sno, count(cno) from score group by sno order by count(cno);
9.統計全部選修課的學生總數和考試的平均成績。
select count(distinct sno), avg(grade) from score;
10.查詢選課門數超過2門的學生的學生編號, 平均成績和選課門數。
select sno, avg(grade), count(cno) from score group by sno having count(cno) > 2;
11.列出總成績超過200分的學生,要求列出學號、總成績。
select st.sno, sum(sc.grade) from student st, score sc where st.sno = sc.sno
group by sc.sno having sum(sc.grade) > 200;
12.查詢選修了c02號課程的學生的姓名和所在系。
select st.sname, st.sdept from student st, score sc where st.sno = sc.sno
and sc.cno = 'C02';
13.查詢成績80分以上的學生的姓名、課程號和成績,並按成績的降序排列結果。
select st.sname, sc.cno, sc.grade from student st, score sc where st.sno = sc.sno and sc.grade > 80 order by sc.grade desc;
14.查詢計算機系男生修了"資料庫基礎"的學生的姓名、性別、成績。
select st.sname, st.ssex, sc.grade, co.cname from student st, score sc, course co where st.sno = sc.sno and sc.cno = co.cno and co.cname='資料庫基礎' and st.ssex='男' and st.sdept = '計算機系';
15.查詢哪些學生的年齡相同,要求列出年齡相同的學生的姓名和年齡。
select distinct st1.sname, st1.sage from student st1, student
st2 where st1.sno != st2.sno and st1.sage = st2.sage;
16.查詢哪些課程沒有人選,要求列出課程號和課程名。
select cno, cname from course where cno not in (select distinct cno from score);
-- where 只能用於內連線
select * from score sc right join course co on sc.cno = co.cno where sc.sno
is null;
17.查詢有考試成績的所有學生的姓名、修課名稱及考試成績
--要求將查詢結果放在一張新的永久表(假設新表名為newsc)中。
create table new_sc
select st.sname, co.cname, sc.grade from student st, score sc, course co where st.sno = sc.sno and sc.cno = co.cno and sc.grade is not null;
18.分別查詢資訊系和計算機系的學生的姓名、性別、修課名稱、修課成績,
--並以系名、姓名、性別、修課名稱、修課成績的順序顯示各列。
select st.sdept, st.ssex, co.cname, sc.grade from student st, score sc,
course co where st.sno = sc.sno and sc.cno = co.cno and st.sdept='資訊系' or
st.sdept='計算機系';
19.實現如下查詢:
--(1) 查詢選修了C01號課程的學生的姓名和所在系。
select st.sname, st.sdept from student st, score sc where st.sno = sc.sno
and sc.cno = 'C01';
--(2) 查詢數學系成績80分以上的學生的學號、姓名。
select distinct st.sno, st.sname from student st, score sc where st.sno =
sc.sno and sc.grade > 80 and st.sdept='數學系';
--(3) 查詢計算機系學生所選的課程名.
select distinct co.cname from student st, score sc, course co where st.sno = sc.sno and sc.cno = co.cno and st.sdept='計算機系';
20.將計算機系成績高於80分的學生的修課情況(學生姓名, 成績,課程編號)插入到另一張表中,在插入資料過程中建表。
create table new_table
select st.sname, sc.grade, sc.cno from student st, score sc where st.sno =
sc.sno and st.sdept = '計算機系' and sc.grade > 80;
select distinct co.cno, co.cname from student st, score sc, course co where
st.sno = sc.sno and sc.cno = co.cno and st.sdept='計算機系';
21.刪除修課成績小於50分或者沒有分數的學生的修課記錄
delete from score where grade < 50 or grade is null;
22.將所有選修了"c01"課程的學生的成績加10分。
update score set grade = grade + 10 where cno = 'c01';