还内容针对学校所讲内容进行复习,个人感觉这个顺序会稍微好一些,也都是比较基础的内容,如果有写的不太好的地方,请及时指教(针对于学习过数据库)
(登录)不得先登上去
mysql -u root -p创建一个命名空间
create schema students;
use students;创建三个表
student
+-----------+-------+------+------+-------+
| Sno | Sname | Sex | Sage | Sdept |
+-----------+-------+------+------+-------+
| 201215121 | 李勇 | 男 | 20 | CS |
| 201215122 | 刘晨 | 女 | 19 | CS |
| 201215123 | 王敏 | 女 | 18 | MA |
| 201215124 | 张立 | 男 | 19 | IS |
+-----------+-------+------+------+-------+
cs
+-----+------------+------+---------+
| Cno | Cname | Cpno | Ccredit |
+-----+------------+------+---------+
| 1 | 数据库 | 5 | 4 |
| 2 | 数学 | NULL | 2 |
| 3 | 信息系统 | 1 | 4 |
| 4 | 操作系统 | 6 | 3 |
| 5 | 数据结构 | 7 | 4 |
| 6 | 数据处理 | NULL | 2 |
| 7 | PASCAL语言 | 6 | 4 |
+-----+------------+------+---------+
sc
+-----------+------+-------+
| Sno | Cno | Grade |
+-----------+------+-------+
| 201215121 | 1 | 92 |
| 201215121 | 2 | 85 |
| 201215121 | 3 | 88 |
| 201215122 | 2 | 90 |
| 201215122 | 3 | 80 |
+-----------+------+-------+我就写一个哦,剩下的模仿着写就好(可以给主键添加约束)
mysql> create table student(
-> Sno varchar(10)
-> ,Sname varchar(10),
-> Sex varchar(5),
-> Sage int(10),
-> Sdept varchar(10));给student添加值即可(有两种添加方式)
insert into student values('201215121','李勇','男',20,'CS');
insert into student values('201215122','刘晨','女',19,'CS');
insert into student values('201215123','王敏','女',18,'MA');
insert into student(sno,sname,sex,sage,sdept) values('201215124','张立','男',19,'IS');最后查一下就好啦
mysql> select * from student;
+-----------+-------+------+------+-------+
| Sno | Sname | Sex | Sage | Sdept |
+-----------+-------+------+------+-------+
| 201215121 | 李勇 | 男 | 20 | CS |
| 201215122 | 刘晨 | 女 | 19 | CS |
| 201215123 | 王敏 | 女 | 18 | MA |
| 201215124 | 张立 | 男 | 19 | IS |
+-----------+-------+------+------+-------+必须确保三个表格都出来
修改表中的值
修改学号为201215121的年龄为22
mysql> update student set sage=22
-> where sno='201215121';修改所有同学的年龄为0
update student set Sage=0;修改所有同学的年龄加上20岁
update student set Sage=Sage+20;查询
简单的查询我就不写了,如果不会我严重怀疑你没认真学呀!
字符串匹配
mysql> select * from student
-> where Sname like '刘%';空值的查询:
is null/is not null 查询缺少成绩的学生的学号以及课程号
mysql> select * from course
-> where Cpno is null;查所有的有成绩的学生的学号以及课程号
mysql> select sno,cno
-> from sc
-> where grade is not null;多重条件的查询:
查询计算机系,数学系,学生的姓名以及性别
mysql> select * from student
-> where sdept in ('CS','MA');无非就是 and or 在where中加上
mysql> select * from student
-> where sdept='CS'or sdept='MA';order by(排序的语句)默认就是升序(ASC),DESC是降序
查询选修了3号课程的学生的学号及其成绩,查询结 果按分数降序排列
mysql> select * from sc
-> where cno='3'
-> order by grade desc;mysql> select * from course
-> order by Ccredit desc;查询全体学生情况,查询结果按所在系的系号升序 排列,同一系中的学生按年龄降序排列。
mysql> select * from student
-> order by sdept,sage desc;聚集函数:
查询选修了课程的学生人数。
mysql> select count(distinct Sno) as '总人数'//distinct可以去重
-> from sc;查询学生201215012选修课程的总学分数。
mysql> select sum(Ccredit) as '总学分'
-> from Course,sc
-> where Sno='201215122' and sc.cno=course.cno;avg,max,min就不多介绍了
group by 分组函数
查询平均成绩大于等于90分的学生学号和平均成绩
mysql> select sno,avg(grade)
-> from sc
-> group by sno
-> having avg(grade)>=80
-> ;查询选修了3门以上课程的学生学号。
mysql> select sno
-> from sc
-> group by sno
-> having count(cno)>=3;带有any或者all的子查询
查询非计算机科学系中比计算机科学系任意一个 学生年龄小的学生姓名和年龄
mysql> select sname,sage
-> from student
-> where sage<any(select sage from student where sdept='CS')
-> and sdept<>'CS';
mysql> select sname,sage
-> from student
-> where sage<(select min(sage) from student where sdept='CS')
-> and sdept<>'CS';带有比较运算符的子查询
找出每个学生超过他选修课程平均成绩的课程号
mysql> select sno,cno
-> from sc x
-> where grade>=(select avg(grade) from sc y where y.sno=x.sno);集合查询
union(不包含重复元素) union all(包含重复元素)
查询计算机科学系的学生及年龄不大于19岁的学生。
mysql> select * from student
-> where sdept='CS'
-> union
-> select * from student
-> where sage<=19;
+-----------+-------+------+------+-------+
| Sno | Sname | Sex | Sage | Sdept |
+-----------+-------+------+------+-------+
| 201215121 | 李勇 | 男 | 23 | CS |
| 201215122 | 刘晨 | 女 | 20 | CS |
| 201215123 | 王敏 | 女 | 19 | MA |
+-----------+-------+------+------+-------+找出每个学生超过他选修课程平均成绩的课程号
mysql> select sno,cno
-> from sc
-> join (select sno,avg(grade) as avg_grade from sc group by sno) as avg_sc
-> on sc.sno=avg_sc.sno
-> where sc.grade<=avg_sc.avg_grade;
ERROR 1052 (23000): Column 'sno' in field list is ambiguous带有IN谓词的子查询
查询与“刘晨”在同一个系学习的学生。
mysql> select sno,sname
-> from student
-> where sdept in(select sdept from student where sname='刘晨');
+-----------+-------+
| sno | sname |
+-----------+-------+
| 201215121 | 李勇 |
| 201215122 | 刘晨 |
+-----------+-------+建立一个新的表格(子表)
对每一个系,求学生的平均年龄
mysql> create table Dept_age(
-> sdept char(15),
-> avg_avg int(3));
mysql> insert into dept_age(sdept, AVG_age)
-> select sdept,avg(Sage)
-> from student
-> group by sdept;删除操作
删除学号为201215128的学生记录
delete from student where sno='201215128';
















