一、数据库的创建、引用、删除
#DDL——数据库的创建、引用、删除
CREATE DATABASE classic50
SHOW DATABASES
DROP DATABASE classic50
USE classic50
二、表的增、删、改
#DDL——数据表的创建
CREATE TABLE student(
s_id INT(10),
s_name VARCHAR(20) NOT NULL DEFAULT '',
s_birth VARCHAR(20) NOT NULL DEFAULT '',
s_sex CHAR(4) NOT NULL DEFAULT '',
PRIMARY KEY(s_id));
CREATE TABLE course(
c_id INT(10),
c_name VARCHAR(20) NOT NULL DEFAULT '',
t_id INT(10) NOT NULL ,
PRIMARY KEY(c_id));
CREATE TABLE teacher(
t_id INT(10),
t_name VARCHAR(20) NOT NULL DEFAULT '',
PRIMARY KEY(t_id));
CREATE TABLE score(
s_id INT(10),
c_id INT(10),
s_score INT(20) ,
PRIMARY KEY(s_id));
#添加主键
ALTER TABLE student
ADD PRIMARY KEY(s_id)
#设置非空及默认值
ALTER TABLE student
ALTER COLUMN s_name SET DEFAULT ''
#删除表
DROP TABLE student,course,teacher,score
#修改属性
ALTER TABLE student
MODIFY COLUMN s_name VARCHAR(20)
#添加列删除列
ALTER TABLE student
ADD COLUMN aaa INT(10)
ALTER TABLE student
DROP COLUMN aaa
#表改名
ALTER TABLE students RENAME student
三、数据插入
# INSERT INTO tb_name VALUES('','','','')
-- 插入学生表测试数据
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');
-- 课程表测试数据
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
-- 教师表测试数据
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
-- 成绩表测试数据
insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);
四、查询
#模糊查询
-- 1.查询姓“张”的老师的个数
SELECT COUNT(IF(t_name LIKE '张%',1,NULL))
FROM teacher;
SELECT COUNT(t_name)
FROM teacher
WHERE t_name LIKE '张%';
-- 2.查询名字里有“风”的学生名单
SELECT COUNT(IF(s_name LIKE '%风%',1,NULL))
FROM student;
#日期类型
-- 3.1981年出生的学生名单(注:Student表中s_birth列的类型是datetime)
#修改列属性#
ALTER TABLE student
MODIFY COLUMN s_birth datetime;
SELECT * FROM student
WHERE YEAR(s_birth)=1981;
SELECT * FROM student
WHERE s_birth LIKE '1981%';
#聚合函数sum
-- 4.查询课程编号为“02”的总成绩
SELECT c_id, SUM(s_score)
FROM score
WHERE c_id='02';
#聚合函数count 去重操作distinct
-- 5.查询选了课程的学生人数
SELECT COUNT(DISTINCT s_id) AS 学生数 FROM score;
#分组(group),聚合函数(最大值max,最小值min)
-- 6.查询各科成绩最高和最低的分: 以如下的形式显示:课程ID,最高分,最低分
ALTER TABLE course
MODIFY COLUMN c_id VARCHAR(10);
SELECT c_id AS 课程ID, MAX(s_score) AS 最高分, MIN(s_score) AS 最低分
FROM score
GROUP BY c_id
-- 7.查询每门课程被选修的学生数
SELECT c_id, COUNT(s_id)
FROM score
GROUP BY c_id
-- 8.查询男生、女生人数
SELECT s_sex, COUNT(s_sex)
FROM student
GROUP BY s_sex
#分组(group),聚合函数avg, HAVING子句
-- 9.查询平均成绩大于60分的学生的学号和平均成绩
SELECT s_id, ROUND(AVG(s_score),1)
FROM score
GROUP BY s_id
HAVING AVG(s_score)>60
-- 10.查询至少选修两门课程的学生学号
SELECT s_id
FROM score
GROUP BY s_id
HAVING COUNT(c_id)>=2
-- 11.查询两门以上不及格课程的同学的学号及其平均成绩
SELECT s_id,ROUND(AVG(s_score),1)
FROM score
GROUP BY s_id
HAVING COUNT(IF(s_score<60,1,NULL))>=2
-- 12.查询同名同性学生名单并统计同名人数
SELECT s_name, COUNT(s_name)
FROM student
GROUP BY s_name
HAVING COUNT(s_name)>1
#ORDER BY 排序
-- 13.查询每门课程的平均成绩,结果按平均成绩升序排序,平均成绩相同时,按课程号降序排列
SELECT c_id, AVG(s_score)
FROM score
GROUP BY c_id
ORDER BY AVG(s_score) ASC, c_id DESC
-- 14.查询不及格的课程并按课程号从大到小排列
SELECT c_id,s_score
FROM score
WHERE s_score<60
ORDER BY c_id DESC
-- 15.检索课程编号为“04”且分数小于60的学生学号,结果按按分数降序排列
SELECT s_id
FROM score
WHERE c_id=4 AND s_score<60
ORDER BY s_score DESC
-- 16.统计每门课程的学生选修人数(超过5人的课程才统计)。
-- 要求输出课程号和选修人数,查询结果按人数降序排序,若人数相同,按课程号升序排序
SELECT c_id, COUNT(s_id)
FROM score
GROUP BY c_id
HAVING COUNT(s_id)>5
ORDER BY COUNT(s_id) DESC ,c_id ASC
#谓词(in)
-- 17.查询所有课程成绩小于60分的学生的学号、姓名
SELECT DISTINCT student.s_id, s_name
FROM student JOIN score ON student.s_id=score.s_id
WHERE s_score<60
SELECT s_id, s_name
FROM student
WHERE s_id IN (SELECT s_id FROM score WHERE s_score<60)
-- 18.查询没有学全所有课的学生的学号、姓名
SELECT s_id, s_name
FROM student
WHERE s_id IN (
SELECT s_id
FROM score
GROUP BY s_id
HAVING COUNT(c_id) < (SELECT COUNT(c_id) FROM course))
-- 19.查询出只选修了两门课程的全部学生的学号和姓名
SELECT s_id, s_name
FROM student
WHERE s_id IN (
SELECT s_id
FROM score
GROUP BY s_id
HAVING COUNT(c_id)=2)
-- 20.查询课程编号为03且课程成绩在80分以上的学生的学号和姓名
SELECT s_id, s_name
FROM student
WHERE s_id IN(
SELECT s_id
FROM score
WHERE c_id=03 AND s_score>80)
#联接 JOIN
-- 21.查询课程编号为“001”的课程比“002”的课程成绩高的所有学生的学号
SELECT a.s_id
FROM(
(SELECT * FROM score WHERE c_id=001) AS a
INNER JOIN
(SELECT * FROM score WHERE c_id=002) AS b
ON a.s_id=b.s_id
)
WHERE a.s_score>b.s_score
-- 22.按平均成绩从高到低,按如下形式显示:学生ID,有效课程数,有效平均分
SELECT s_id AS 学生ID,COUNT(c_id) AS 有效课程数, AVG(s_score) AS 有效平均分
FROM score
GROUP BY s_id
ORDER BY AVG(s_score) DESC
#分段统计 CASE case_value WHEN when_value THEN statement_list ELSE statement_list END CASE;
-- 23.使用分段[100-85],[85-70],[70-60],[<60]来统计各科成绩,分别统计各分数段人数:课程ID和课程名称
SELECT c.c_id AS '课程ID', c_name AS '课程名称',
COUNT(CASE WHEN s_score >85 AND s_score<=100 THEN 1 ELSE NULL END) AS '[100-85]',
COUNT(CASE WHEN s_score >70 AND s_score<=85 THEN 1 ELSE NULL END) AS '[85-70]',
COUNT(CASE WHEN s_score >60 AND s_score<=70 THEN 1 ELSE NULL END) AS '[70-60]',
COUNT(CASE WHEN s_score<=60 THEN 1 ELSE NULL END) AS '[<60]'
FROM course c JOIN score s ON c.c_id=s.c_id
GROUP BY c.c_id,c_name
-- 24.查询出每门课程的及格人数和不及格人数
SELECT c.c_id, c_name,
COUNT(CASE WHEN s_score>=60 THEN 1 ELSE NULL END) AS 及格人数,
COUNT(CASE WHEN s_score<60 THEN 1 ELSE NULL END) AS 不及格人数,
COUNT(s.s_score)
FROM course c JOIN score s ON c.c_id=s.c_id
GROUP BY c.c_id
-- 25.查询出每门课程的及格百分数 子查询结果作为查询表
SELECT c_id, (a.及格人数/a.总人数)*100 AS 及格百分数
FROM(
SELECT c_id,
COUNT(CASE WHEN s_score>=60 THEN 1 ELSE NULL END) AS 及格人数,
COUNT(DISTINCT s_id) AS 总人数
FROM score
GROUP BY c_id
) AS a
未完待续···
网友评论