MySQL 基础二(B 站黑马程序员MySQL教程笔记)

四、约束

  • 概念

约束是作用于表中字段上的规则,用于限制存储在表中的数据,目的是保证数据库中数据的正确、有效性和完整性约束

约束描述关键字
非空约束限制该字段的数据不能为nullnot null
唯一约束保证该字段的所有数据都是唯一不重复的unique
主键约束主键是一行数据的唯一标识、要求非空且唯一primary key
默认约束保存数据时,如果未指定该字段的值,则采用默认值default
检查约束保证字段值满足某一个条件check
外键约束用来让两张表的数据之间建立连接,保证数据的一致性和完整性foreign key
create table user(
    id int primary key auto_increment comment '主键',
    name varchar(10) not null  unique  comment '姓名',
    age int check ( age>0 and age<120) comment '年龄',
    status char(1) default '1' comment '状态',
    gender char(1) comment '性别'
) comment '用户表';

#插入数据
insert into user(name, age, status, gender) values ('Tom1',19,'1','男'),('Tom2',25,'0','男');
insert into user(name, age, status, gender) values ('Tom3',19,'1','男');#验证主键的自动增长
insert into user(name, age, status, gender) values (null,19,'1','男');验证非空
insert into user(name, age, status, gender) values ('Tom3',19,'1','男');验证唯一
  • 外键约束

语法

create table 表名(

           字段名 数据类型,...[constraint][外键名称] foreign key(外键字段名) references 主表(主表列名)

);

alter table 表名 add constrint 外键名称 foreign key (外键字段名)references 主表(主表列名);
行为说明
no action当父表中删除/更新对应记录时,首先检查该记录是否有对应外键若有则不允许删除/更新(与restrict一致)
restrict当父表中删除/更新对应记录时,首先检查该记录是否有对应外键若有则不允许删除/更新(与no action一致)
cascade当父表中删除/更新对应记录时,首先检查该记录是否有对应外键若有也删除/更新外键在子表中的记录
set null当父表中删除/更新对应记录时,首先检查该记录是否有对应外键若有设置子表中外键值为null(这就要要求外键允许取null)
set defaull父表有变更时,子表将外键列设置成一个默认的值(lnnodb不支持)
alter table 表名 add constraint 外键名称 foreign key(外键字段)references 主表名(主表字段名) on update 行为 on delete 行为;
#准备数据
create table dept(
    id int auto_increment comment 'ID' primary key,
    name varchar(50) not null comment '部门名称'
) comment '部门表';
insert into dept(id, name) values (1,'研发部'),(2,'市场部'),(3,'财务部'),(4,'销售部'),(5,'总经办');

create table emp(
    id int auto_increment comment 'ID' primary key,
    name varchar(50) not null comment '姓名',
    age int comment '年龄',
    job varchar(20) comment '职位',
    salary int comment '薪资',
    entrydate date comment '入职时间',
    managerid int comment '直属领导ID',
    dept_id int comment '部门ID'
) comment '员工表';
 insert into emp(id, name, age, job, salary, entrydate, managerid, dept_id) values (1,'金庸',66,'总裁',20000,'2000-01-01',null,5),(2,'金小庸',46,'项目经理',12500,'2005-11-01',1,1),(3,'逍遥',33,'开发',8400,'2000-11-03',2,1),(4,'韦一笑',48,'开发',11000,'2002-02-05',2,1),(5,'常遇',43,'开发',10500,'2004-09-07',3,1),(6,'小赵',19,'程序员鼓励师',6600,'2004-10-01',2,1);

#添加外键(出现蓝色小钥匙)
alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id);
#删除外键
alter table emp drop foreign key  fk_emp_dept_id;
#删除更新行为
alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id) on update cascade on delete cascade;

五、多表查询

  • 多表关系
    一对多(多对一)在多的一方建立外键,指向一的一方的主键
    多对多建立第三张中间表,中间表至少包含两个外键,分别关联两主键
    一对一在任意一方加入外键,关联另外一方的主键,并且设置外键为唯一的(unique)

#多对多
create table student(
    id int auto_increment primary key comment '主键ID',
    name varchar(10) comment '姓名',
    no varchar(10) comment '学号'
) comment '学生表';
insert into  student values (null,'戴笠','2000100101'),(null,'谢逊','2000100102'),(null,'尹红','2000100103'),(null,'韦一笑','2000100104');

create table course(
    id int auto_increment primary key comment '主键ID',
    name varchar(10) comment '课程名称'
) comment '课程表'
insert into course values (null,'java'),(null,'PHP'),(null,'mysql'),(null,'hadoop');

