一切从创建数据库开始
数据库→基本表(创建-增删改查)→视图(创建-增删改查)
int #4个字节
smallint #2个字节
tinyint #1个字节
float #浮点型
numeric/decimal #小数
char #固定长度
varchar #可变长度
text #文本
datetime #8个字节
year #年
date #天
time #时间
create database 数据库名
default character set utf8;
alter database 数据库名;
drop database 数据库名;
定义
create table 表名
(
列名 数据类型 约束条件,
列名 数据类型 约束条件,
……
);
创建学生表student
唯一性标识 id:整型,自动增长列,主键
学号 sno:9个字符,非空,不允许重复
姓名 sname:20个字符,非空
性别 ssex:2个字符,男或女
年龄 sage:tinyint,15到45之间
系别 sdept:20个字符,默认值为计算机系
create table student
(
id int auto_increment primary key,
sno char(9) not null unique,
sname char(20) not null,
ssex char(2) check(ssex in ('男','女')),
sage smallint check(sage>=15 and sage<=45),
sdept char(20) default '计算机系'
);
创建课程表course
课程号 cno:4个字符 主键
课程名 cname: 50个可变长字符,不允许为空
先修课程号 cpno: 4个字符
学分 ccredit: smallint
先修课程号 cpno 参照 课程号cno 取值
create table course
(
cno char(4) primary key,
cname varchar(50) not null,
cpno char(4),
ccredit smallint,
foreign key(cpno) references course(cno)
);
创建成绩表sc
学号 sno:9个字符
课程号 cno:4个字符
成绩 grade: smallint
sno,cno:组合主键
sno参照student表的sno取值
cno参照course表的cno取值
create table sc
(
sno char(9) not null,
cno char(4) not null,
grade smallint,
primary key(sno,cno),
foreign key(sno) references student(sno),
foreign key(cno) references course(cno)
);
定义
alter table 表名 modify 列名 新数据类型 约束;
例:修改student的sdept为40个字符宽度
alter table student modify sdept char(40);
定义
alter table 表名 add 列名 数据类型 约束;
例:在student中增加address列
alter table student add address varchar(50) not null;
定义
alter table 表名 drop column 列名;
例:删除student的sage列
alter table student drop column sage;
定义
alter table 表名 add constraint 约束名 约束类型(列名);
例:在student中增加sno为主关键字
alter table student add primary key(sno);
例:添加sc的sno列的外码约束为student的sno
alter table sc add foreign key(sno) references studnet(sno);
定义
alter table 表名 drop 约束类型 约束名
例:删除student的主键约束
alter table student drop primary key
定义
drop table 表名
定义
select 查询目标
from 目标所在表
where 条件
group by 分组依据
having 分组条件
order by 排序依据
limit 限制;
select sno,sname from student;
select * from student;
select * from course;
select * from sc;
select sname as 'student name',2023-sage 'birthday' from student;
select sname
from student
where sdept='计算机系';
select sname
from student
where sage<20;
select sname
from student
where sage<20 and sdept='计算机系';
select sname,sage
from student
where sage>=22 and sage<=24;
select sname,sage
from student
where sage between 22 and 24;
select sname,ssex
from student
where sdept='信息系'
or sdept='数学系'
or sdept='计算机系';
select sname,ssex
from student
where sdept in ('信息系','数学系','计算机系');
select *
from student
where sdept is null;
select sno
from sc
where grade is null;
select sno,grade
from sc
where grade is not null;
select distinct sno
from sc;
select *
from student
where sname like "王%";
select *
from student
where sname like "%王%";
select *
from student
order by sage asc;
select sno,grade
from sc
where sno='c02'
order by grade desc;
select count(*)
from student
group by sno;
select count(distinct sno)
from sc
group by sno;
select sum(grade)
from sc
where sno='200215121'
group by sno;
select avg(grade)
from sc
where cno='c01'
group by cno ;
select max(sage)
from student
group by sno;
select cno,avg(grade)
from sc
group by cno;
select cno,count(*)
from sc
group by cno;
select sno,count(*),avg(grade)
from sc
group by sno;
select sno
from sc
group by sno
having count(*)>3;
select sno,avg(grade),count(*)
from sc
group by sno
having count(*)>=4;
select sno,count(*),avg(grade)
from sc
where grade>=60
group by sno
having count(*)>3 and avg(grade)>70
order by avg(grade) desc;
select student.sno,student.sname,avg(sc.grade) from student,sc
where student.sno=sc.sno
group by student.sno
order by avg(sc.grade)
limit 3;
select sno,group_concat(cno)
from sc
where sno='200215121';
select sno,group_concat(cno)
from sc
group by sno;
select student.sno,student.sname,count(sc.cno),
group_concat(cname order by cname separator ',')
from sc,student,course
where student.sno=sc.sno
and course.cno=sc.cno
group by student.sno
order by sno asc;
select student.sno,sname,ssex,sage,sdept,cno,grade
from student left join sc on student.sno=sc.sno;
select student.sname,sc.cno,sc.grade
from student,sc
where student.sno=sc.sno and sdept='计算机系';
select student.sname,course.cname,sc.grade
from student,course,sc
where student.sno=sc.sno
and course.cno=sc.cno
and sdept='计算机系';
select student.sname,course.cname
from student,course,sc
where student.sno=sc.sno
and course.cno=sc.cno
and sdept='计算机系';
select student.sno,student.sname,sc.cno,sc.grade from student,sc
where student.sno=sc.sno;
select student.sno,student.sname,sc.cno,sc.grade
from student
left join sc
on student.sno=sc.sno;
select a.cno,a.cname,b.cname
from course a,course b
where a.cpno=b.cno;
select *
from student
where sdept=
(select sdept from student where sname='刘晨')
and sname!='刘晨';
select *
from student
where sage=
(select min(sage) from student);
select *
from student
order by sage asc
limit 1;
select student.sno,student.sname
from student,sc
where student.sno=sc.sno and grade>90;
select sno,sname
from student
where sno in
(select sno from sc where grade>90);
select student.sno,student.sname,sc.grade
from student,sc
where student.sno=sc.sno and grade>90;
select sno,grade
from sc
where grade>
(select avg(grade) from sc where cno='c02' group by cno);
select a.sno,a.grade
from sc a
where grade>
(select avg(grade) from sc b
where a.cno=b.cno
group by b.cno);
select sno,cno,grade
from sc a
where grade>
(select avg(grade)
from sc b
where b.sno=a.sno);
select sname,sage
from student
where sdept!='信息系'and
sage<
(select max(sage)
from student
where sdept='信息系');
select sname,sage
from student
where sdept!='信息系' and
sage<
any(select age
from student
where sdept='信息系');
select sname
from student
where exists
(select * from sc where cno='c02' and sno=student.sno);
select sname from
student where sno in
(select sno from sc where cno='c02');
select sname from sc,student
where sc.sno=student.sno and sno='c02';
select sname from student
where not exists
(select * from sc where cno='c02' and sno=student.sno);
select sname
from student,sc
where student.sno=sc.sno and cno!='c02';
select sname
from student,sc,course
where student.sno=sc.sno and course.cno=sc.cno and cname='数据库';
select sname
from student
where exists
(select * from sc where cno=(select cno from course where cname='数据库'));
select sno from sc where cno='c01' and cno='c02';
select distinct sno
from sc where
sno in (select sno from sc where sno='c01') and
sno in (select sno from sc where sno='c02');
select distinct sno
from sc a
where exists
(select * from sc b where b.sno=a.sno
and cno='c01') and exists
(select * from sc c where c.sno=a.sno
and cno='c02');
select distinct sno from sc a where
exists(select * from sc b where b.sno=a.sno and
cno=(select cno from course where cname='数据库'))
and exists(select * from sc c where c.sno=a.sno and cno=(select cno from course where cname='数据结构'));
select sname from student
where not exists(select * from course
where not exists(select * from sc
where sno=student.sno and cno=course.cno);
select distinct sno from sc a
where not exists(select * from sc b
where sno='200215122' and
not exists(select * from sc c where
c.sno=a.sno and c.cno=b.cno);
(select sname as name from student)
union
(select cname from course);
定义
insert into 表名 (列名) values (值列表);
1. 将新生记录为(200821105,陈冬,男,18,信息系)插入到student表中
insert into student (sno,sname,ssex,sage,sdept)
values('200821105','陈冬','男',18,'信息系');
insert student
values('200821105','陈冬','男',18,'信息系');
2. 将与刘晨同一个系的新生记录(200821105,陈冬,男,18)插入到student表中
insert into student set sno='200821105',sname='陈冬',ssex='男',sage=18,
sdept=(select sdept from student where sname='刘晨');
3. 将新生记录(200821107,陈冬,男,18,信息系),(200821118,刘晨,男,18,信息系)一起插入到student表中
insert into student
values('200821107','陈冬','男',18,'信息系'),('200821108','刘晨','男',18,'信息系');
4. 在sc表中插入一新记录学号为200821105,课程为c01
insert into sc (sno,cno) values('200821105','c01',null);
insert into sc values('200821105','c01',null);
5. 备份c01的成绩到c01_cj新表中
create table c01_cj like sc;
insert into c01_cj
select * from sc where cno='c01';
定义
update 表名 set 列名=表达式 where 条件;
1. 将计算机系全体学生的成绩加5分
update sc set grade=grade+5
where sno in (select sno from student where sdept='计算机系');
2. 将平均成绩80分以上的学生成绩加3分
update sc set grade=grade+3
where sno in
(select sno from sc group by sno having avg(grade)>80);
3. 将数据结构3-5名学生的成绩加5分
update sc set grade=grade+5 where sno=
(select sno from course where course.cno=sc.cno and cname='数据结构'
order by grade desc limit 2,4);
定义
delete from 表名 where 条件;
1. 删除学号为200215121的学生的记录
delete from sc where sno='200215121';
delete from student where sno='200215121';
2. 删除所有学生的记录
delete from student;
3. 删除计算机系所有学生的选课记录
delete from sc where sno in(select sno from student where sdept='计算机系');
定义
create view 视图名 as 子查询;
1. 建立系名为计算机系的学生的视图
create view student_view as
select sno,sname,ssex,sdept from student
where sdept='计算机系';
2. 建立系名为计算机系的学生的视图,要求对视图的更新进行检查
create view student_view as
select sno,sname,ssex,sdept
from student
where sdept='计算机系'
with check option;
3. 建立信息系选修了c01课程的学生的视图
create view student_view as
select student.sno,student.sname,sc.grade
from student,sc
where student.sno=sc.sno
and sdept='信息系'
and cno='c01';
同基本表操作一样
数据库→基本表(创建-增删改查)→视图(创建-增删改查)