MySQL约束条件和多表查询方式详解

一、约束
什么是约束?
简述:除了数据类型以外的约束的
为什么使用约束?
简述:为了保证数据的合法性 完整性;
二、约束分类:
  not null 跟整型时使用其作用是限制插入数据不能为空

create table student (id ind,name char(10) not null);

  default 默认值(并不是约束)其作用是在于当插入的数据为空的时候使用默认值并不会报错;

 create table user (id ind,name char(10) not null,sex char(1) default "woman");

  unique 唯一约束 其作用是限制插入表中的数据不能够重复但是可以为空并且其也存在索引可以提高查询数据的效率。

                     单列唯一约束
                     create table t5(idcard char(18) unique);
                     多列联合唯一约束
                     create table t6(idcard char(18),phonenumber char(11),unique(idcard,phonenumber));
                     意思: 身份证相同 并且 手机号相同 那就叫相同


  primary key 主键约束其是一种概念并不是物理层面上的它的作用跟(not null与unique组合)的效果一样,并且主键约束其本身有索引可以提高查询的效率,primary key其常跟整型连用
  

  有主键 和没有主键的区别?
  1.无法区分两个相同记录 比如班级里有两个人名字相同
  2.有主键则意味有这索引 效率更高
  3.可以建立关联关系

语法:
                create table stu (stuid int primary key,name char(3));
                 #下面是使用唯一约束与非空约束结合的代码:
                create table t7(id int unique not null,name char(3));

 多列联合主键:
                create table t8(idcard char(18),phonenumber char(11),primary key(idcard,phonenumber));

  auto_increment 起到对于整型字段名自增1的效果

#语法:
                create table t9(id int primary key auto_increment,name char(3));
#如果主键是自动增长  你可以跳过这个字段 也可以为它插入null  都可以
#修改自动增长的起始位置  **
                alter table t9 auto_increment = 7;
#注意: 自动增长 只能用于整型

  foreign key 的作用是将references拿到的数据放入要存放的字段名的数据中
  references 的作用是起到了连接你要关联的表从而在关联的表中拿取数据

# mysql提供了 foreign key 专门用于为表和表之间 建立物理关联
# 
# 
# 思考 表里存储的是一条条的记录
#      两个表之间能产生的关系有哪些?
#         现有 A B两张表
#         1.多对一
#         2.一对一
#         3.多对多
# 
#       在查找表之间的关系时  要分别站在 不同表去思考
#         1. 从员工出发  员工对于部门来说 时 多个员工对应一个部门
#         2. 从部门出发  一个部门对应多个员工
#             如果两个得到的关系不同 则认为 这种多对一关系是单向
# 
# 
#       先创建部门表
       create table dept(id int primary key auto_increment,name char(10),manager char(10));
#       在创建员工表
       create table emp(id int primary key auto_increment,name char(10),dept_id int,foreign key(dept_id) references dept(id));
# 
# 
#         需求: 设计  学员表 和 班级表  五分钟练习
#               两个表多对一的关系  通过外键来进行关联
#               外键加在谁身上?   加到从表上
               create table class(id int primary key auto_increment,name char(10));
               create table student(id int primary key auto_increment,name char(10),c_id int,foreign key(c_id) references class(id));
# 
#         总结: 外键的作用  表与表之间建立联系
#         添加外键约束时: 产生的限制
#                        被关联的表需要先被创建
#                        部门数据(主表)应该先插入   员工数据(从表)后插入
#                        在删除部门数据前(主表)前 要保证该部门的员工数据都删除了
#                        在更新部门编号前  要先保证没有员工关联到这个部门
# 
#         简单的说 外键指的是 另一张的主键
# 
#         外键加上以后 主表中的数据 删除 和更新时 都受到限制
#             解决的方案是为 外键 添加 级联操作

补充:级联操作

# 级联操作
#             指的是就是同步更新和删除
#             语法:在创建外键时  在后面添加  on update cascade   同步更新
#                                            on delete cascade   同步删除

              # 实例:
              create table class(id int primary key auto_increment,name char(10));

              create table student(
              id int primary key auto_increment,
              name char(10),
              c_id int,
              foreign key(c_id) references class(id)
              on update cascade
              on delete cascade
              );

               insert into class value(null,"python3期");
               insert into student value(null,"罗傲宇",1);

             # 对主表的id进行更新
             # 以及删除某条主表记录 来验证效果

三、分表及其查询方式

1、为什么要分表

简述:分表可以更好处理重复使用同一个表数据所造成的数据冗余、效率低、扩张性差问题。

