MySQL表的增删改查(进阶)

1.数据库约束

1.1约束类型

not null                -指定列不能存储null的值。

unique                -保证某列的每行必须有唯一的值。

default                -规定没有给列赋值时的默认值。

primary key        -not null 和 unique的结合。确保某列(或俩个列多个列的结合) 有唯一标识,有助于更容易跟快速地找到表中的一个特定得记录。

foreign key        -保证一个表中的数据匹配到另一个表中值的参照完整性。

check                -保证列中的值符合指定的条件。对于MySQL数据库,对check字句进行分析,但是忽略check字句

1.2 null 约束

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

-- 重新设置学生表结构

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列为空,默认值unkown:

-- 重新设置学生表结构

drop table if exists student;

create table student (  

id int not null,  

sn int unique,  

name varchar(20) default 'unkown',  

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 'unkown',  

qq_mail varchar(20)

);

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

-- 主键是 not null和 uique的结合,可以不用 not null

id int primary key auto_increment,

1.6 foreign key:外键约束

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

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

案例:

创建班级表classes,id为主键:

-- 创建班级表,有使用MySQL关键字作为字段时,需要使用``来标识

drop table if exists classes;

create table classes (

id int primary key auto_increment,

name varchar(20),

`desc` varchar(100)

);

创建学生表student,一个学生对应一个班级,一个班级对应多个学生。使用id为主键, classes_id为外键,关联班级表id

-- 重新设置学生表结构

drop table if exists student;

create table student (

id int primary key auto_increment,

sn int unique,

name varchar(20) default 'unkown',  

qq_mail varchar(20),

classes_id int,

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(1),   check (sex ='男' or sex='女')

);

2.表的设计

三大范式:

2.1一对一

 2.2一对多

2.3多对多

 

创建课程表:

-- 创建课程表

drop table if  exists course;

create table course (  

id int primary key auto_increment,  

name varchar(20)

); 

创建学生课程中间表,考试成绩表

-- 创建课程学生中间表:考试成绩表

drop table if exists score;

create table score (  

id int primary key auto_increment,

score decimal(3, 1),  

student_id int,

course_id int,

foreign key (student_id) references student(id),

foreign key (course_id) references course(id)

);

3.新增

插入查询结果

语法:

insert into table_name [ ( colum [, column ...] ) ]   select...

案例:创建一张用户表,设计有name姓名、email邮箱、sex性别、mobile手机号字段。需要把已有的 学生数据复制进来,可以复制的字段为name、qq_mail

-- 创建用户表

drop table if exists test_user;

create table test_user (  

id int primary key auto_increment,  

name varchar(20) comment '姓名',  

age int comment '年龄',  

email varchar(20) comment '邮箱',

sex varchar(1) comment '性别',

mobile varchar(20) comment '手机号'

);

-- 将学生表中的数据复制到用户表

insert into test_user(name, email) select name, qq_mail from student;

4.查询

4.1聚合查询

4.1.1聚合函数

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

案例:

count

-- 统计班级共有多少同学

select count(*) from student;

select count(0) from student;

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

select count(qq_mail) from student; 

sum

-- 统计数学成绩总分

select sum(math) from exam_result;

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

select sum(math)  from exam_result where math < 60;

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;

4.1.2 group by 子句

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

select column1, sum(column2), .. from table group by column1,column3;

案例:

准备测试表及数据:职员表,有id(主键)、name(姓名)、role(角色)、salary(薪水)

create table emp(

id int primary key auto_increment,

name varchar(20) not null,

role varchar(20) not null,

salary numeric(11,2)

);

insert into emp(name, role, salary) values

('马云','服务员', 1000.20),

('马化腾','游戏陪玩', 2000.99),

('孙悟空','游戏角色', 999.11),

('猪无能','游戏角色', 333.5),

('沙和尚','游戏角色', 700.33),

('隔壁老王','董事长', 12000.66);

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

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联合查询

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

 ​​​

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

初始化测试数据:

insert into classes(name, `desc`) values