create table student_course(
    id int auto_increment comment '主键' primary key ,
    studentid int  not null  comment '学生ID',
    courseid int  not null  comment '课程ID',
    constraint  fk_courseid foreign key (courseid) references course(id),
    constraint  fk_studentid foreign key (studentid) references student(id)
) comment '学生课程中间表';
INSERT INTO student_course VALUES (null,1,1),(null,1,2),(null,1,3),(null,2,2),(null,2,3),(null,3,4);
  • 多表查询概述
#多表查询
select * from emp,dept;#笛卡尔积
select * from emp,dept where dept_id=dept.id;

#多表查询分类
多表查询select * from emp,dept;#笛卡尔积
select * from emp,dept where dept_id=dept.id;

(1)连接查询

内连接:相当于查询A、B交集部分数据

   select 字段列表 from 表1、表2 where 条件...;

   select 字段列表 from 表1 [inner] join 表2 on 连接条件...;

外连接:左外连接(查询左表所有数据,以及两张表交集部分数据) (会包含左表所有数据)

  select 字段列表 from 表1 left [outer] join 表2 on 条件

              右外连接(查询右表所有数据,以及两张表交集部分数据)(会包含右表所有数据)

自连接:当前表与自身的连接查询,自连接必须使用表别名

  select 字段列表 from 表A 别名A join 表A 别名B on 条件...;

(2)子查询

标量子查询:子查询返回的结果是单个值(数字、字符串、日期)最简单的形式 ,这种子查询成为标量子查询。(<,>...)

列子查询:返回的结果是一列(可以是多行),这种子查询称为列子查询(in,not in, any, some,all)

   (3)联合查询union(去重后接在一起) union all(直接接在一起)【多张表的列数必须保持一致,字段类型也需要保持一致】

select 字段列表 from 表A...

union [all]

select 字段列表 from 表B...

多表查询select * from emp,dept;#笛卡尔积
select * from emp,dept where dept_id=dept.id;
(1)连接查询

内连接:相当于查询A、B交集部分数据

   select 字段列表 from 表1、表2 where 条件...;

   select 字段列表 from 表1 [inner] join 表2 on 连接条件...;

外连接:左外连接(查询左表所有数据,以及两张表交集部分数据) (会包含左表所有数据)

  select 字段列表 from 表1 left [outer] join 表2 on 条件

              右外连接(查询右表所有数据,以及两张表交集部分数据)(会包含右表所有数据)

自连接:当前表与自身的连接查询,自连接必须使用表别名

  select 字段列表 from 表A 别名A join 表A 别名B on 条件...;

 (3)联合查询

 联合查询union(去重后接在一起) union all(直接接在一起)【多张表的列数必须保持一致,字段类型也需要保持一致】

select 字段列表 from 表A...

union [all]

select 字段列表 from 表B.

(2)子查询

标量子查询:子查询返回的结果是单个值(数字、字符串、日期)最简单的形式 ,这种子查询成为标量子查询。(<,>...)

列子查询:返回的结果是一列(可以是多行),这种子查询称为列子查询

