超键:能唯一标识一个元祖的属性和属性集合
候选键:能唯一标识一个元祖的属性,且不包含其他多余的属性
主键:候选键中选一个出来作为主键
外键:关系表R中的一个属性(非超键)与另一个关系表R1中的主键存在对应关系
数据表操作
创建数据表:
create student
(Sno vachar(30) unique not null,
Sname varchar(30),
Sage int,
Sdept varchar(30)
);
删除数据表
drop table student;
修改表
alter student add Sphone varchar(30);
创建索引
create [unique] [cluster] index [索引名] on 表名(列名)
create unique index Scno on sc( sno desc,cno)
删除索引:
drop index student
查询操作:
单表查询
select Sno,Sname,Sage,Sdept from student;
select Sname student;
selcet * from;
计算与别名
select Sname Name,2021-Sage Age ,lower(Sdept) from Student;
去重
select distinct Sdept from student;
where
is null,not null
is like ,not like
between …and… not between and(包含)
and ,not ,or
int ,not in
条件查询
select * from student where sept not in (“软件工程”,“计算机工程”,“大数据工程”);
like
select * from student where name like ‘杨__’;一个汉字占两个字符
select * from student name like ‘杨%’;
排序
select * from student order by sage desc
select * from student order by sage asc
聚集函数
avg、count、max、min、sum’
count(*)不忽略空值
count ([distinct |all ] <列名>|*)
查询选修了课程学生总人数
select count(distinct sno) from sc;
分组查询
- 查询各系学生人数
select sdept, count(*) 人数 from student group by sdept;
- 查询各门课程平均成绩 以及人数
select cName,avg(grade) 平均成绩,count(sno) from cource group by cno;
对分组查询进行进一步筛选用having
-
查询平局成绩在80分以上的同学学号和平均成绩
select sno ,age(grade) 平均成绩 from student goup by sno having avg(grade)>=80;
-
查询选修课程超过三门,且都超过75分以上同学
select Sno from Sc where grade>=75 group by Sno having count(*)>3;
多表查询
1. 自然连接
查询选修了01课程且成绩都在75分以上的学生的姓名和成绩
select Sname,grade from student ,Sc where student.sno=Sc.sno and grade >=75;
2. 自连接查询
表本身的连接查询,比如找出相同列值的行,需要对表取别名限定
查询不同课程 成绩相同的学生学生号,课程号,成绩
select a.sno ,a.cno,b.cno,a.grade from student a,student b where a.grade=b.grade and a.sno =b.sno and a.cno!=b.cno;
3.外连接
左外连接
查看学生的选课情况及选修课的课程号情况
select student. * , Cno from student left outer join cource on student.sno=ccource.sno;
4.集合查询
< SELECT 语句> union [all] |intersect|except <select 语句>
all重复行不删除
except 排出不包含条件
-
查询既选了01 课程,又选了03 课程
select sno from sc
where cn0=01
intersect
select sno from sc where cno=03;
注意:这里不能使用and连接
嵌套查询
1.使用in谓语子查询
- 查询选修了01号课程的学生信息
select * from student where sno in (select sno from sc where cn0=‘01’)
- 查询选修数据库学生的基本信息
select * from student where Sno in
(select sno from sc where cno in
(select Cno from course where in Cname=“数据库”)
);
2.使用比较运算符的子查询
- 查询01号课程成绩高于李强的同学学号
select sno from sc where Cno =“01” and grade > (select grade from Sc where cno =‘01’ and sno =
(select sno from student whrere sname =‘李强’))
3.带any或all 谓语动词的子查询
返回值返回单值时可以用比较运算符,当返回结果为多值时要用any或者all
- 查询比所有计算机系学士年龄都大的学生信息
select * from student where Sage > all (select Sage from student where sedpt=‘计算机’)
- 查询课程号为02且成绩低于课程号为01的最高成绩的学生的学号、课程号、和成绩。
select Sno ,Cno,Grade from SC where Cno=‘02’ and grade < any
(select grade from sc where cno=‘01’);
使用聚集函数实现:
select Sno,Cno,Grade from where cno=‘02’ and grade <
(select max(grade) from sc where Cno=‘01’ )
聚集函数的效率更高
4.使用exits 谓语的子查询
- 查询选修了02号课程的学生姓名
select Sname from student where exits
(select * from SC where student.sno=sc.sno and cno=‘02’)
与其他的子查询方式不同,其他只查询一次,而这个查询多次,因为子查询与student.sno有关,父查询中的student表不同行有不同的sno,这类子查询称为相关子查询
- 查询选修了全部课程的学生姓名
select Sname from student where not exits
(select * from Course where not exits
(select * from sc where sno=student.sno and cno=Course.cno));