当前位置: 首页 > news >正文

MySQL知识大总结(进阶)

一,数据库的约束

1,约束类型

1not null非空约束,标记这个字段不可以为空
2unique唯一约束,标记这个字段的值是该列唯一的值,在这一列的其他行,不可以与该字段相等
3default

默认约束,在该字段没有赋值时,使用默认值填充该列

4primary key主键约束,相当于not null  +  unique
5foreign key外键约束,与其他表的主键简历联系,在添加或修改数据是,会根据主外键关系检查数据是否合法

 1,not null 

试着使用not null 来创建数据表 

create table if not exists student(-> id bigint not null,-> name varchar(20) not null-> );
 desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | bigint      | NO   |     | NULL    |       |
| name  | varchar(20) | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+

我们看到在null的列,两个字段都不允许为空,我们添加两行不为空和一行为空的数据来试一下。

insert into student values (1,'张三'),(2,'李四');
select * from student;
+----+--------+
| id | name   |
+----+--------+
|  1 | 张三   |
|  2 | 李四   |
+----+--------+

成功添加

insert into student values (null,null);
ERROR 1048 (23000): Column 'id' cannot be null

在新增数据发生报错,它不允许字段id为空。

2,unique

在试着用unique来创建一个表

create table if not exists student(-> id bigint unique,-> name varchar(20) unique-> );
desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | bigint      | YES  | UNI | NULL    |       |
| name  | varchar(20) | YES  | UNI | NULL    |       |
+-------+-------------+------+-----+---------+-------+

key 变成了唯一类型

我们添加三个数据,两个相同的,一个不同的

insert into student values (1,'张三'),(2,'李四');
Query OK, 2 rows affected (0.00 sec)

成功添加了,

insert into student values (1,'王五');
ERROR 1062 (23000): Duplicate entry '1' for key 'student.id'
 insert into student values (3,'张三');
ERROR 1062 (23000): Duplicate entry '张三' for key 'student.name'

两个字段都设置为唯一类型的,所以无论哪一个字段重复了都不可以。

3,default

 create table if not exists student(-> id bigint default 0,-> name varchar(20) default '无名氏' unique-> );

这些数据库约束是可以一起使用的,

desc student;
+-------+-------------+------+-----+-----------+-------+
| Field | Type        | Null | Key | Default   | Extra |
+-------+-------------+------+-----+-----------+-------+
| id    | bigint      | YES  |     | 0         |       |
| name  | varchar(20) | YES  | UNI | 无名氏    |       |
+-------+-------------+------+-----+-----------+-------+

我们来添加两个啥都没有的数据

insert into student (id) values (1);
Query OK, 1 row affected (0.00 sec)mysql> insert into student (name) values ('张三');
Query OK, 1 row affected (0.00 sec)
select * from student;
+------+-----------+
| id   | name      |
+------+-----------+
|    1 | 无名氏    |
|    0 | 张三      |
+------+-----------+

空缺的元素都由默认值填补了。

4,primary key 

可以使用auto_increment来在主键后面,这样就代表这个字段是自增主键,不用对其进行赋值,在其他列的插入时,就会自行生成对应的id,但是如果我们这行sql语句写错了的话id是不会接着上一行数据的id继续的,

 create table if not exists student(-> id bigint primary key auto_increment,-> name varchar(20)-> );

复合主键

create table if not exists student(-> id bigint,-> name varchar(20),-> primary key(id,name)-> );
desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | bigint      | NO   | PRI | NULL    |       |
| name  | varchar(20) | NO   | PRI | NULL    |       |
+-------+-------------+------+-----+---------+-------+

5,foreign key

create table if not exists student(-> id bigint primary key auto_increment,-> name varchar(20),-> class_id bigint,-> foreign key (class_id) references class(id)-> );
desc student;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | bigint      | NO   | PRI | NULL    | auto_increment |
| name     | varchar(20) | YES  |     | NULL    |                |
| class_id | bigint      | YES  | MUL | NULL    |                |
+----------+-------------+------+-----+---------+----------------+

有了外键到约束我们就不能随便插入数据了

select * from class;
+----+---------+
| id | name    |
+----+---------+
|  1 | java113 |
|  2 | java112 |
|  3 | java111 |
+----+---------+

我们班级表新增三个班级

select * from student;
+----+--------+----------+
| id | name   | class_id |
+----+--------+----------+
|  1 | 张三   |        1 |
|  2 | 李四   |        2 |
|  3 | 王五   |        3 |
+----+--------+----------+

再给每个班级新增一个学生

我们再试着给学生表中添加一个4班的学生,但是班级表是不存在编号为4的班级的