('计算机系2019级1班', '学习了计算机原理、C和Java语言、数据结构和算法'),

('中文系2019级3班','学习了中国传统文学'),

('自动化2019级5班','学习了机械自动化');

insert into student(sn, name, qq_mail, classes_id) values

('09982','黑旋风李逵','xuanfeng@qq.com',1),

('00835','菩提老祖',null,1), ('00391','白素贞',null,1),

('00031','许仙','xuxian@qq.com',1),

('00054','不想毕业',null,1),

('51234','好好说话','say@qq.com',2),

('83223','tellme',null,2),

('09527','老外学中文','foreigner@qq.com',2);

insert into course(name) values

('Java'),

('中国传统文化'),

('计算机原理'),

('语文'),

('高阶数学'),

('英文');

insert into score(score, student_id, course_id)

values

-- 黑旋风李逵 (70.5, 1, 1),(98.5, 1, 3),(33, 1, 5),(98, 1, 6),

-- 菩提老祖 (60, 2, 1),(59.5, 2, 5),

-- 白素贞 (33, 3, 1),(68, 3, 3),(99, 3, 5),

-- 许仙 (67, 4, 1),(23, 4, 3),(56, 4, 5),(72, 4, 6),

-- 不想毕业 (81, 5, 1),(37, 5, 5),

-- 好好说话 (56, 6, 2),(43, 6, 4),(79, 6, 6),

-- tellme (80, 7, 2),(92, 7, 6);

4.2.1内连接

语法:

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

select 字段 from 表1 别名1 ,表2 别名2 where 连接条件 and 其他条件;

案例:

(1)查询“许仙”同学的 成绩

select sco.score from student stu inner join score sco on stu.id=sco.student_id and stu.name='许仙';

select sco.score from student stu,score sco where stu.id=sco.student_id and stu.name=’许仙';

--inner join 等同于  join  等同于  where

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

-- 成绩表对学生表是多对1关系,查询总成绩是根据成绩表的同学id来进行分组的

select stu.sn, stu.name, stu.qq_mail, sum( sco.score ) from student stu join score sco on stu.id = sco.student_id group by sco.student_id;

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

-- 查询出来的都是有成绩的同学

select * from student stu join score sco on stu.id=sco.student_id;

-- 学生表、成绩表、课程表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;

4.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张表关联查询

select stu.id, stu.sn, stu.name, stu.qq_mail, sco.score, sco.course_id, cou.name

from student stu

left join score sco on stu.id = sco.student_id

left join course cou on sco.course_id = cou.id order by stu.id;

4.2.3自连接

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

案例:

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

-- 先查询“计算机原理”和“Java”课程的id

select id,name from course where name='Java' or name='计算机原理';

-- 再查询成绩表中,“计算机原理”成绩比“Java”成绩 好的信息

select s1.* from score s1, score s2 where s1.student_id = s2.student_id

and s1.score < s2.score

and s1.course_id = 1

and s2.course_id = 3;

-- 也可以使用join on 语句来进行自连接查询

select s1.*

from 

score s1 join score s2 on s1.student_id = s2.student_id

and s1.score < s2.score

and s1.course_id = 1

and s2.course_id = 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 = '计算机原理';

4.2.4子查询

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

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

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

select * from student where classes_id=(select classes_id from student where name='不想毕业');

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

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

1. [not] in关键字:

-- 使用 in

select * from score where course_id in (select id from course where name='语文' or name='英文');

-- 使用 not in

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

可以使用多列包含:

-- 插入重复的分数:

score, student_id, course_id列重复

insert into score(score, student_id, course_id) values

(70.5, 1, 1),

(98.5, 1, 3),

(60, 2, 1);

-- 查询重复的分数

select * from score where ( score, student_id, course_id ) in ( select score, student_id, course_id from score group by score, student_id, course_id having count( 0 ) > 1 );

2.[not] exists 关键字

-- 使用 exists

select * from score sco 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); 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿瞒有我良计15

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值