一、工具准备
安装mysql数据库管理系统;
安装Navicat for MySQL工具(是一套为MySQL设计的强大数据库管理及开发工具)。
二、数据准备
创建数据库
CREATE DATABASE test;
建表
CREATE TABLE students
(sno VARCHAR(3) NOT NULL,
sname VARCHAR(4) NOT NULL,
ssex VARCHAR(2) NOT NULL,
sbirthday DATE,
class VARCHAR(5));
CREATE TABLE courses
(cno VARCHAR(5) NOT NULL,
cname VARCHAR(10) NOT NULL,
tno VARCHAR(10) NOT NULL);
CREATE TABLE scores
(sno VARCHAR(3) NOT NULL,
cno VARCHAR(5) NOT NULL,
degree NUMERIC(10, 1) NOT NULL) ;
CREATE TABLE teachers
(tno VARCHAR(3) NOT NULL,
tname VARCHAR(4) NOT NULL, tsex VARCHAR(2) NOT NULL,
tbirthday DATE NOT NULL, prof VARCHAR(6),
depart VARCHAR(10) NOT NULL);
插入数据
INSERT INTO STUDENTS (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (108 ,'曾华' ,'男' ,'1977-09-01',95033);
INSERT INTO STUDENTS (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (105 ,'匡明' ,'男' ,'1975-10-02',95031);
INSERT INTO STUDENTS (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (107 ,'王丽' ,'女' ,'1976-01-23',95033);
INSERT INTO STUDENTS (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (101 ,'李军' ,'男' ,'1976-02-20',95033);
INSERT INTO STUDENTS (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (109 ,'王芳' ,'女' ,'1975-02-10',95031);
INSERT INTO STUDENTS (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (103 ,'陆君' ,'男' ,'1974-06-03',95031);
INSERT INTO COURSES(CNO,CNAME,TNO)VALUES ('3-105' ,'计算机导论',825);
INSERT INTO COURSES(CNO,CNAME,TNO)VALUES ('3-245' ,'操作系统' ,804);
INSERT INTO COURSES(CNO,CNAME,TNO)VALUES ('6-166' ,'数据电路' ,856);
INSERT INTO COURSES(CNO,CNAME,TNO)VALUES ('9-888' ,'高等数学' ,100);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (103,'3-245',86);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (105,'3-245',75);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (109,'3-245',68);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (103,'3-105',92);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (105,'3-105',88);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (109,'3-105',76);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (101,'3-105',64);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (107,'3-105',91);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (108,'3-105',78);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (101,'6-166',85);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (107,'6-106',79);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (108,'6-166',81);
INSERT INTO TEACHERS(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (804,'李诚','男','1958-12-02','副教授','计算机系');
INSERT INTO TEACHERS(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (856,'张旭','男','1969-03-12','讲师','电子工程系');
INSERT INTO TEACHERS(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (825,'王萍','女','1972-05-05','助教','计算机系');
INSERT INTO TEACHERS(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (831,'刘冰','女','1977-08-14','助教','电子工程系');
创建好的数据如下:
三、题目&答案&结果(如有不对的地方,还望不吝赐教)
- 查询Student表中的所有记录的Sname、Ssex和Class列。
SELECT sname,ssex,class
FROM students;
- 查询教师所有的单位即不重复的Depart列。
SELECT DISTINCT depart
FROM teachers;
- 查询Student表的所有记录。
SELECT *
from students;
- 查询Score表中成绩在60到80之间的所有记录。
SELECT *
from scores
WHERE degree BETWEEN 60 AND 80;
- 查询Score表中成绩为85,86或88的记录。
SELECT *
FROM scores
WHERE degree IN (85,86,88);
- 查询Student表中“95031”班或性别为“女”的同学记录。
SELECT *
FROM students
WHERE class='95031' OR ssex='女';
- 以Class降序查询Student表的所有记录。
SELECT *
FROM students
ORDER BY class DESC;
- 以Cno升序、Degree降序查询Score表的所有记录。
SELECT *
FROM scores
ORDER BY cno ASC,degree DESC;
- 查询“95031”班的学生人数。
SELECT COUNT(1) AS stuNum
FROM students
WHERE class='95031';
- 查询score表中的最高分的学生学号和课程号。
SELECT sno,cno
FROM scores
ORDER BY degree DESC
LIMIT 1;
- 查询‘3-105’号课程的平均分。
SELECT AVG(degree)
from scores
WHERE cno='3-105';
- 查询score表中至少有5名学生选修的并以3开头的课程的平均分数。
SELECT cno,AVG(degree)
from scores
WHERE cno LIKE '3%'
GROUP BY cno
HAVING COUNT(sno)>=5;
# LIKE 操作符用于在 WHERE 子句中搜索列中的指定模式。
# "%" 可用于定义通配符(模式中缺少的字符)
- 查询最低分大于70,最高分小于90的sno列。
SELECT sno
FROM scores
GROUP BY sno
HAVING MIN(degree)>70 AND MAX(degree)<90;
- 查询所有学生的sname、cno和degree列。
SELECT sname,cno,degree
FROM students INNER JOIN scores
ON students.sno=scores.sno;
GROUP BY sname;
- 查询所有学生的sno、cname和degree列。
SELECT sno,cname,degree
FROM scores INNER JOIN courses
on scores.cno=courses.cno
ORDER BY sno;
- 查询所有学生的sname、cname和degree列。
SELECT sname,cname,degree
FROM students INNER JOIN scores
ON (students.sno=scores.sno)
INNER JOIN courses
on (scores.cno=courses.cno)
ORDER BY sname;
- 查询“95033”班所选课程的平均分。
SELECT cno,AVG(degree)
FROM students INNER JOIN scores
on (students.sno=scores.sno)
WHERE class='95033'
GROUP BY cno
ORDER BY cno;
- 假设使用如下命令建立了一个grade表:
create table grade(low number(3,0),upp number(3),rank char(1));
insert into grade values(90,100,’A’);
insert into grade values(80,89,’B’);
insert into grade values(70,79,’C’);
insert into grade values(60,69,’D’);
insert into grade values(0,59,’E’);
commit;
现查询所有同学的sno、cno和rank列。
SELECT sno,cno,rank
FROM scores INNER JOIN grade
on (scores.degree>=grade.low AND degree<=grade.upp)
ORDER BY sno;
- 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
#答案一
SELECT *
FROM scores
WHERE cno='3-105' AND degree>(SELECT degree FROM scores WHERE cno='3-105' AND sno='109')
ORDER BY sno;
#答案二
SELECT s1.sno,s1.degree
FROM scores AS s1 INNER JOIN scores AS s2
ON(s1.cno=s2.cno AND s1.degree>s2.degree)
WHERE s1.cno='3-105' AND s2.sno='109'
ORDER BY s1.sno;
- 查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。
SELECT *
FROM scores a
WHERE sno IN (SELECT sno FROM scores GROUP BY sno HAVING count(cno)>1)
AND degree<( SELECT max(degree) FROM scores b WHERE b.cno = a.cno);
- 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
同19
- 查询和学号为108的同学同年出生的所有学生的sno、sname和sbirthday列。
SELECT sno,sname,sbirthday
FROM students
WHERE YEAR(sbirthday)=(SELECT YEAR(sbirthday) FROM students WHERE sno='108')
ORDER BY sno;
- 查询“张旭“教师任课的学生成绩。
SELECT sno,degree
FROM scores
WHERE cno=(SELECT cno FROM courses WHERE tno=(SELECT tno FROM teachers WHERE tname='张旭'))
ORDER BY sno;
- 查询选修某课程的同学人数多于5人的教师姓名。
SELECT tname
FROM teachers
WHERE tno=(SELECT tno FROM courses WHERE cno=(SELECT cno FROM scores GROUP BY(cno) HAVING COUNT(sno)>5));
- 查询95033班和95031班全体学生的记录。
SELECT students.*,scores.cno,scores.degree
FROM students,scores
WHERE class IN (95033,95031) AND students.sno=scores.sno
ORDER BY students.sno;
- 查询存在有85分以上成绩的课程cno.
SELECT DISTINCT cno
FROM scores
WHERE degree>85;
- 查询出“计算机系“教师所教课程的成绩表。
SELECT *
FROM scores
WHERE cno IN(SELECT cno FROM courses WHERE tno IN(SELECT tno FROM teachers WHERE depart='计算机系'));
- 查询“计算机系”与“电子工程系“不同职称的教师的tname和prof。
SELECT tname,prof
FROM teachers
WHERE depart='计算机系' AND prof NOT IN (SELECT prof FROM teachers WHERE depart='电子工程系');
- 查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的cno、sno和degree,并按Degree从高到低次序排序。
SELECT cno,sno,degree
FROM scores a
WHERE cno='3-105' AND degree>ANY(SELECT degree FROM scores b WHERE cno='3-245' AND b.sno=a.sno)
ORDER BY degree DESC;
# ANY 代表括号内的任意一个
- 查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的cno、sno和degree.
SELECT sno,cno,degree
FROM scores a
WHERE cno='3-105' AND degree>ALL(SELECT degree FROM scores b WHERE cno='3-245' AND b.sno=a.sno);
# ALL代表所有的
- 查询所有教师和同学的name、sex和birthday.
SELECT sname AS name,ssex AS sex,sbirthday AS birthday
FROM students
UNION
SELECT tname AS name,tsex AS sex,tbirthday AS birthday
FROM teachers;
# UNION 操作符用于合并两个或多个 SELECT 语句的结果集。请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每条 SELECT 语句中的列的顺序必须相同。
- 查询所有“女”教师和“女”同学的name、sex和birthday.
SELECT tname AS name, tsex AS sex, tbirthday AS birthday
FROM teachers
WHERE tsex='女'
UNION
SELECT sname AS name, ssex AS sex, sbirthday AS birthday
FROM students
WHERE ssex='女'
- 查询成绩比该课程平均成绩低的同学的成绩表。
SELECT *
FROM scores a
WHERE degree<(SELECT AVG(degree) FROM scores b WHERE b.cno=a.cno);
- 查询所有任课教师的tname和depart.
SELECT tname,depart
FROM teachers
WHERE tno IN(SELECT tno from courses);
- 查询所有未讲课的教师的tname和depart.
SELECT tname,depart
FROM teachers
WHERE tno NOT IN(SELECT tno from courses);
- 查询至少有2名男生的班号。
SELECT class
FROM students
WHERE ssex='男'
GROUP BY class
HAVING COUNT(sno)>=2;
- 查询Student表中不姓“王”的同学记录。
SELECT *
FROM students
WHERE sname NOT LIKE '王%';
- 查询Student表中每个学生的姓名和年龄。
SELECT sname,YEAR(NOW())-YEAR(sbirthday) AS sage
FROM students;
# YEAR(NOW())当前年,YEAR(sbirthday)生日取年
- 查询Student表中最大和最小的sbirthday日期值。
SELECT MAX(sbirthday),MIN(sbirthday)
FROM students;
- 以班号和年龄从大到小的顺序查询Student表中的全部记录。
SELECT *
FROM students
ORDER BY class DESC,sbirthday ASC;
- 查询“男”教师及其所上的课程。
SELECT tname,tsex,cname
FROM teachers INNER JOIN courses
WHERE tsex='男' and courses.tno=teachers.tno;
- 查询最高分同学的sno、cno和degree列。
SELECT sno,cno,degree
FROM scores
WHERE degree=(SELECT MAX(degree) FROM scores);
- 查询和“李军”同性别的所有同学的sname.
SELECT sname
FROM students
WHERE ssex=(SELECT ssex FROM students WHERE sname='李军');
- 查询和“李军”同性别并同班的同学sname.
SELECT sname
FROM students a
WHERE ssex=(SELECT ssex FROM students b WHERE sname='李军' AND a.class=b.class);
- 查询所有选修“计算机导论”课程的“男”同学的成绩表
SELECT *
FROM scores
WHERE cno=(SELECT cno FROM courses WHERE cname='计算机导论')AND sno=ANY(SELECT sno FROM students WHERE ssex='男');