【MySQL数据库】MySQL表的增删改查(2)

目录

 

1.数据库库约束

1.1 约束类型

1.2 null约束

1.3 unique:唯一标识

1.4 default:默认值约束

1.5 primary key :主键约束

1.6 foreign key:外键约束

1.7 check约束

2.表的设计

2.1 一对一

2.2 一对多

2.3 多对多

3.新增

4.查询

4.1 聚合查询

4.1.1 聚合函数

4.1.2 group by 子句

4.1.3 having

4.2 联合查询

4.2.1 内连接

4.2.2 外连接

4.2.3 子查询

4.2.4 合并查询


1.数据库库约束

1.1 约束类型

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

1.2 null约束

创建表时,可以指定某列不为空:

-- 指定id列不为空:
-- 重新设置学生表结构
drop table if exists student;
create table student(
    id int not null,
    sn int,
    name varchar(20),
    qq_mail varchar(20)
) ;

1.3 unique:唯一标识

-- 指定sn列为唯一的,不重复的:
-- 重新设置学生表结构
drop table if exists student;
create table student(
    id int not null,
    sn int unique,
    name varchar(20),
    qq_mail varchar(20)
) ;

1.4 default:默认值约束

-- 指定插入数据时,name列为空,默认值unknown:
-- 重新设置学生表结构
drop table if exists student;
create table student(
    id int not null,
    sn int unique,
    name varchar(20) default 'unknown',
    qq_mail varchar(20)
) ;

1.5 primary key :主键约束

-- 指定id列为主键
-- 重新设置学生表结构
drop table if exists student;
create table student(
    id int not null primary key,
    sn int unique,
    name varchar(20) default 'unknown',
    qq_mail varchar(20)
) ;

对于整数类型的主键,常配搭自增长auto_increment来使用。插入数据对应字段不给值时,使用最大值+1.

-- 主键是not null 和 unique的结合,可以不用not null
id int primary key auto_incremen,

1.6 foreign key:外键约束

外键用于关联其他表的主键或唯一键,语法:

foreign key (字段名) references 主表(列)

-- 班级表classes,id为主键
id int primary key anto_increment,
-- 学生表student,一个学生对应一个班级,一个班级对应多个学生,使用id为主键,classes_id为外键,关键1班级表id
 id int primary key auto_increment,
 foreign key(classes_id)references classes(id),

1.7 check约束

MySQL使用时不报错,但忽略该约束

drop table if exists test_user;
create table test_user(
    id int,
    name varchar(20),
    sex varchar(20),
    check(sex='男' or sex='女')
);

2.表的设计

三大范式:

2.1 一对一

2.2 一对多

2.3 多对多

3.新增

语法:

insert into 表名 [(列名[,列名…])] select…

例子:

-- 将学生表中的所有数据复制到用户表
insert into test_user(name,email) select name,qq_mail from student;

4.查询

4.1 聚合查询

4.1.1 聚合函数

常见的统计总数、计算平均值等操作,可以使用聚合函数来实现,常见的聚合函数有:

例子:

-- 1.count
-- 统计班级里共有多少同学
select count (*) from student;
select count (0) from student;
-- 统计班级收集的qq_mail有多少个,qq_mail为null的数据不会计入结果
select count(qq_mail) from student;
-- 2.sum
-- 统计数学成绩总分
select sum(math) from exam_result;
-- 不及格<60的总分,没有结果,返回null
select sum(math) from exam_resullt where math<60;
-- 3.avg
-- 统计平均总分
select avg (chinese+math+english) 平均总分 from exam_result;
-- 4.max
-- 返回英语最高分
select max(english) from exam_result;
-- 5.min
-- 返回>70分以上的数学最低分
select min(math) from exam_result where math>70;

4.1.2 group by 子句

select中使用group by 子句可以对指定列进行分组查询。需要满足:使用group by 进行分组查询时,select指定的字段必须是“分组依据字段”,其他字段若想出现在select 中则必须包含在聚合函数中。

语法:

select 列名1,sum(列名2),… from 表名 group by 列名1;

例子:

测试表emp(职员表)中有id(主键)、name(姓名)、role(角色)、salary(薪水)。

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

select role,max(salary),min(salary),avg(salary) from emp group by role;

4.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;

4.2 联合查询

实际开发中往往数据来自不同的表,所以需要多表联合查询。多表查询是对多表的数据取笛卡尔积。

注意:关联查询可以对关联表使用别名。

4.2.1 内连接

语法:

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

4.2.2 外连接

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

语法:

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

4.2.3 子查询

子查询是指嵌入在其他sql语句中的select语句中的select语句,也叫嵌套查询

单行子查询:返回一行记录的子查询

例子:查询与“张三”同学的同班同学

select * from student where classes_id=(select classes_id from student where name='张三');

多行子查询:返回多行记录的子查询

例子:查询“语文”或“英文”课程的成绩信息

1)[not]in关键字

-- 使用in
select * from score where courde_id in(select id from course where name='语文'orname='英文');
-- 使用not in
select * from score where course_id not in (select id from course where name !='语文'and name!='英文');

2)[not] exists关键字:

-- 使用exists
select * from score where exists (select sco.id from course cou 
where (name='语文' or name!='英文') and cou.id =sco.course_id);
-- 使用not exists
select * from score sco where not exists (select sco.id from course cou 
where (name!='语文' and name !='英文') and cou.id =sco.course_id);

在from字句中使用子查询:子查询语句出现在from字句中。治理要用到数据查询的技巧,把一个子查询当做一个临时表使用。

4.2.4 合并查询

在实际应用中,为了合并多个select的执行结果,可以使用集合操作符union,union all。使用union和union all 时,前后查询的结果集中,字段需要一致。

1)union

该操作符用于取得两个结果集的并集。当使用该操作符时,会自动去掉结果集中的重复行。

例子:查询id小于3,或者名字为“英文”的课程:

在from字句中使用子查询:子查询语句出现在from字句中。治理要用到数据查询的技巧,把一个子查询当做一个临时表使用。
4.2.4 合并查询
在实际应用中,为了合并多个select的执行结果,可以使用集合操作符union,union all。使用union和union all 时,前后查询的结果集中,字段需要一致。
1)union
该操作符用于取得两个结果集的并集。当使用该操作符时,会自动去掉结果集中的重复行。
例子:查询id小于3,或者名字为“英文”的课程:

2)union all

该操作用于取得两个结果集的并集。当使用该操作时,不会去掉结果集中的重复行。

例子:查询id小于3,或者名字为”英文“的课程。

发布了62 篇原创文章 · 获赞 9 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43669007/article/details/104101558