外键约束三种形式

外键有三种约束模式:

  • district : 严格模式(默认的),父表不能删除或更新一个已经被子表数据引用的记录。
  • cascade : 级联模式,父表的操作,对应子表关联的数据也跟着操作。
  • set null : 置空模式,父表被操作之后,子表对应的外键字段被置空。

  • 通常情况下,合理的模式应该是这样的:删除父表中的数据,子表置空,更新父表的时候,子表做级联操作。

  • -- 指定模式的语法:
    foreign key(外键字段) references 父表(外键字段) on delete 模式  on update 模式
    
    
    foreign key(外键字段) references 父表(外键字段) on delete set null on update cascade
    
    -- 创建外键,指定模式:删除置空,更新级联
    create table my_foreignthree(
        id int primary key auto_increment,
        name varchar(20) not null,
        c_id int,
        -- 增加外键
        foreign key(c_id)
        -- 引用表
        references my_class(c_id)
        -- 指定删除模式
        on delete set null
        -- 指定更新模式
        on update cascade
    )charset utf8;                                                                                                                                                                                                                                                                                                                                                                                                                                        

  • 联合查询

    联合查询:将多次查询(多条select语句),在记录上进行拼接。

    -- 基本语法
    多条select构成,每一条select语句获取的字段数必须严格一致(与字段类型无关)。
    
    
    select 语句1
    union [union 选项]
    select 语句2...
    
    
    union选项: 
         all: 保留所有
         distinct: 去重:默认的
    
    -- 联合查询
    select c_id,c_name,c_room from my_class
    union -- 去重
    select studentid,number,name from my_student;
    
    -- 联合查询
    select c_id,c_name,c_room from my_class
    union all -- 不去重
    select studentid,number,name from my_student;
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    外键约束三种形式_第1张图片

    联合查询的意义

    联合查询意义:

    • 查询同一张表,但是需求不同:如查询学生信息,男生身高升序,女生身高降序
    • 多表查询:多张表的结构是一样的,保存的数据的结构也是相同的。

    联合查询中:order by不能直接使用,需要对查询语句使用括号才行。

    -- 需求:男生升序,女生降序(年龄)
    (select * from my_student where gender='boy' order by age asc) 
    union
    (select * from my_student where gender='girl' order by age desc); 
    • 1
    • 2
    • 3
    • 4

    外键约束三种形式_第2张图片

    可以发现,虽然使用了order by关键字,但是并没有达到预期效果,此时需要注意的是,若要order by生效,必须搭配limit,limit使用限定的最大数。

    -- 需求:男生升序,女生降序(年龄)
    (select * from my_student where gender='boy' order by age asc limit 500) 
    union
    (select * from my_student where gender='girl' order by age desc limit 500); 
    • 1
    • 2
    • 3
    • 4

    外键约束三种形式_第3张图片

    子查询

    子查询:查询是在某一个查询结果集中查询

    子查询分类

    子查询有两种分类方式:按位置,按结果

    • 按位置:子查询(select语句)在外部查询(select 语句)中出现的位置。
      from 子查询
      where 子查询
      exists 子查询
    • 按结果分类: 根据子查询得到的结果进行分类
      标量子查询: 子查询得到的结果是一行一列
      列子查询: 子查询得到的额结果是一列多行
      行子查询: 子查询得到的结果是多行多列
      表子查询: 子查询得到的结果是多行多列
    -- 获取java003班的所有学生
    select * from my_student where c_id=?
    select c_id from my_class where c_name='java003'
    -- 标量子查询
    select * from my_student where c_id= (select c_id from my_class where c_name='java003')
    • 1
    • 2
    • 3
    • 4
    • 5

    外键约束三种形式_第4张图片

    -- 查询有效班级里的学生信息
    -- 确定数据源
    select * from my_student where c_id in(?)
    
    -- 确定有效班级
    select c_id from my_class
    
    -- 列子查询
    select * from my_student where c_id in (select c_id from my_class);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    外键约束三种形式_第5张图片

    -- 查询学生中年龄最大且身高最高的学生
    -- 行子查询
    select * from my_student where (age,height) = (select max(age),max(height) from my_student);
    • 1
    • 2
    • 3

    外键约束三种形式_第6张图片

    -- 找出每个班中最高的学生
    -- 表子查询
    select * from (select * from my_student order by height desc) as student group by c_id;
    • 1
    • 2
    • 3

    外键约束三种形式_第7张图片

    exists子查询:用来判断某些条件是否满足(跨表),exists是接在where之后,exists返回的结果:0或者1

    -- 查询有所属班级的所有学生信息
    select * from my_student where exists(select * from my_class)

你可能感兴趣的:(外键约束三种形式)