四、MySQL表的增删改查

四、MySQL表的增删改查

1. 数据库约束

  • NOT NULL - 指示某列不能存储 NULL 值。
  • UNIQUE - 保证某列的每行必须有唯一的值。
  • DEFAULT - 规定没有给列赋值时的默认值。
  • PRIMARY KEY - NOT NULL 和 UNIQUE 的结合。确保某列(或两个列多个列的结合)有唯一标识,有助于更容易更快速地找到表中的一个特定的记录。
  • FOREIGN KEY - 保证一个表中的数据匹配另一个表中的值的参照完整性。
  • CHECK - 保证列中的值符合指定的条件。对于MySQL数据库,对CHECK子句进行分析,但是忽略CHECK子句。

2. 表的设计

  • 一对一
    在这里插入图片描述
  • 一对多
    在这里插入图片描述
  • 多对多
    在这里插入图片描述

3. 查询

3.1 聚合查询

3.1.1 聚合函数

函数 说明
COUNT([DISTINCT] expr) 返回查询到的数据的 数量
SUM([DISTINCT] expr) 返回查询到的数据的 总和,不是数字没有意义
AVG([DISTINCT] expr) 返回查询到的数据的 平均值,不是数字没有意义
MAX([DISTINCT] expr) 返回查询到的数据的 最大值,不是数字没有意义
MIN([DISTINCT] expr) 返回查询到的数据的 最小值,不是数字没有意义
-- COUNT
-- 统计班级共有多少同学
SELECT COUNT(*) FROM student;

-- SUM
-- 统计数学成绩总分
SELECT SUM(math) FROM exam_result;

-- AVG
-- 统计平均总分
SELECT AVG(chinese + math + english) 平均总分 FROM exam_result;

-- MAX
-- 返回英语最高分
SELECT MAX(english) FROM exam_result;

-- MIN
-- 返回 > 70 分以上的数学最低分
SELECT MIN(math) FROM exam_result WHERE math > 70;

3.1.2 GROUP BY子句

  • 语法:

    select column1, sum(column2), .. from table group by column1;
    
  • 示例:

    -- 查询每个角色的最高工资、最低工资和平均工资
    select role,max(salary),min(salary),avg(salary) from emp group by role;
    

3.1.3 HAVING

  • GROUP BY 子句进行分组以后,需要对分组结果再进行条件过滤时,不能使用 WHERE 语句,而需要用HAVING

  • -- 显示平均工资低于1500的角色和它的平均工资
    select role,max(salary),min(salary),avg(salary) from emp group by role
    having avg(salary)<1500;
    

3.2 联合查询

多表查询是对多张表的数据取笛卡尔积:
在这里插入图片描述

3.2.1 内连接

  • 语法:

    select 字段 from 表1 别名1 [inner] join 表2 别名2 on 连接条件 and 其他条件;
    select 字段 from 表1 别名1,表2 别名2 where 连接条件 and 其他条件;
    
  • 示例:

    -- 查询A同学的成绩
    select sco.score from student stu, score sco where stu.id=sco.student_id and stu.name='A';
    
    -- 学生表、成绩表、课程表3张表关联查询
    SELECT
    	stu.id,
    	stu.sn,
    	stu.NAME,
    	stu.qq_mail,
    	sco.score,
    	sco.course_id,
    	cou.NAME
    FROM
    	student stu
    	JOIN score sco ON stu.id = sco.student_id
    	JOIN course cou ON sco.course_id = cou.id
    ORDER BY
    	stu.id;
    

3.2.2 外连接

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

  • 语法:

    -- 左外连接,表1完全显示
    select 字段名 from 表名1 left join 表名2 on 连接条件;
    -- 右外连接,表2完全显示
    select 字段名 from 表名1 right join 表名2 on 连接条件;
    
  • 示例:

    -- 没有考试成绩的同学,也会显示出来
    select * from student stu left join score sco on stu.id = sco.student_id;
    -- 对应的右外连接为:
    select * from score sco right join student stu on stu.id = sco.student_id;
    

3.2.3 自连接

自连接是指在同一张表连接自身进行查询。

SELECT
	stu.*,
	s1.score Java,
	s2.score 计算机原理
FROM
	score s1
	JOIN score s2 ON s1.student_id = s2.student_id
	JOIN student stu ON s1.student_id = stu.id
	JOIN course c1 ON s1.course_id = c1.id
	JOIN course c2 ON s2.course_id = c2.id
	AND s1.score < s2.score
	AND c1.NAME = 'Java'
	AND c2.NAME = '计算机原理';

3.2.4 子查询

-- 单行子查询
 select ... from 表1 where 字段1 = (select ... from ...);
 -- [NOT] IN
select ... from 表1 where 字段1 in (select ... from ...);
 -- [NOT] EXISTS
 select ... from 表1 where exists (select ... from ... where 条件);
 -- 临时表:form子句中的子查询
 select ... from 表1, (select ... from ...) as tmp where 条件

3.2.5 合并查询

  • union:该操作符用于取得两个结果集的并集。当使用该操作符时,会自动去掉结果集中的重复行。
  • union all:该操作符用于取得两个结果集的并集。当使用该操作符时,不会去掉结果集中的重复行。
-- UNION:去除重复数据
select ... from ... where 条件
	union
select ... from ... where 条件
-- UNION ALL:不去重
select ... from ... where 条件
	union all
select ... from ... where 条件
-- 使用UNION和UNION ALL时,前后查询的结果集中,字段需要一致
发布了16 篇原创文章 · 获赞 2 · 访问量 570

猜你喜欢

转载自blog.csdn.net/ilovestudy_/article/details/104355922