指定某列不能存null值
create table [表名](id int not null);
保证某列的每一行的值是唯一的
create table [表名](id int unique);
当列没有被赋值时,指定默认值,不指定时为null
create table [表名](id int default 10);
主键----not null 和 unique 的结合
一张表中只有一列是主键,主键相当于一行记录的确定身份的唯一标识,就是“身份证号码”
外键 两张表之间的关联关系
常见于学生表和班级表,订单表和商品表
foreign key(列) references 主表(列)
将查询结果插入到表中,可用于将一张表的数据插入到另一张表中
查询结果的列数和类型必须和插入数据的表相匹配
varchar(20) ---->varchar(100) , OK
varchar(100)---->varchar(20),可能OK,关键看查询记录中对应数据的大小
insert into [表名](列名,列名...) select
聚合函数 | |
---|---|
count( ) | 返回查询到的数据的行数,null不会计入到结果 |
sum( ) | 返回查询到数据的总和 |
avg( ) | 返回查询到数据的平均值 |
max( ) | 返回查询到数据的最大值 |
min( ) | 返回查询到数据的最小值 |
行和行之间的运算
select sum(math) from [表名]
相当于先执行select math from [表名]
再根据其查询的结果,进行计算,展现计算结果
在backboard>90的记录中找到最小值
先执行 select backboard from player where backboard>90;
在进行计算,得出结果
看其他列如何展现,展现第一行的对应值
使用group by 进行分组查询
分组聚合:
根据指定列进行分组,指定列中值相同的记录(行)分到一组中,在针对每一组分别执行聚合函数
select sum(列名) from [表名] group by [列名],[列名]...
将球员按照相同的position分组,计算各组的sum(score)
having针对聚合后的数据进行筛选
where根据需求,也可以和group by 搭配,是针对聚合前的数据进行筛选的
select sum() from [表名] where... group by...
select sum() from [表名] group by...having...
//不同位置分数<70的球员的数据总和
//先筛选出score<70的记录,再分组,再计算
select position,sum(score) from NBA where score<70 group by position
//显示分数总和>100的位置
//先分组,再计算,再筛选
select position,sum(score) from NBA group by position having sum(score)>100
多张表联合查询,运用笛卡尔积
笛卡尔积
select from 表1,表2 where...
select from 表1 join 表2 on ...
//两张表进行笛卡尔积
select * from score,student;
//去掉无效数据,留下每个学生成绩与自己的信息的组合
select * from score,student where score.student_id = student.id;
//找到威少
select * from score,student where score.student_id = student.id and student.name = "威少";
select score.score,score.course_id from score,student where score.student_id = student.id and student.name = "威少";
更牛逼一点,查询每个科目的成绩
select score.score,student.name,course.name from score,student,course where score.student_id = student.id and student.name = "威少";
select score.score,course.name from score,student,course where score.student_id = student.id and student.name = "威少" and course.id = score.course_id;
2.查询每个同学的总成绩及个人信息
select student.name,student.class_id,student.email,sum(score.score) from score,student where student.id = student_id group by student_id;
select * from student,course,score;
select * from student,course,score where student.id = score.student_id and course.id = score.course_id;
select student.id,student.name,student.email,student.class_id,score.score,course.name from student,course,score where student.id = score.student_id and course.id = score.course_id;
select * from [表1] left join [表2] on ...
select * from [表1] right join [表2] on ...
一张表自己和自己作笛卡尔积
用于行和行的比较
SQL语句只能进行列和列的比较,不能进行 行和行 的比较,使用自连接解决这一问题
select * from score as s1,score as s2 where s1.student_id = s2.student_id;
select * from score as s1,score as s2 where s1.student_id = s2.student_id and s1.course_id = 1 and s2.course_id = 5;
select * from score as s1,score as s2 where s1.student_id = s2.student_id and s1.course_id = 1 and s2.course_id = 5 and s1.score > s2.score;
先执行子查询,再执行外层查询
select * from student where class_id = (select class_id from student where name = "哈登");
2. 多行子查询(in)
查询 “ 物理化学 ” 和 “ 马原 ” 的成绩信息
select * from score where course_id in (select id from course where name = "物理化学" or name = "马原");
select * from student,class where class_id = class.id and class.name = "火箭"
(2)、再查成绩
select * from student,class,score where class_id = class.id and class.name = "火箭" and student_id = student.id
(3)、求平均值
select student.*,avg(score) from student,class,score where class_id = class.id and class.name = "火箭" and student_id = student.id;
2、利用子查询
select * from score where score>(select avg(score) from student,class,score where class_id = class.id and class.name = "火箭" and student_id = student.id);
把多个select查询的结果,合并成为一个结果
关键词 union
select... from [表1] union select...from [表2]
露从今夜白,月是故乡明,中秋快乐!