insert into student (name,class_id) values ('张三','4');
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`java113`.`student`, CONSTRAINT `student_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `class` (`id`))

因为外键约束,我们无法添加class表中没有的班级的学生编号

 二,表的设计

1,三大范式

1,第一范式:要求表中的每一列不可再分,其实就是数据库提供的数据类型能描述这个字段,如果不满足第一范式,那么数据库就不是关系型数据库

2,第二范式:在满足第一范式的基础上,消除部分函数依赖,只能出现在有复合主键的表上,如果有一列字段只依赖于复合主键的其中一列,就需要移除这个列,单独建表。如果不满足第二范式就会出现删除异常,新增异常,更新异常,数据冗余

3,第三范式:在第二范式的基础上,消除传递依赖,这个是如果除主键外的某一列,对其他某一列存在依赖时,我们就要移除这个列

2,关系模型

1,一对一模型:

各自建立各自的表,在一个表中添加一个字段完成对另一个表的引用。

2,一对多模型:

各自建立各自的表,在一表中设置主键,在多表中设置外键于一表主键关联。

3,多对多模型:

各自建立各自的表,另外添加一个新的关系表。

我们举个多对多模型的例子,

一个class表

> create table if not exists class (-> id bigint primary key auto_increment,-> name varchar(20)-> );

一个student表

create table if not exists student(-> id bigint primary key auto_increment,-> name varchar(20)-> );

一个sorce表来关联这两个表

 create table if not exists sorce(-> id bigint primary key auto_increment,-> student_id bigint,-> class_id bigint,-> stu_score decimal(3,1),-> foreign key (student_id) references student(id),-> foreign key (class_id) references class(id)-> );

这就形成了多对多关系

三,查询(进阶)

1,插入时查询

语法:

insert into 新表名 (列名) select 要复制的列名(这里不要使用括号)from 旧表名

这个我们可以用到表复制的时候

select * from student;
+----+---------+
| id | name    |
+----+---------+
|  1 | 张三    |
|  2 | 0李四   |
|  3 | 王五    |
+----+---------+

我们现在创建一个新表,直接将旧表的数据复制到新表中

 create table if not exists new_student(-> id bigint primary key auto_increment,-> name varchar(20)-> );
 insert into new_student (id,name) select id,name from student;

2,聚合函数

select * from course;
+-----------+--------------------+
| course_id | name               |
+-----------+--------------------+
|         1 | Java               |
|         2 | 中国传统文化       |
|         3 | 计算机原理         |
|         4 | 语文               |
|         5 | 高阶数学           |
|         6 | 英文               |
+-----------+--------------------+
 select * from class;
+----------+-------------------------+
| class_id | name                    |
+----------+-------------------------+
|        1 | 计算机系2019级1班       |
|        2 | 中文系2019级3班         |
|        3 | 自动化2019级5班         |
+----------+-------------------------+
 select * from student;
+------------+-------+-----------------+------------------+----------+
| student_id | sn    | name            | mail             | class_id |
+------------+-------+-----------------+------------------+----------+
|          1 | 09982 | 黑旋风李逵      | xuanfeng@qq.com  |        1 |
|          2 | 00835 | 菩提老祖        | NULL             |        1 |
|          3 | 00391 | 白素贞          | NULL             |        1 |
|          4 | 00031 | 许仙            | xuxian@qq.com    |        1 |
|          5 | 00054 | 不想毕业        | NULL             |        1 |
|          6 | 51234 | 好好说话        | say@qq.com       |        2 |
|          7 | 83223 | tellme          | NULL             |        2 |
|          8 | 09527 | 老外学中文      | foreigner@qq.com |        2 |
+------------+-------+-----------------+------------------+----------+
 select * from exam;
+------+-----------+---------+------+---------+
| id   | name      | chinese | math | english |
+------+-----------+---------+------+---------+
|    1 | 唐三藏    |    67.0 | 98.0 |    56.0 |
|    2 | 孙悟空    |    87.5 | 78.0 |    77.0 |
|    3 | 猪悟能    |    88.0 | 98.0 |    90.0 |
|    4 | 曹孟德    |    82.0 | 84.0 |    67.0 |
|    5 | 刘玄德    |    55.5 | 85.0 |    45.0 |
|    6 | 孙权      |    70.0 | 73.0 |    78.5 |
|    7 | 宋公明    |    75.0 | 65.0 |    30.0 |
+------+-----------+---------+------+---------+

mysql> select * from emp;
+----+-----------+--------------+------------+
| id | name      | role         | salary     |
+----+-----------+--------------+------------+
|  1 | 马云      | 老板         | 1500000.00 |
|  2 | 马化腾    | 老板         | 1800000.00 |
|  3 | 鑫哥      | 讲师         |   10000.00 |
|  4 | 博哥      | 讲师         |   12000.00 |
|  5 | 平姐      | 学管         |    9000.00 |
|  6 | 莹姐      | 学管         |    8000.00 |
|  7 | 猪悟能    | 游戏角色     |     700.50 |
|  8 | 沙和尚    | 游戏角色     |     333.30 |
+----+-----------+--------------+------------+
select * from score;
+----------+------------+-----------+-------+
| score_id | student_id | course_id | score |
+----------+------------+-----------+-------+
|        1 |          1 |         1 | 70.50 |
|        2 |          1 |         3 | 98.50 |
|        3 |          1 |         5 | 33.00 |
|        4 |          1 |         6 | 98.00 |
|        5 |          2 |         1 | 60.00 |
|        6 |          2 |         5 | 59.50 |
|        7 |          3 |         1 | 33.00 |
|        8 |          3 |         3 | 68.00 |
|        9 |          3 |         5 | 99.00 |
|       10 |          4 |         1 | 67.00 |
|       11 |          4 |         3 | 23.00 |
|       12 |          4 |         5 | 56.00 |
|       13 |          4 |         6 | 72.00 |
|       14 |          5 |         1 | 81.00 |
|       15 |          5 |         5 | 37.00 |
|       16 |          6 |         2 | 56.00 |
|       17 |          6 |         4 | 43.00 |
|       18 |          6 |         6 | 79.00 |
|       19 |          7 |         2 | 80.00 |
|       20 |          7 |         6 | 92.00 |
+----------+------------+-----------+-------+

我们拿这几个表来举例子。 

1,count(列):统计一列的个数

————1,统计班级有多少同学

select count(*) from exam;
+----------+
| count(*) |
+----------+
|        7 |
+----------+

————2,统计班级收集的 qq_mail 有多少个,qq_mail 为 NULL 的数据不会计入结果

 select count(mail) from student-> ;
+-------------+
| count(mail) |
+-------------+
|           4 |
+-------------+

2,Sum(列):求和

————1,统计数学成绩总分

select sum(math) from exam;
+-----------+
| sum(math) |
+-----------+
|     581.0 |
+-----------+

————2,不及格 < 60 的总分,没有结果,返回 NULL

select sum(math) from exam where math<60;
+-----------+
| sum(math) |
+-----------+
|      NULL |
+-----------+

3,AVG(列):求平均值

————1,统计平均总分

 select avg(english) from exam;
+--------------+
| avg(english) |
+--------------+
|     63.35714 |
+--------------+

4,MAX(列):求最大值

————1,返回英语最高分

 select max(english) from exam;
+--------------+
| max(english) |
+--------------+
|         90.0 |
+--------------+

5,MIN(列):求最小值

————1,返回 > 70 分以上的数学最低分

select min(math) from exam where math>70;
+-----------+
| min(math) |
+-----------+
|      73.0 |
+-----------+

3,group by

group by 语句就是分组查询

语法:

select 字段1 字段2 from 表名 group by 字段1 ,字段2;

group by是可以使用别名的,但是where不行 

select * from emp;
+----+-----------+--------------+------------+
| id | name      | role         | salary     |
+----+-----------+--------------+------------+
|  1 | 马云      | 老板         | 1500000.00 |
|  2 | 马化腾    | 老板         | 1800000.00 |
|  3 | 鑫哥      | 讲师         |   10000.00 |
|  4 | 博哥      | 讲师         |   12000.00 |
|  5 | 平姐      | 学管         |    9000.00 |
|  6 | 莹姐      | 学管         |    8000.00 |
|  7 | 猪悟能    | 游戏角色     |     700.50 |
|  8 | 沙和尚    | 游戏角色     |     333.30 |
+----+-----------+--------------+------------+

我们使用这个表

————1,查询每个角色的最高工资、最低工资和平均工资

select role,max(salary),min(salary),avg(salary) from emp group by role;
+--------------+-------------+-------------+----------------+
| role         | max(salary) | min(salary) | avg(salary)    |
+--------------+-------------+-------------+----------------+
| 老板         |  1800000.00 |  1500000.00 | 1650000.000000 |
| 讲师         |    12000.00 |    10000.00 |   11000.000000 |
| 学管         |     9000.00 |     8000.00 |    8500.000000 |
| 游戏角色     |      700.50 |      333.30 |     516.900000 |
+--------------+-------------+-------------+----------------+

4,Having

我们使用group by 的时候不能使用where字句,所以出现了Having,我们使用Having来过滤条件;

————1,显示平均工资低于1500的角色和它的平均工资

select role,avg(salary) from emp group by role having avg(salary)<1500;
+--------------+-------------+
| role         | avg(salary) |
+--------------+-------------+
| 游戏角色     |  516.900000 |
+--------------+-------------+

5,联合查询

对多张表进行取笛卡尔积,在选取正确的联合表;

1,内连接

语法:

select 表1 别名,表2 别名 where 连接条件 and 其他条件

select 表1 别名 [inner] join 表2 别名2 on 连接条件 and 其他条件

————1,查询“许仙”同学的 成绩

 select stu.name,sc.score from student stu,score sc where sc.student_id = stu.student_id and stu.name = '许仙';
+--------+-------+
| name   | score |
+--------+-------+
| 许仙   | 67.00 |
| 许仙   | 23.00 |
| 许仙   | 56.00 |
| 许仙   | 72.00 |
+--------+-------+
 select stu.name,sc.score from student stu inner join score sc on stu.student_id = sc.student_id where stu.name = '许仙';
+--------+-------+
| name   | score |
+--------+-------+
| 许仙   | 67.00 |
| 许仙   | 23.00 |
| 许仙   | 56.00 |
| 许仙   | 72.00 |
+--------+-------+

————2,查询所有同学的总成绩,及同学的个人信息:

select stu.name,stu.sn,stu.mail,sum(sc.score) from student stu,score sc where stu.student_id = sc.student_id group by stu.student_id;
+-----------------+-------+-----------------+---------------+
| name            | sn    | mail            | sum(sc.score) |
+-----------------+-------+-----------------+---------------+
| 黑旋风李逵      | 09982 | xuanfeng@qq.com |        300.00 |
| 菩提老祖        | 00835 | NULL            |        119.50 |
| 白素贞          | 00391 | NULL            |        200.00 |
| 许仙            | 00031 | xuxian@qq.com   |        218.00 |
| 不想毕业        | 00054 | NULL            |        118.00 |
| 好好说话        | 51234 | say@qq.com      |        178.00 |
| tellme          | 83223 | NULL            |        172.00 |
+-----------------+-------+-----------------+---------------+
select stu.sn,stu.name,stu.mail,sum(sc.score) from student stu inner join score sc on stu.student_id = sc.student_id group by stu.student_id;
+-------+-----------------+-----------------+---------------+
| sn    | name            | mail            | sum(sc.score) |
+-------+-----------------+-----------------+---------------+
| 09982 | 黑旋风李逵      | xuanfeng@qq.com |        300.00 |
| 00835 | 菩提老祖        | NULL            |        119.50 |
| 00391 | 白素贞          | NULL            |        200.00 |
| 00031 | 许仙            | xuxian@qq.com   |        218.00 |
| 00054 | 不想毕业        | NULL            |        118.00 |
| 51234 | 好好说话        | say@qq.com      |        178.00 |
| 83223 | tellme          | NULL            |        172.00 |
+-------+-----------------+-----------------+---------------+

————3,查询所有同学的成绩,及同学的个人信息

 select stu.sn,stu.name,stu.mail,sc.score from student stu,score sc where stu.student_id = sc.student_id;
+-------+-----------------+-----------------+-------+
| sn    | name            | mail            | score |
+-------+-----------------+-----------------+-------+
| 09982 | 黑旋风李逵      | xuanfeng@qq.com | 70.50 |
| 09982 | 黑旋风李逵      | xuanfeng@qq.com | 98.50 |
| 09982 | 黑旋风李逵      | xuanfeng@qq.com | 33.00 |
| 09982 | 黑旋风李逵      | xuanfeng@qq.com | 98.00 |
| 00835 | 菩提老祖        | NULL            | 60.00 |
| 00835 | 菩提老祖        | NULL            | 59.50 |
| 00391 | 白素贞          | NULL            | 33.00 |
| 00391 | 白素贞          | NULL            | 68.00 |
| 00391 | 白素贞          | NULL            | 99.00 |
| 00031 | 许仙            | xuxian@qq.com   | 67.00 |
| 00031 | 许仙            | xuxian@qq.com   | 23.00 |
| 00031 | 许仙            | xuxian@qq.com   | 56.00 |
| 00031 | 许仙            | xuxian@qq.com   | 72.00 |
| 00054 | 不想毕业        | NULL            | 81.00 |
| 00054 | 不想毕业        | NULL            | 37.00 |
| 51234 | 好好说话        | say@qq.com      | 56.00 |
| 51234 | 好好说话        | say@qq.com      | 43.00 |
| 51234 | 好好说话        | say@qq.com      | 79.00 |
| 83223 | tellme          | NULL            | 80.00 |
| 83223 | tellme          | NULL            | 92.00 |
+-------+-----------------+-----------------+-------+
 select stu.sn,stu.name,stu.mail,sc.score from student stu inner join score sc on stu.student_id = sc.student_id;
+-------+-----------------+-----------------+-------+
| sn    | name            | mail            | score |
+-------+-----------------+-----------------+-------+
| 09982 | 黑旋风李逵      | xuanfeng@qq.com | 70.50 |
| 09982 | 黑旋风李逵      | xuanfeng@qq.com | 98.50 |
| 09982 | 黑旋风李逵      | xuanfeng@qq.com | 33.00 |
| 09982 | 黑旋风李逵      | xuanfeng@qq.com | 98.00 |
| 00835 | 菩提老祖        | NULL            | 60.00 |
| 00835 | 菩提老祖        | NULL            | 59.50 |
| 00391 | 白素贞          | NULL            | 33.00 |
| 00391 | 白素贞          | NULL            | 68.00 |
| 00391 | 白素贞          | NULL            | 99.00 |
| 00031 | 许仙            | xuxian@qq.com   | 67.00 |
| 00031 | 许仙            | xuxian@qq.com   | 23.00 |
| 00031 | 许仙            | xuxian@qq.com   | 56.00 |
| 00031 | 许仙            | xuxian@qq.com   | 72.00 |
| 00054 | 不想毕业        | NULL            | 81.00 |
| 00054 | 不想毕业        | NULL            | 37.00 |
| 51234 | 好好说话        | say@qq.com      | 56.00 |
| 51234 | 好好说话        | say@qq.com      | 43.00 |
| 51234 | 好好说话        | say@qq.com      | 79.00 |
| 83223 | tellme          | NULL            | 80.00 |
| 83223 | tellme          | NULL            | 92.00 |
+-------+-----------------+-----------------+-------+

2,外连接

外连接分为左外连接和右外连接。如果联合查询,左侧的表完全显示我们就说是左外连接;右侧的表完 全显示我们就说是右外连接。

————左外连接,表1完全显示

语法:

select 字段名 from 表名1 left join 表名2 on 连接条件

————右外连接,表2完全显示

语法

select 字段名 from 表名1 right join 表名2 on 连接条件

 ————1,显示,“老外学中文”同学的考试成绩 ,没有考试成绩也要显示

 select stu.sn,stu.name,stu.mail,sc.score from student stu left join score sc on stu.student_id = sc.student_id;
+-------+-----------------+------------------+-------+
| sn    | name            | mail             | score |
+-------+-----------------+------------------+-------+
| 09982 | 黑旋风李逵      | xuanfeng@qq.com  | 70.50 |
| 09982 | 黑旋风李逵      | xuanfeng@qq.com  | 98.50 |
| 09982 | 黑旋风李逵      | xuanfeng@qq.com  | 33.00 |
| 09982 | 黑旋风李逵      | xuanfeng@qq.com  | 98.00 |
| 00835 | 菩提老祖        | NULL             | 60.00 |
| 00835 | 菩提老祖        | NULL             | 59.50 |
| 00391 | 白素贞          | NULL             | 33.00 |
| 00391 | 白素贞          | NULL             | 68.00 |
| 00391 | 白素贞          | NULL             | 99.00 |
| 00031 | 许仙            | xuxian@qq.com    | 67.00 |
| 00031 | 许仙            | xuxian@qq.com    | 23.00 |
| 00031 | 许仙            | xuxian@qq.com    | 56.00 |
| 00031 | 许仙            | xuxian@qq.com    | 72.00 |
| 00054 | 不想毕业        | NULL             | 81.00 |
| 00054 | 不想毕业        | NULL             | 37.00 |
| 51234 | 好好说话        | say@qq.com       | 56.00 |
| 51234 | 好好说话        | say@qq.com       | 43.00 |
| 51234 | 好好说话        | say@qq.com       | 79.00 |
| 83223 | tellme          | NULL             | 80.00 |
| 83223 | tellme          | NULL             | 92.00 |
| 09527 | 老外学中文      | foreigner@qq.com |  NULL |
+-------+-----------------+------------------+-------+

————2,学生表、成绩表、课程表3张表关联查询

 select stu.sn,stu.name,stu.mail,sc.score,co.name from student stu left join score sc on stu.student_id = sc.student_id left join course co on sc.course_id = co.course_id;
+-------+-----------------+------------------+-------+--------------------+
| sn    | name            | mail             | score | name               |
+-------+-----------------+------------------+-------+--------------------+
| 09982 | 黑旋风李逵      | xuanfeng@qq.com  | 70.50 | Java               |
| 09982 | 黑旋风李逵      | xuanfeng@qq.com  | 98.50 | 计算机原理         |
| 09982 | 黑旋风李逵      | xuanfeng@qq.com  | 33.00 | 高阶数学           |
| 09982 | 黑旋风李逵      | xuanfeng@qq.com  | 98.00 | 英文               |
| 00835 | 菩提老祖        | NULL             | 60.00 | Java               |
| 00835 | 菩提老祖        | NULL             | 59.50 | 高阶数学           |
| 00391 | 白素贞          | NULL             | 33.00 | Java               |
| 00391 | 白素贞          | NULL             | 68.00 | 计算机原理         |
| 00391 | 白素贞          | NULL             | 99.00 | 高阶数学           |
| 00031 | 许仙            | xuxian@qq.com    | 67.00 | Java               |
| 00031 | 许仙            | xuxian@qq.com    | 23.00 | 计算机原理         |
| 00031 | 许仙            | xuxian@qq.com    | 56.00 | 高阶数学           |
| 00031 | 许仙            | xuxian@qq.com    | 72.00 | 英文               |
| 00054 | 不想毕业        | NULL             | 81.00 | Java               |
| 00054 | 不想毕业        | NULL             | 37.00 | 高阶数学           |
| 51234 | 好好说话        | say@qq.com       | 56.00 | 中国传统文化       |
| 51234 | 好好说话        | say@qq.com       | 43.00 | 语文               |
| 51234 | 好好说话        | say@qq.com       | 79.00 | 英文               |
| 83223 | tellme          | NULL             | 80.00 | 中国传统文化       |
| 83223 | tellme          | NULL             | 92.00 | 英文               |
| 09527 | 老外学中文      | foreigner@qq.com |  NULL | NULL               |
+-------+-----------------+------------------+-------+--------------------+

3,自连接

自连接是在同一张表中进行查询,表要起两个不同的别名

————1,显示所有“计算机原理”成绩比“Java”成绩高的成绩信息

这个我们来分析一下,计算机原理和java是课程表中,成绩是score表的内容,要取这两个表的笛卡尔积,java的course_id = 1,计算机原理的course_id = 3,这次我们不是比列而是行与行,

 select * from score sc1,score sc2 where sc1.course_id = 1 and sc2.course_id = 3 and sc2.score > sc1.score and sc1.student_id = sc2.student_id;
+----------+------------+-----------+-------+----------+------------+-----------+-------+
| score_id | student_id | course_id | score | score_id | student_id | course_id | score |
+----------+------------+-----------+-------+----------+------------+-----------+-------+
|        1 |          1 |         1 | 70.50 |        2 |          1 |         3 | 98.50 |
|        7 |          3 |         1 | 33.00 |        8 |          3 |         3 | 68.00 |
+----------+------------+-----------+-------+----------+------------+-----------+-------+

 4,子查询

子查询就是嵌套查询,

————1,查询与“不想毕业” 同学的同班同学:

select * from student where class_id = (select class_id from student where name = '不想毕业');
+------------+-------+-----------------+-----------------+----------+
| student_id | sn    | name            | mail            | class_id |
+------------+-------+-----------------+-----------------+----------+
|          1 | 09982 | 黑旋风李逵      | xuanfeng@qq.com |        1 |
|          2 | 00835 | 菩提老祖        | NULL            |        1 |
|          3 | 00391 | 白素贞          | NULL            |        1 |
|          4 | 00031 | 许仙            | xuxian@qq.com   |        1 |
|          5 | 00054 | 不想毕业        | NULL            |        1 |
+------------+-------+-----------------+-----------------+----------+

————2,查询“语文”或“英文”课程的成绩信息

 select * from score where course_id in (select course_id from course where name = '语文' or name = '英文');
+----------+------------+-----------+-------+
| score_id | student_id | course_id | score |
+----------+------------+-----------+-------+
|       17 |          6 |         4 | 43.00 |
|        4 |          1 |         6 | 98.00 |
|       13 |          4 |         6 | 72.00 |
|       18 |          6 |         6 | 79.00 |
|       20 |          7 |         6 | 92.00 |
+----------+------------+-----------+-------+

exists 关键字

这个是如果子查询的语句有返回语句,整体查询语句就执行,反之不执行,

select * from score where not exists (select course_id from course where name = '语文' or name = '英文');
Empty set (0.00 sec)
 select * from score where exists (select course_id from course where name = '语文' or name = '英文');
+----------+------------+-----------+-------+
| score_id | student_id | course_id | score |
+----------+------------+-----------+-------+
|        1 |          1 |         1 | 70.50 |
|        2 |          1 |         3 | 98.50 |
|        3 |          1 |         5 | 33.00 |
|        4 |          1 |         6 | 98.00 |
|        5 |          2 |         1 | 60.00 |
|        6 |          2 |         5 | 59.50 |
|        7 |          3 |         1 | 33.00 |
|        8 |          3 |         3 | 68.00 |
|        9 |          3 |         5 | 99.00 |
|       10 |          4 |         1 | 67.00 |
|       11 |          4 |         3 | 23.00 |
|       12 |          4 |         5 | 56.00 |
|       13 |          4 |         6 | 72.00 |
|       14 |          5 |         1 | 81.00 |
|       15 |          5 |         5 | 37.00 |
|       16 |          6 |         2 | 56.00 |
|       17 |          6 |         4 | 43.00 |
|       18 |          6 |         6 | 79.00 |
|       19 |          7 |         2 | 80.00 |
|       20 |          7 |         6 | 92.00 |
+----------+------------+-----------+-------+

————1,查询所有比“中文系2019级3班”平均分高的成绩信息:

select * from score sc where sc.score > (select avg(score) from score sc,class cl,student stu where cl.class_id = stu.class_id and sc.student_id = stu.student_id and cl.name = '中文系2019级3班');
+----------+------------+-----------+-------+
| score_id | student_id | course_id | score |
+----------+------------+-----------+-------+
|        1 |          1 |         1 | 70.50 |
|        2 |          1 |         3 | 98.50 |
|        4 |          1 |         6 | 98.00 |
|        9 |          3 |         5 | 99.00 |
|       13 |          4 |         6 | 72.00 |
|       14 |          5 |         1 | 81.00 |
|       18 |          6 |         6 | 79.00 |
|       19 |          7 |         2 | 80.00 |
|       20 |          7 |         6 | 92.00 |
+----------+------------+-----------+-------+

5,合并查询

去重union

不去重 union all

比如查询id<3,或者名字为英文的课程

select * from course where course_id<3 union select * from course where name = '英文';
+-----------+--------------------+
| course_id | name               |
+-----------+--------------------+
|         1 | Java               |
|         2 | 中国传统文化       |
|         6 | 英文               |
+-----------+--------------------+

union all可以去重,这里就不演示了。

相关文章:

MySQL知识大总结(进阶)

一&#xff0c;数据库的约束 1&#xff0c;约束类型 1not null非空约束&#xff0c;标记这个字段不可以为空2unique唯一约束&#xff0c;标记这个字段的值是该列唯一的值&#xff0c;在这一列的其他行&#xff0c;不可以与该字段相等3default 默认约束&#xff0c;在该字段没…...

【C语言】库函数常见的陷阱与缺陷(2):字符串转化函数

目录 一、atoi 函数 1.1. 功能与用法 1.2. 陷阱与缺陷 1.2.1. 输入验证不足 1.2.2. 溢出问题 1.3 安全替代 1.4. 代码示例 二、atof 函数 2.1. 功能与用法 2.2. 陷阱与缺陷 2.3. 安全使用建议 2.4. 代码示例 三、strtol 函数 3.1. 功能与用法 3.2. 陷阱与缺陷 …...

渗透测试基础

渗透测试基础是指对计算机系统、网络或应用程序进行模拟攻击&#xff0c;以发现其安全漏洞和潜在威胁的一种安全评估技术。通过模拟真实的攻击场景&#xff0c;渗透测试帮助组织了解其系统的安全弱点、验证防护措施的有效性&#xff0c;并提供改进建议。 渗透测试的核心概念 1…...

传奇996_53——后端ui窗口局部刷新

描述&#xff1a;一个大窗口&#xff0c;点击某个键&#xff0c;弹出小窗口。 小窗口中将msg存进变量中 大窗口中判断一个参数是否为null&#xff0c;如果不为null&#xff0c;说明界面不是第一次打开&#xff0c;而是被刷新了。就加上小窗口的那个变量 有时小窗口中还有其他…...

C++ constexpr vs const

笼统的讲 constexpr 主要用于编译时期&#xff0c;const用于运行时&#xff0c;但实际上两者都可以同时用于编译时期和运行时。 const const可以修饰全局变量&#xff0c;局部变量&#xff0c;函数参数&#xff0c;指针&#xff0c;引用&#xff0c;也可以修饰类成员函数&…...

【达梦数据库】存储过程调用实践案例-select

目录 前言创建表插入数据查询表中数据创建存储过程打开dbms_output包输出开关调用存储过程 前言 如果要在存储过程中执行一个SELECT语句并处理其结果&#xff0c;你不能直接使用EXECUTE IMMEDIATE&#xff0c;因为EXECUTE IMMEDIATE主要用于执行那些不返回行的语句&#xff08;…...

041_Compare_Matrix_Squre_Sum_in_MATLAB中矩阵平方和的比较

矩阵平方和的计算 矩阵平方和的定义 矩阵平方和的定义是对矩阵中的每一个元素进行平方&#xff0c;然后求和。 对于一个矩阵 A A A&#xff0c;其平方和定义为&#xff1a; sum ∑ i 1 m ∑ j 1 n A ( i , j ) 2 \text{sum} \sum_{i1}^{m}\sum_{j1}^{n} A(i,j)^2 sumi1∑…...

TimeXplusplus——提高时间序列数据的可解释性,避免琐解和分布偏移问题的深度学习可解释性的框架

摘要 论文地址&#xff1a;https://arxiv.org/abs/2405.09308 源码地址&#xff1a;https://github.com/zichuan-liu/timexplusplus 信号传输技术的优化对于推动光通信的发展至关重要。本文将详细探讨线路编码技术的目标及其实现方式。线路编码旨在提高带宽和功率效率&#xf…...

批处理读取文本第n行并赋值给变量?--遍历所有行并赋值给变量数组

::TraceLines.bat goto :test1http://www.bathome.net/thread-27229-1-1.html#批处理如何获取txt文本中某行某列的内容/指定行指定列的内容 http://www.bathome.net/thread-47304-1-1.html#如何用批处理读取文本第二行并赋值给变量&#xff1f; https://github.com/npocmaka/ba…...

嵌入式入门Day26

IO Day2 IO相关函数标准文件流指针缓冲区刷新时机作业 IO相关函数 time #include <time.h>time_t time(time_t *tloc);功能&#xff1a;返回1970年到现在的秒数参数&#xff1a;计算出来的秒数&#xff1b;返回值&#xff1b;成功返回1970年到现在的秒数&#xff0c;失…...

【Vue3项目实战系列一】—— 从零开始一个vue3项目 vue3+javascript+vite 非常详细 手把手教学

&#x1f609; 你好呀&#xff0c;我是爱编程的Sherry&#xff0c;很高兴在这里遇见你&#xff01;我是一名拥有十多年开发经验的前端工程师。这一路走来&#xff0c;面对困难时也曾感到迷茫&#xff0c;凭借不懈的努力和坚持&#xff0c;重新找到了前进的方向。我的人生格言是…...

Python_Flask04(牛马问答平台01)

项目名称&#xff1a;牛马问答平台 项目简介&#xff1a;1.主要用来给社会上的牛马们探讨老板的恶心之处。 2. 用来学习交流。 技术手段&#xff1a;Python3.x Flask render_template pymysql flask_sqlalchemy........ 注意该博客为迭代项目&#xff0c;于最后一期展示完整…...

Java转C之并发和多线程

提纲&#xff1a; 概念介绍与对比概述 简述Java与C在并发和多线程方面的核心区别解释C11标准、POSIX、C11 <threads.h>、Pthread等名词 Java多线程与并发回顾 线程、Runnable、ExecutorService概念说明同步关键字与工具类含义 C并发基础 没有Java式的内置线程类&#xf…...

针对一个系统的权限管理这样的业务场景,使用各设计模式解说

通义灵码 下面将介绍如何在Java中使用不同的设计模式来实现权限管理系统。每个设计模式都有其特定的应用场景和实现方式&#xff0c;我们将逐一讲解。 1. 单例模式 (Singleton Pattern) 应用场景&#xff1a;确保权限管理服务在整个系统中只有一个实例&#xff0c;避免重复创…...

Android AppCompatImageView View.Gone状态切换到View.VISIBLE重新layout,Kotlin

Android AppCompatImageView View.Gone状态切换到View.VISIBLE重新layout&#xff0c;Kotlin import android.content.Context import android.util.AttributeSet import android.util.Log import androidx.appcompat.widget.AppCompatImageViewclass MyImageView : AppCompatI…...

在云上轻松部署达梦数据库

达梦数据库&#xff08;DM Database&#xff09;是由达梦数据库有限公司开发的一款关系型数据库管理系统&#xff08;RDBMS&#xff09;。作为国内领先的数据库产品&#xff0c;达梦数据库在政府、金融、能源、电信、交通、医疗、教育等多个行业得到广泛应用&#xff0c;尤其在…...

什么是厄尔米特(Hermitian)矩阵?

厄米矩阵&#xff08;Hermitian Matrix&#xff09;定义 在数学和物理中&#xff0c;厄米矩阵是满足以下条件的复方阵&#xff1a; A A † \mathbf{A}\mathbf{A}^\dagger AA† 其中&#xff0c; A † \mathbf{A}^\dagger A†表示矩阵 A \mathbf{A} A的共轭转置&#xff0c;即…...

React - useActionState、useFormStatus与表单处理

参考文档&#xff1a;react18.3.1官方文档 一些概念&#xff1a; React 的 Canary 和 Experimental 频道是 React 团队用于发布和测试新功能的渠道。 useActionState useActionState 是一个可以根据某个表单动作的结果更新 state 的 Hook。 const [state, formAction, isPe…...

v3账号密码登录随机图片验证码

安装插件 pnpm i identify --save图形验证码组件 <template><div class"s-canvas"><!-- 图形验证码的宽和高都来自于父组件的传值&#xff0c;若父组件没有传值&#xff0c;那么就按当前子组件的默认值进行渲染 --><canvas id"s-canvas&…...

不只是请求和响应:使用Fiddler解读Cookie与状态码全指南(下)

欢迎浏览高耳机的博客 希望我们彼此都有更好的收获 感谢三连支持! 不只是请求和响应&#xff1a;使用Fiddler抓包HTTP协议全指南(上)_fiddler 获取响应脚本-CSDN博客https://blog.csdn.net/Chunfeng6yugan/article/details/144005872?spm1001.2014.3001.5501 不只是请求和响…...

mongodb源码分析session执行handleRequest命令find过程

mongo/transport/service_state_machine.cpp已经分析startSession创建ASIOSession过程&#xff0c;并且验证connection是否超过限制ASIOSession和connection是循环接受客户端命令&#xff0c;把数据流转换成Message&#xff0c;状态转变流程是&#xff1a;State::Created 》 St…...

srs linux

下载编译运行 git clone https:///ossrs/srs.git ./configure --h265on make 编译完成后即可启动SRS # 启动 ./objs/srs -c conf/srs.conf # 查看日志 tail -n 30 -f ./objs/srs.log 开放端口 默认RTMP接收推流端口是1935&#xff0c;SRS管理页面端口是8080&#xff0c;可…...

【论文笔记】若干矿井粉尘检测算法概述

总的来说&#xff0c;传统机器学习、传统机器学习与深度学习的结合、LSTM等算法所需要的数据集来源于矿井传感器测量的粉尘浓度&#xff0c;通过建立回归模型来预测未来矿井的粉尘浓度。传统机器学习算法性能易受数据中极端值的影响。YOLO等计算机视觉算法所需要的数据集来源于…...

【RockeMQ】第2节|RocketMQ快速实战以及核⼼概念详解(二)

升级Dledger高可用集群 一、主从架构的不足与Dledger的定位 主从架构缺陷 数据备份依赖Slave节点&#xff0c;但无自动故障转移能力&#xff0c;Master宕机后需人工切换&#xff0c;期间消息可能无法读取。Slave仅存储数据&#xff0c;无法主动升级为Master响应请求&#xff…...

docker 部署发现spring.profiles.active 问题

报错&#xff1a; org.springframework.boot.context.config.InvalidConfigDataPropertyException: Property spring.profiles.active imported from location class path resource [application-test.yml] is invalid in a profile specific resource [origin: class path re…...

2025季度云服务器排行榜

在全球云服务器市场&#xff0c;各厂商的排名和地位并非一成不变&#xff0c;而是由其独特的优势、战略布局和市场适应性共同决定的。以下是根据2025年市场趋势&#xff0c;对主要云服务器厂商在排行榜中占据重要位置的原因和优势进行深度分析&#xff1a; 一、全球“三巨头”…...

[免费]微信小程序问卷调查系统(SpringBoot后端+Vue管理端)【论文+源码+SQL脚本】

大家好&#xff0c;我是java1234_小锋老师&#xff0c;看到一个不错的微信小程序问卷调查系统(SpringBoot后端Vue管理端)【论文源码SQL脚本】&#xff0c;分享下哈。 项目视频演示 【免费】微信小程序问卷调查系统(SpringBoot后端Vue管理端) Java毕业设计_哔哩哔哩_bilibili 项…...

scikit-learn机器学习

# 同时添加如下代码, 这样每次环境(kernel)启动的时候只要运行下方代码即可: # Also add the following code, # so that every time the environment (kernel) starts, # just run the following code: import sys sys.path.append(/home/aistudio/external-libraries)机…...

C++ 设计模式 《小明的奶茶加料风波》

&#x1f468;‍&#x1f393; 模式名称&#xff1a;装饰器模式&#xff08;Decorator Pattern&#xff09; &#x1f466; 小明最近上线了校园奶茶配送功能&#xff0c;业务火爆&#xff0c;大家都在加料&#xff1a; 有的同学要加波霸 &#x1f7e4;&#xff0c;有的要加椰果…...

MySQL:分区的基本使用

目录 一、什么是分区二、有什么作用三、分类四、创建分区五、删除分区 一、什么是分区 MySQL 分区&#xff08;Partitioning&#xff09;是一种将单张表的数据逻辑上拆分成多个物理部分的技术。这些物理部分&#xff08;分区&#xff09;可以独立存储、管理和优化&#xff0c;…...