温故而知新,可以为师矣~~
编译器:datagrip
数据库语言:MySQL
插入元素
向数据库中插入单行元素:
Insert into student (sno, sname, ssex, sage, sdept)
VALUES ('201315121', '李勇', '男', '20', 'CS');
插入多行元素:
Insert into student (sno, sname, ssex, sage, sdept)
VALUES ('201315121', '李勇', '男', '20', 'CS'),
('201215122', '刘晨', '女', '19', 'CS'),
('201215123', '王敏', '女', '18', 'MA'),
('201215126', '张立', '男', '19', 'IS');
删除元素
删除course表的cno值为1的所有行:
DELETE FROM course
WHERE
course.cno = 1;
删除student表的所有数据:
##删除student表的所有数据
delete
from sc;
删除student表中学号为201215122的该行元素:
##删除student表中学号为201215122的该行元素
delete
from student
where sno=201215122;
删除sc表所有学生的选课记录:
##删除sc表所有学生的选课记录
delete
from sc
where sno in
(
select sno
from student
where sdept='cs'
);
查找元素
简单的单表查询:
##查询student表的所有元素
select *
from student;
##查询student表的sno和sage的所有元素
select sno,sage
from student;
##查询经过计算的值
select sname,2024-sage as 出生年份
from student;
##查询去掉重复值的sc表中的sno
select distinct sno
from sc;
##查询student表中的ssex为'男'的sname
select sname
from student
where ssex='男';
##查询student表中sage为19的人的所有信息
select *
from student
where sage=19;
##查询grade大于60的所有人的学号,用distinct短语使得多门课程超过60分的也只显示一次学号
select distinct sno
from sc
where grade>60;
##查询student表中的sage介于18和19之间的所有人的信息
select *
from student
where sage between 18 and 19;
##查询student表中的sage不在18和19之间的所有人的信息
select *
from student
where sage not between 18 and 19;
##查询student表中sname为'李勇'的所有信息
select *
from student
where sname like '李勇';
##查询student表中sname的第一个字为'李'的学生的所有信息
select *
from student
where sname like '李%';
##查询student表中sname的第一个字为'李'且全名为两个汉字的学生的所有信息
select *
from student
where sname like '李_';
##查询student表中sname的第一个字不为'李'的学生的所有信息
select *
from student
where sname not like '李%';
##查询sc表中所有有成绩的学生的所有信息
select *
from sc
where grade is not null;
##查询sc表并降序排序
select *
from sc
order by grade desc;
##查询sc表并升序排序
select *
from sc
order by grade asc;
##查询sc表所有人数(有重复值)
select count(distinct sno)
from sc;
##查询student表所有人数
select count(*)
from student;
##查询sc表中各个cno以及相应的sno总数
select cno,count(sno)
from sc
group by cno;
##用group by语句按sno分组,再用聚集函数count对每组计数,having短语给出选择组的条件(人数大于等于2)满足时,才会被选出来
select sc.sno
from sc
group by sno
having count(sc.sno)>=2;
连接查询
##连接查询
##查询每个人的所有选课情况的所有信息
select *
from sc,student,course
where sc.cno=course.cno and sc.sno=student.sno;
##自身连接查询course表的每一门课的间接先修课(先修课的先修课)
select first.cno,second.cpno
from course first,course second
where first.cpno=second.cno;
##左外连接
select student.sno,sname,ssex,sage,sdept,cno,grade
from student left outer join sc on (student.sno=sc.sno);
##右外连接
select *
from student right outer join sc on (student.sno=sc.sno);
嵌套查询
##嵌套查询
##查询student表sno中sc表的cno为'2'的sno值,得出sname
select sname
from student
where sno in
(
select sno
from sc
where cno='2'
);
##等价?,连接student和sc表,查询sc表中cno为'2'的值,得出sname
select student.sname
from student,sc
where student.sno=sc.sno and sc.cno='2';
完整代码
use stu;
#创建student表
Create table student(
Sno char(9) not null,
Sname char(20) null,
Ssex char(2) null,
Sage smallint null,
Sdept char(20) null,
Primary key(sno),
Unique index sname_unique(sname asc)visible);
#插入student数据
Insert into student (sno, sname, ssex, sage, sdept)
VALUES ('201215121', '李勇', '男', '20', 'CS'),
('201215122', '刘晨', '女', '19', 'CS'),
('201215123', '王敏', '女', '18', 'MA'),
('201215126', '张立', '男', '19', 'IS');
#创建课程表
Create table stu.course(
cno char(4) not null,
cname char(40) not null,
cpno char(4) not null,
ccredit smallint null,
Primary key(cno));
#插入课程表的数据
insert into stu.course(cno,cname,cpno,ccredit) values('1','数据库','5','4'),
('2','数学','2','2'),
('3','信息系统','1','4'),
('4','操作系统','6','3'),
('5','数据结构','7','4'),
('6','数据处理','2','2'),
('7','PASCAL语言','6','4');
#删除数据库某列(cno)的元素的一行
DELETE FROM course
WHERE
course.cno = 1;
#创建sc表
Create table stu.sc(
sno char(9) not null,
cno char(4) not null,
grade smallint null,
Primary key(sno,cno));
#添加数据到sc表
Insert into stu.sc(sno,cno,grade) values('201215121','1','92'),
('201215121','2','85'),
('201215121','3','88'),
('201215122','2','90'),
('201215122','3','88');
create table stu.sbak like stu.student;
ALTER TABLE stu.sbak CHANGE Sage Syear SMALLINT;
ALTER TABLE stu.sbak MODIFY COLUMN Syear datetime;
desc stu.sbak;
Alter table stu.sbak add index sbak_1 (sdept);
show index from stu.sbak;
##删除student表的所有数据
delete
from student;
##删除student表中学号为201215122的该行元素
delete
from student
where sno=201215122;
##删除sc表所有学生的选课记录
delete
from sc
where sno in
(
select sno
from student
where sdept='cs'
);
##查询student表的所有元素
select *
from student;
##查询student表的sno和sage的所有元素
select sno,sage
from student;
##查询经过计算的值
select sname,2024-sage as 出生年份
from student;
##查询去掉重复值的sc表中的sno
select distinct sno
from sc;
##查询student表中的ssex为'男'的sname
select sname
from student
where ssex='男';
##查询student表中sage为19的人的所有信息
select *
from student
where sage=19;
##查询grade大于60的所有人的学号,用distinct短语使得多门课程超过60分的也只显示一次学号
select distinct sno
from sc
where grade>60;
##查询student表中的sage介于18和19之间的所有人的信息
select *
from student
where sage between 18 and 19;
##查询student表中的sage不在18和19之间的所有人的信息
select *
from student
where sage not between 18 and 19;
##查询student表中sname为'李勇'的所有信息
select *
from student
where sname like '李勇';
##查询student表中sname的第一个字为'李'的学生的所有信息
select *
from student
where sname like '李%';
##查询student表中sname的第一个字为'李'且全名为两个汉字的学生的所有信息
select *
from student
where sname like '李_';
##查询student表中sname的第一个字不为'李'的学生的所有信息
select *
from student
where sname not like '李%';
##查询sc表中所有有成绩的学生的所有信息
select *
from sc
where grade is not null;
##查询sc表并降序排序
select *
from sc
order by grade desc;
##查询sc表并升序排序
select *
from sc
order by grade asc;
##查询sc表所有人数(有重复值)
select count(distinct sno)
from sc;
##查询student表所有人数
select count(*)
from student;
##查询sc表中各个cno以及相应的sno总数
select cno,count(sno)
from sc
group by cno;
##用group by语句按sno分组,再用聚集函数count对每组计数,having短语给出选择组的条件(人数大于等于2)满足时,才会被选出来
select sc.sno
from sc
group by sno
having count(sc.sno)>=2;
##连接查询
##查询每个人的所有选课情况的所有信息
select *
from sc,student,course
where sc.cno=course.cno and sc.sno=student.sno;
##自身连接查询course表的每一门课的间接先修课(先修课的先修课)
select first.cno,second.cpno
from course first,course second
where first.cpno=second.cno;
##左外连接
select student.sno,sname,ssex,sage,sdept,cno,grade
from student left outer join sc on (student.sno=sc.sno);
##右外连接
select *
from student right outer join sc on (student.sno=sc.sno);
##嵌套查询
##查询student表sno中sc表的cno为'2'的sno值,得出sname
select sname
from student
where sno in
(
select sno
from sc
where cno='2'
);
##等价?,连接student和sc表,查询sc表中cno为'2'的值,得出sname
select student.sname
from student,sc
where student.sno=sc.sno and sc.cno='2';