(in:在指定的集合范围之内

not in:不在指定的集合范围之内 

any:子查询返回列表中,有任意一个满足即可

some:与any等同,使用some的地方都可以使用any

all:子查询返回列表的所有值都必须满足

行子查询返回的结果是一行(可以是多列)

=,<>,in ,not in

表子查询:返回的结果的多行多列

in

#隐式内连接
select emp.name,dept.name from emp,dept where emp.dept_id=dept.id;
select  e.name,d.name from emp e,dept d where e.dept_id=d.id;#别名简化
#显式内连接
select e.name,d.name from emp e inner join dept d on e.dept_id=d.id;
#左外连接
select e.* from emp e left outer join dept d on e.dept_id = d.id;
#右外连接
select d.*,e.* from emp e right outer join dept d on e.dept_id=d.id;
#自连接
select a.name,b.name from emp a ,emp b where a.managerid=b.id;
select a.name '员工',b.name '领导' from emp a left outer join emp b on a.managerid=b.id;
#联合查询(两次查询结果直接粘在一起)
select * from emp where salary<20000
union all
select * from emp where age<35;
#联合查询(两次查询结果去重后接在一起)
select * from emp where salary<20000
union
select * from emp where age<35;

#子查询(标量子查询)
#查询研发部所有员工信息
select id from dept where name='研发部';#6
select * from emp where dept_id=6;
select * from emp where dept_id=(select id from dept where name='研发部');#子查询
#查询在逍遥入职以后的员工信息
select * from emp where entrydate>(select entrydate from emp where name='逍遥');


#列子查询
select * from emp where dept_id in (select id from dept where name='研发部' or name='总经办');
#行子查询
#查询与逍遥的职位及领导相同的员工信息
select * from emp where (job,managerid) = (select job,managerid from emp where name='逍遥');

#表子查询
#查询与逍遥,小赵职位和领导相同的员工信息
select * from emp where (job,managerid) in (select job,managerid from emp where name='逍遥'or name='小赵');
#查询入职是2000-01-01之后的员工信息及其部门信息
select * from emp where entrydate>'2000-01-01';#这是一张表
select * from (select * from emp where entrydate>'2000-01-01') e left join dept d on e.dept_id=d.id;

练习

create table salgrade
(
    grade int,
    losal int,
    hisal int
) comment '薪资等级表';
insert into salgrade
values (1, 0, 5000),
       (2, 5000, 10000),
       (3, 10000, 15000),
       (4, 15000, 20000);
#查询员工的姓名、年龄、职位、部门信息(隐式内连接)
select e.name, e.age, e.job, d.name
from emp e,
     dept d
where e.dept_id = d.id;
#查询年龄小于30岁的员工的姓名、年龄、职位、部门信息(显式内连接)
select e.name, e.age, e.job, d.name
from emp e
         inner join dept d on d.id = e.dept_id
where e.age < 40;
#查询拥有员工的部门ID、部门名称
select distinct e.dept_id, d.name
from emp e
         inner join dept d on d.id = e.dept_id;
#查询所有年龄大于40岁的员工,及其归属的部门名称,如果员工没有分配部门,也需要展示出来
select e.*, d.name
from emp e
         left join dept d on e.dept_id = d.id
where (e.age > 40);
#查询所有员工的工资等级
select e.name, s.grade
from emp e,
     salgrade s
where e.salary >= s.losal
  and e.salary <= s.hisal;
#查询研发部所有员工及工资等级(格式化后形式)
select e.*, s.grade
from emp e,
     salgrade s,
     dept d
where e.salary between s.losal and s.hisal
  and e.dept_id = d.id
  and d.name = '研发部';
#查询研发部的平均工资
select d.name,avg(e.salary) '平均工资' from emp e ,dept d where d.name='研发部';
#查询工资比逍遥高的员工信息
select * from emp where salary>(select salary from emp where name='逍遥');

六、事务

(1)事务

是一组操作的集合,它是一个不可分割的工作单位,事务会把所有操作作为一个整体一起向系统提交或撤销操作请求即这些操作要么同时成功,要么同时失败。

(2)事务操作

方式一

1.查看/设置事务提交方式    select @@autocommit;#查看事务的提交方式 1自动提交0手动提交

set @@autocommit=0;#改为手动

2.事务

3.提交事务:commit;

4.回滚事务;rollback;

方式二

1.开启事务:start transaction 或begin

2.提交事务:commit;

3.回滚事务;rollback;

create table account(
    id int auto_increment primary key comment '主键ID',
    name varchar(10) comment '姓名',
    money int comment '余额'
) comment '账户表';
insert into account(id, name, money) VALUES (null,'张三',2000),(null,'李四',2000);
#恢复数据
update account set money=2000 where name='张三' or name='李四';

#转账操作(张三给李四转账1000)
#查询张三账户余额
select * from account where name='张三';
#将张三账户减去1000
update account set money=money-1000 where name='张三';
#将李四账户余额加1000
update account set money=money+1000 where name='李四';

select @@autocommit;#1
set @@autocommit=0;#设置手动提交,只有输入提交后数据库才会变化
#转账操作(张三给李四转账1000)
#查询张三账户余额
select * from account where name='张三';
#将张三账户减去1000
update account set money=money-1000 where name='张三';
#将李四账户余额加1000
update account set money=money+1000 where name='李四';
#到这里上面对数据库的改变没有发生
#提交事务
commit;#数据改变了

(3)事务的四大特性(acid)

  • 原子性:事务是不可分割的最小操作单元,要么全部成功,要么全部失败
  • 一致性:事务完成时,必须使得所有数据都保持一致状态
  • 隔离性:数据库系统提供的隔离机制,保证事务在不受外部并发操作影响的独立环境下运行
  • 持久性:事务一旦提交或回滚,它对数据库中的数据的改变就是永久的

(4)并发事务问题

  • 脏读:一个事务读到另一个事务还没有提交的数据
  • 不可重复读:一个事务先后读取同一条记录,但两次读取的数据不同,称之为不可重复读
  • 幻读:一个事务按照条件查询数据时,没有对应的数据行,但是在插入数据时,又发现这行数据已经存在,好像出现了幻影

(5)事务隔离级别

隔离级别(由低到高)脏读不可重复读幻读
read uncommitted√ 
read committed×
repeatable read××
serializable×××
  • 查看事务隔离级别:select @@transaction_isolation;
  • 设置事务隔离级别:set [session/global] transaction isolation level [read uncommitted/read committed/repeatable read/serializable]
  • 注意:事务隔离级别越高,数据越安全,性能越低
  • 0
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值