MySQL练习
练习一(基础增删改查)
-
建立一个张表,表名为stu, 一共四个字段(id编号, name姓名, age年龄, height身高), 字段要求如下:
id 类型: 整数,主键,自增长;
name 类型: 字符串 长度10 , 值唯一, 不能重复,
age 类型: 无符号小整数, 默认值20,
height 类型: 小数,3位整数,2位小数, 非空;
CREATE TABLE stu ( id INT PRIMARY key auto_increment, NAME VARCHAR (10) UNIQUE, age TINYINT UNSIGNED DEFAULT (20), height DECIMAL (5, 3) NOT NULL );
-
用insert语句插入记录
INSERT INTO stu VALUE (0, '王五', 25, 1.81); INSERT INTO stu VALUE (0, '赵六', 27, 1.76); -- 自增长要用0或者null来占位 INSERT INTO stu (name, age, height) VALUES ('张三', 20, 1.72), ('李四', 30, 1.78); -- 同时插入两条记录
-
查询表中所有记录
SELECT * FROM stu;
-
修改表中记录
UPDATE stu set age = 21 where name = '王五'; UPDATE stu set age = age + 10 WHERE name = '李四';
-
删除表中记录
DELETE from stu WHERE name = '赵六'; DELETE FROM stu; -- 删除表中记录 TRUNCATE TABLE stu; -- 保留表结构,自增长字段从1开始,处理速度大于delete DROP TABLE stu; -- 删除表 DROP TABLE IF EXISTS stu;
练习二(进阶增删改查)
-
数据准备
/*建学生信息表student*/ drop table if exists student; create table student ( sno varchar(20) not null primary key,/*学生编号*/ sname varchar(20) not null,/* 学生姓名*/ ssex varchar(20) not null,/* 学生性别*/ sbirthday datetime,/* 学生生日*/ class varchar(20)/* 班级*/ ); /* 建立教师表 */ drop table if exists teacher; create table teacher ( tno varchar(20) not null primary key,/*教师编号 */ tname varchar(20) not null,/* 教师名称*/ tsex varchar(20) not null,/* 教师性别*/ tbirthday datetime,/* 教师生日*/ prof varchar(20),/* 教师职称*/ depart varchar(20) not null/* 教师部门*/ ); /* 建立课程表course */ drop table if exists course; create table course ( cno varchar(20) not null primary key,/*课程编号 */ cname varchar(20) not null,/* 课程名称*/ tno varchar(20) not null/* 教师编号*/ ); /* 建立成绩表*/ drop table if exists score; CREATE TABLE score ( sno VARCHAR (20) NOT NULL ,/*学生编号 */ cno VARCHAR (20) NOT NULL,/*课程编号 */ degree DECIMAL /*成绩 */ ); /* 添加学生信息*/ insert into student values('108','曾华','男','1977-09-01','95033'); insert into student values('105','匡明','男','1975-10-02','95031'); insert into student values('107','王丽','女','1976-01-23','95033'); insert into student values('101','李军','男','1976-02-20','95033'); insert into student values('109','王芳','女','1975-02-10','95031'); insert into student values('103','陆君','男','1974-06-03','95031'); /* 添加教师信息 */ insert into teacher values('804','李诚','男','1958-12-02','副教授','计算机系'); insert into teacher values('856','张旭','男','1969-03-12','讲师','电子工程系'); insert into teacher values('825','王萍','女','1972-05-05','助教','计算机系'); insert into teacher values('831','刘冰','女','1977-08-14','助教','电子工程系'); insert into teacher values('859','张文','男','1968-10-14','教授','计算机系'); /* 添加课程信息 */ insert into course values('3-105','计算机导论','825'); insert into course values('3-245','操作系统','804'); insert into course values('6-166','数字电路','856'); insert into course values('9-888','高等数学','831'); /* 添加成绩信息 */ insert into score values('108','3-245','86'); insert into score values('105','3-245','75'); insert into score values('109','3-245','68'); insert into score values('103','3-105','92'); insert into score values('105','3-105','88'); insert into score values('109','3-105','76'); insert into score values('103','3-105','64'); insert into score values('105','3-105','91'); insert into score values('109','3-105','78'); insert into score values('103','6-166','85'); insert into score values('105','6-166','79'); insert into score values('109','6-166','81');
-
teacher表增加记录
INSERT INTO teacher VALUE('861','刘羽','女','1978-09-14','助教','电子工程系');
-
查询student表的sname,ssex,class字段
SELECT sname,ssex,class from student;
-
查询teacher表中的部门depart,不显示重复值
SELECT distinct depart from teacher;
-
查询score表中的degree在60-80的所有记录
SELECT * from score where degree >=60 and degree <=80;
-
student表中姓名sname“王丽”学生改名“王美丽”
update student set sname = '王美丽' where sname = '王丽';
-
score表中degree 85分以上的课程编号cno
SELECT cno from score where degree > 85;
-
查询score表中degree成绩为(85,86,88)的记录
SELECT * from score where degree in (85,86,88);
-
查询student表中“95031”班class或性别ssex为“女”的同学记录
SELECT * from student where class = 95031 or ssex = '女';
-
删除teacher表中教师编号tno等于’861’的记录
DELETE from teacher where tno = '861';
-
以Class降序查询Student表的所有记录
SELECT * FROM student ORDER BY class desc;
-
以Cno升序、Degree降序查询Score表的所有记录
select * from score order by cno asc ,degree desc;
-
查询Student表中class为“95031”的学生总人数
select class,count(class) from student where class = '95031';
-
查询Student表中95033班和95031班全体学生的记录
select * from student where class = '95033' or class = '95031';
-
查询Student表中不姓“王”的同学记录
select * from student where sname not like '王%';
-
以年龄从小到大的顺序查询Student表中的全部记录
select * from student ORDER BY sbirthday desc;
-
查询score表中每门课的平均成绩
SELECT cno,avg(degree) FROM score GROUP BY cno;
-
查询score表中分数degree大于70,小于90的sno列
SELECT sno from score where degree > 70 and degree < 90;
练习三(自关联,子查询,连接查询,分页查询)
-
查询成绩表score中学号sno为“109”、课程号cno为“3-105”的所有记录
select * from score where sno = 109 and cno = '3-105';
-
查询部门depart为“计算机系”与“电子工程系“教师的Tname和Prof
select tname,prof from teacher where depart = '计算机系' or depart = '电子工程系';
-
查询“男”教师的姓名tname及其所上的课程名称cname
SELECT t.tname,c.cname from teacher t INNER JOIN course c on t.tno = c.tno where tsex = '男';
-
查询最高分同学的Sno、Cno和Degree
SELECT st.sname,sc.sno,sc.cno,sc.degree from student st INNER JOIN score sc on st.sno = sc.sno ORDER BY degree desc LIMIT 1;
-
查询和“李军”同性别的所有同学的Sname
SELECT sname from student where ssex = (SELECT ssex from student where sname = '李军');
-
查询至少有2名男生的class
SELECT class from student WHERE ssex = '男' GROUP BY class having count(*) >= 2;
-
查询所有学生的姓名Sname、课程编号Cno和成绩Degree
SELECT st.sname,sc.cno,sc.degree from student st INNER JOIN score sc on st.sno = sc.sno;
-
查询所有学生的学生编号Sno、学生姓名sname, 课程名称Cname和成绩Degree
SELECT st.sno,st.sname,c.cname,sc.degree from student st INNER JOIN score sc on st.sno = sc.sno INNER JOIN course c on sc.cno = c.cno;
-
查询“95033”班学生的平均分
SELECT avg(degree) from score where sno in (SELECT sno from student where class = '95033');
-
查询课程编号cno 为’3-105’中成绩degree最高学生的姓名sname和性别ssex以及成绩degree
SELECT st.sname,st.ssex,sc.degree FROM student st INNER JOIN score sc on st.sno = sc.sno where sc.cno = '3-105' ORDER BY degree DESC LIMIT 1;
练习四(综合强化)
-
查询Score表中的最高分的学生学号sno和课程号cno。(子查询或者排序)
SELECT sno,cno from score where degree = (select max(degree) from score); SELECT sno,cno from score ORDER BY degree desc LIMIT 1;
-
查询所有未讲课的教师的Tname和Depart
SELECT t.tname from teacher t LEFT JOIN course c on t.tno=c.tno where cno is null;
-
查询部门depart为“计算机系“教师所教课程的成绩degree
SELECT t.tname,s.degree from teacher t INNER JOIN course c on t.tno = c.tno INNER JOIN score s on c.cno = s.cno WHERE t.depart = '计算机系';
-
查询成绩比平均成绩低的学生信息
SELECT * from student st INNER JOIN score sc on st.sno = sc.sno where degree < (select avg(degree) from score);
-
查询所有任课教师的Tname和Depart(没有课程编号cno的讲师不显示)
SELECT * from teacher t LEFT JOIN course c on t.tno = c.tno WHERE cno is not null;
-
查询课程名cname“计算机导论”的“男”同学姓名sname,和课程成绩degree
SELECT st.sname,sc.degree from student st INNER JOIN score sc on st.sno = sc.sno INNER JOIN course c on sc.cno = c.cno where ssex = '男' and cname = '计算机导论';
-
查询“张旭“教师任课的学生名称sname和成绩degree
SELECT st.sname,s.degree from teacher t INNER JOIN course c on t.tno = c.tno INNER JOIN score s on s.cno = c.cno INNER JOIN student st on st.sno = s.sno where t.tname = '张旭';
-
查询选修某课程的同学人数多于5人的教师姓名tname
SELECT t.tname,c.cname from student st INNER JOIN score sc on st.sno = sc.sno INNER JOIN course c on c.cno = sc.cno INNER JOIN teacher t on t.tno = c.tno GROUP BY cname HAVING count(*) > 5;