2、分表所存在的关系

    一对一关系:指的是一张表中的数据只能与另一张表中有联系的一条数据进行关联

    多对一关系:指的是一张表中的数据可以包含另一张表中多条数据

    多对多关系:指的是两张表中数据有相互包含的关系

3、查询方式

3.1多表查询与子查询

# 多表查询
#     在多个表中查询需要的数据
#     例如:有班级表 和学生表
#         给你已给班级名称  请查询所有的学员数据
#         先查班级表 得到一个班级的id  再根据id去学院表查询对应的学员

    # 准备数据:
    create table emp (id int,name char(10),sex char,dept_id int);
     insert emp values(1,"大黄","m",1);
     insert emp values(2,"老王","m",2);
     insert emp values(3,"老李","w",30);

    create table dept (id int,name char(10));
    insert dept values(1,"市场");
    insert dept values(2,"财务");
    insert dept values(3,"行政");

    # 多表查询的方式
    # 1.笛卡尔积查询
    #     什么是笛卡尔积,用坐标中的一条记录 去链接另一张表的所有记录
    #     就像是把 两张表的数据做了一个乘法
    #     这将导致 产生大量的无用重复数据
    #         我们要的效果是:员工表中的部门id 与 部门表中的id相同 就拼接在一起
    #     用 where 筛选出正确的数据
      select *from emp,dept where emp.dept_id = dept.id;

      # on关键字
      #   作用 用于多表查询是 进行条件限制
        select *from emp,dept on emp.dept_id = dept.id;  #这是错误的语法  因为 on 它只能用在专门多表查询语句中


      # 2.内连接查询
        inner join
        select *from emp inner join dept  on emp.dept_id = dept.id;

       # 查询 所有的员工以及他们所属的部门信息
      # 3.左外连接
      #   left join
      #  左边表中的数据完全显示   右边表中的数据匹配上才显示
       select *from emp left join dept  on emp.dept_id = dept.id;

       # 查询 所有的部门以及他们所有的员工信息
      # 4.右外连接
      #   right join
      #   左边表中的数据匹配上才显示   右边表中的数据完全显示
        select *from emp right join dept  on emp.dept_id = dept.id;

       # 在一个表中 显示多个表中的所有数据
      # 5. 全外链接
      #   full join  mysql不支持  oracle支持
      #   可以通过union 间接实现
      #                   union  表示合并查询  意思是把多个查询结果合并在一起显示
      #                   要求是 被合并的表结构必须相同
      #                   默认去除重复
      # 
      #                   合并但是不去除重复
      #                   union all

       select *from emp right join dept  on emp.dept_id = dept.id
       union
       select *from emp left join dept  on emp.dept_id = dept.id;




      # 总结:多表链接 在书写时 按照填空来书写   如果左边要全部显示 用left join
      #       右边全部显示 用right join
      #       全部显示 把左链接的结果和右链接的结果 合并
      #       当然 也可以更多表一起查 但是 没有意义  并且你要尽量避免 太多表 一起查
      #       最多三张 在多对多的时候

    select *from emp left join dept left join xxtable on emp.dept_id = dept.id;
子查询:
    # 什么是子查询:将上一次查询的结果 作为本次查询的原始数据(或是查询条件)
    # 
    # 需求 查询出工资最高的人的信息
    # 先查询出 财务部 最高工资是多少
    # 拿着最高工资 去表中看 谁的工资和最高工资匹配
    select *from emp where salary = (select max(salary) from emp);

3.2 多对多三表联查

#准备的数据如下:
create table tec(id int,name char(10));
insert into tec value(1,"egon");
insert into tec value(2,"yyh");

create table stu(id int,name char(10));
insert into stu value(1,"大傻");
insert into stu value(2,"中傻");
insert into stu value(3,"小傻");
create table s_t(s_id int,t_id int);
insert into s_t value(1,2);
insert into s_t value(2,2);
insert into s_t value(3,1);

#需求  找出 yyh 这个老师 教过的学生信息
#思路:
#  第一步 到关系表中 去查询 哪些老师教过哪些学生(学生的id)  形成了一个临时表
#   第二步  将上一步得到临时表 与 学生表进行连接
#   第三步 加上额外的筛选条件   老师的name  是  yyh

    select tec.name teacher,stu.name student from
    tec inner join s_t on tec.id = s_t.t_id
    inner join stu on s_t.s_id = stu.id
    where tec.name = "egon" ;

猜你喜欢

转载自www.cnblogs.com/ageliu/p/9651589.html
今日推荐