zoukankan      html  css  js  c++  java
  • 表之间的关系、外键的变种

    表之间的关系:

    双向一对多的时候就另外定义一个表  然后把两个表的外键关联,体现他们的关系

    一对多  :一般定义外子表 父表后再定义一个他们关系的表

    一对一就是用foreign key 外键和unique 唯一索引来设置

    一对多:

    快速理解foreign key

    员工信息表有三个字段:工号  姓名  部门

    公司有3个部门,但是有1个亿的员工,那意味着部门这个字段需要重复存储,部门名字越长,越浪费

    解决方法:

    我们完全可以定义一个部门表

    然后让员工信息表关联该表,如何关联,即foreign key

    表类型必须是innodb存储引擎,且被关联的字段,即references指定的另外一个表的字段,必须保证唯一
    create table department(
    id int primary key,
    name varchar(20) not null
    )engine=innodb;
    
    #dpt_id外键,关联父表(department主键id),同步更新,同步删除
    create table employee(
    id int primary key,
    name varchar(20) not null,
    dpt_id int,
    constraint fk_name foreign key(dpt_id)
    references department(id)
    on delete cascade
    on update cascade 
    )engine=innodb;
    
    
    #先往父表department中插入记录
    insert into department values
    (1,'欧德博爱技术有限事业部'),
    (2,'艾利克斯人力资源部'),
    (3,'销售部');
    
    
    #再往子表employee中插入记录
    insert into employee values
    (1,'egon',1),
    (2,'alex1',2),
    (3,'alex2',2),
    (4,'alex3',2),
    (5,'李坦克',3),
    (6,'刘飞机',3),
    (7,'张火箭',3),
    (8,'林子弹',3),
    (9,'加特林',3)
    ;
    
    
    #删父表department,子表employee中对应的记录跟着删
    mysql> delete from department where id=3;
    mysql> select * from employee;
    +----+-------+--------+
    | id | name  | dpt_id |
    +----+-------+--------+
    |  1 | egon  |      1 |
    |  2 | alex1 |      2 |
    |  3 | alex2 |      2 |
    |  4 | alex3 |      2 |
    +----+-------+--------+
    
    
    #更新父表department,子表employee中对应的记录跟着改
    mysql> update department set id=22222 where id=2;
    mysql> select * from employee;
    +----+-------+--------+
    | id | name  | dpt_id |
    +----+-------+--------+
    |  1 | egon  |      1 |
    |  3 | alex2 |  22222 |
    |  4 | alex3 |  22222 |
    |  5 | alex1 |  22222 |
    +----+-------+--------+
    一对多

    多对多:

    多对多的时候你就需要建立你的多对多的表了,也就是建立两个表之后还要建立一个他们的关系表

    #多对多
    三张表:出版社,作者信息,书
    
    多对多:一个作者可以写多本书,一本书也可以有多个作者,双向的一对多,即多对多
      
    关联方式:foreign key+一张新的表
    =====================多对多=====================
    create table author(
    id int primary key auto_increment,
    name varchar(20)
    );
    
    
    #这张表就存放作者表与书表的关系,即查询二者的关系查这表就可以了
    create table author2book(
    id int not null unique auto_increment,
    author_id int not null,
    book_id int not null,
    constraint fk_author foreign key(author_id) references author(id)
    on delete cascade
    on update cascade,
    constraint fk_book foreign key(book_id) references book(id)
    on delete cascade
    on update cascade,
    primary key(author_id,book_id)
    );
    
    
    #插入四个作者,id依次排开
    insert into author(name) values('egon'),('alex'),('yuanhao'),('wpq');
    
    #每个作者与自己的代表作如下
    1 egon: 
          1 九阳神功
          2 九阴真经
          3 九阴白骨爪
          4 独孤九剑
          5 降龙十巴掌
          6 葵花宝典
    
    
    2 alex: 
          1 九阳神功
          6 葵花宝典
    
    3 yuanhao:
          4 独孤九剑
          5 降龙十巴掌
          6 葵花宝典
    
    4 wpq:
          1 九阳神功
    
    
    insert into author2book(author_id,book_id) values
    (1,1),
    (1,2),
    (1,3),
    (1,4),
    (1,5),
    (1,6),
    (2,1),
    (2,6),
    (3,4),
    (3,5),
    (3,6),
    (4,1)
    ;
    多对多
    #   作者表
    create table author(
        id int not null auto_increment primary key,
        name varchar(12) not null
    ) engine = innodb;
    
    
    
    
    
    #书籍表
    create table book(
        id int not null auto_increment primary key,
        name varchar(20) not null 
    )engine = innodb;
    
    
    
    
    # 书籍和作者多对多的表
    create table auth2book(
        id int not null auto_increment primary key,
        author_id int not null,   # 自定义一个字段
    book_id int not null,    # 自定义字段
    constraint fk_author foreign key(author_id) references author(id),  # 让你的author_id字段和author的id进行关联
        constraint fk_book foreign key(book_id) references book(id)  # 让你的book_id 字段和book表的id进行关联
    ); 

     其实多对多的表就是建立一个第三张表 然后在第三表中建立一两个foreign  key关系

    一对一:

    一对一
    两张表:学生表和客户表
    
    一对一:一个学生是一个客户,一个客户有可能变成一个学校,即一对一的关系
    
    关联方式:foreign key+unique
    一定是student来foreign key表customer,这样就保证了:
    #1 学生一定是一个客户,
    #2 客户不一定是学生,但有可能成为一个学生
    
    
    create table customer(
    id int primary key auto_increment,
    name varchar(20) not null,
    qq varchar(10) not null,
    phone char(16) not null
    );
    
    
    create table student(
    id int primary key auto_increment,
    class_name varchar(20) not null,
    customer_id int unique, #该字段一定要是唯一的
    foreign key(customer_id) references customer(id) #外键的字段一定要保证unique
    on delete cascade
    on update cascade
    );

    .

  • 相关阅读:
    C#实现京东登录密码加密POST
    查询SQL Server数据库所有表字段备注
    DataGridView数值列和日期列
    (转)Android 系统 root 破解原理分析
    Dynamics AX 中重点数据源方法
    .NET中Debug模式与Release模式
    DotNetBar的初步使用
    省市区联动小功能
    多余的Using Namespaces或引用会影响程序的执行效率么?
    MSIL指令集
  • 原文地址:https://www.cnblogs.com/zhaoyunlong/p/9170529.html
Copyright © 2011-2022 走看看