1. 1 foreign key 2
  2. 则表1的多条记录对应表2的一条记录,即多对一
  3.  
  4. 利用foreign key的原理我们可以制作两张表的多对多,一对一关系
  5. 多对多:
  6. 1的多条记录可以对应表2的一条记录
  7. 2的多条记录也可以对应表1的一条记录
  8.  
  9. 一对一:
  10. 1的一条记录唯一对应表2的一条记录,反之亦然
  11.  
  12. 分析时,我们先从按照上面的基本原理去套,然后再翻译成真实的意义,就很好理解了

1、先确立关系

2、找到多的一方,吧关联字段写在多的一方

一、多对一或者一对多(左边表的多条记录对应右边表的唯一一条记录)

需要注意的:1.先建被关联的表,保证被关联表的字段必须唯一。

      2.在创建关联表,关联字段一定保证是要有重复的。

其实上一篇博客已经举了一个多对一关系的小例子了,那我们在用另一个小例子来回顾一下。

这是一个书和出版社的一个例子,书要关联出版社(多个书可以是一个出版社,一个出版社也可以有好多书)。

谁关联谁就是谁要按照谁的标准。

  1. 书要关联出版社
  2. 被关联的表
  3. create table press(
  4. id int primary key auto_increment,
  5. name char(20)
  6. );
  7. 关联的表
  8. create table book(
  9. book_id int primary key auto_increment,
  10. book_name varchar(20),
  11. book_price int,
  12. press_id int,
  13. constraint Fk_pressid_id foreign key(press_id) references press(id)
  14. on delete cascade
  15. on update cascade
  16. );
  17.  
  18. 插记录
  19. insert into press(name) values('新华出版社'),
  20. ('海燕出版社'),
  21. ('摆渡出版社'),
  22. ('大众出版社');
  23. insert into book(book_name,book_price,press_id) values('Python爬虫',100,1),
  24. ('Linux',80,1),
  25. ('操作系统',70,2),
  26. ('数学',50,2),
  27. ('英语',103,3),
  28. ('网页设计',22,3);

运行结果截图:

二、一对一

例子一:用户和管理员(只有管理员才可以登录,一个管理员对应一个用户)

管理员关联用户

  1. ===========例子一:用户表和管理员表=========
  2. 先建被关联的表
  3. create table user(
  4. id int primary key auto_increment, #主键自增
  5. name char(10)
  6. );
  7. 在建关联表
  8. create table admin(
  9. id int primary key auto_increment,
  10. user_id int unique,
  11. password varchar(16),
  12. foreign key(user_id) references user(id)
  13. on delete cascade
  14. on update cascade
  15. );
  16. insert into user(name) values('susan1'),
  17. ('susan2'),
  18. ('susan3'),
  19. ('susan4'),
  20. ('susan5'),
  21. ('susan6');
  22. insert into admin(user_id,password) values(4,'sds156'),
  23. (2,'531561'),
  24. (6,'f3swe');

运行结果截图:

例子二:学生表和客户表

  1. ========例子二:学生表和客户表=========
  2. create table customer(
  3. id int primary key auto_increment,
  4. name varchar(10),
  5. qq int unique,
  6. phone int unique
  7. );
  8. create table student1(
  9. sid int primary key auto_increment,
  10. course char(20),
  11. class_time time,
  12. cid int unique,
  13. foreign key(cid) references customer(id)
  14. on delete cascade
  15. on update cascade
  16. );
  17. insert into customer(name,qq,phone) values('小小',13564521,11111111),
  18. ('嘻哈',14758254,22222222),
  19. ('王维',44545522,33333333),
  20. ('胡军',545875212,4444444),
  21. ('李希',145578543,5555555),
  22. ('李迪',754254653,8888888),
  23. ('艾哈',74545145,8712547),
  24. ('啧啧',11147752,7777777);
  25. insert into student1(course,class_time,cid) values('python','08:30:00',3),
  26. ('python','08:30:00',4),
  27. ('linux','08:30:00',1),
  28. ('linux','08:30:00',7);

运行结果截图:

三、多对多(多条记录对应多条记录)

书和作者(我们可以再创建一张表,用来存book和author两张表的关系)

要把book_id和author_id设置成联合唯一

联合唯一:unique(book_id,author_id)

联合主键:alter table t1 add primary  key(id,avg)

  1. 多对多:一个作者可以写多本书,一本书也可以有多个作者,双向的一对多,即多对多
  2.   
  3. 关联方式:foreign key+一张新的表

  1. ========书和作者,另外在建一张表来存书和作者的关系
  2. #被关联的
  3. create table book1(
  4. id int primary key auto_increment,
  5. name varchar(10),
  6. price float(3,2)
  7. );
  8. #========被关联的
  9. create table author(
  10. id int primary key auto_increment,
  11. name char(5)
  12. );
  13. #========关联的
  14. create table author2book(
  15. id int primary key auto_increment,
  16. book_id int not null,
  17. author_id int not null,
  18. unique(book_id,author_id),
  19. foreign key(book_id) references book1(id)
  20. on delete cascade
  21. on update cascade,
  22. foreign key(author_id) references author(id)
  23. on delete cascade
  24. on update cascade
  25. );
  26. #========插入记录
  27. insert into book1(name,price) values('九阳神功',9.9),
  28. ('葵花宝典',9.5),
  29. ('辟邪剑谱',5),
  30. ('降龙十巴掌',7.3);
  31. insert into author(name) values('egon'),('e1'),('e2'),('e3'),('e4');
  32. insert into author2book(book_id,author_id) values(1,1),
  33. (1,4),
  34. (2,1),
  35. (2,5),
  36. (3,2),
  37. (3,3),
  38. (3,4),
  39. (4,5);

多对多关系举例

用户表,用户组,主机表

  1. -- 用户组
  2. create table user (
  3. id int primary key auto_increment,
  4. username varchar(20) not null,
  5. password varchar(50) not null
  6. );
  7. insert into user(username,password) values('egon','123'),
  8. ('root',147),
  9. ('alex',123),
  10. ('haiyan',123),
  11. ('yan',123);
  12.  
  13. -- 用户组表
  14. create table usergroup(
  15. id int primary key auto_increment,
  16. groupname varchar(20) not null unique
  17. );
  18. insert into usergroup(groupname) values('IT'),
  19. ('Sale'),
  20. ('Finance'),
  21. ('boss');
  22.  
  23. -- 建立userusergroup的关系表
  1.  
  1. create table user2usergroup(
    id int not NULL UNIQUE auto_increment,
    user_id int not null,
    group_id int not NULL,
    PRIMARY KEY(user_id,group_id),
    foreign key(user_id) references user(id)
    ON DELETE CASCADE
    on UPDATE CASCADE ,
    foreign key(group_id) references usergroup(id)
    ON DELETE CASCADE
    on UPDATE CASCADE
    );
  1.  
  1. insert into user2usergroup(user_id,group_id) values(1,1),
    (1,2),
    (1,3),
    (1,4),
    (2,3),
    (2,4),
    (3,4);
  1. -- 主机表
  2. CREATE TABLE host(
  3. id int primary key auto_increment,
  4. ip CHAR(15) not NULL UNIQUE DEFAULT '127.0.0.1'
  5. );
  6. insert into host(ip) values('172.16.45.2'),
  7. ('172.16.31.10'),
  8. ('172.16.45.3'),
  9. ('172.16.31.11'),
  10. ('172.10.45.3'),
  11. ('172.10.45.4'),
  12. ('172.10.45.5'),
  13. ('192.168.1.20'),
  14. ('192.168.1.21'),
  15. ('192.168.1.22'),
  16. ('192.168.2.23'),
  17. ('192.168.2.223'),
  18. ('192.168.2.24'),
  19. ('192.168.3.22'),
  20. ('192.168.3.23'),
  21. ('192.168.3.24');
  22.  
  23. -- 业务线表
  24. create table business(
  25. id int primary key auto_increment,
  26. business varchar(20) not null unique
  27. );
  28. insert into business(business) values
  29. ('轻松贷'),
  30. ('随便花'),
  31. ('大富翁'),
  32. ('穷一生');
  33.  
  34. -- 建立hostbusiness关系表
  35. CREATE TABLE host2business(
  36. id int not null unique auto_increment,
  37. host_id int not null ,
  38. business_id int not NULL ,
  39. PRIMARY KEY(host_id,business_id),
  40. foreign key(host_id) references host(id),
  41. FOREIGN KEY(business_id) REFERENCES business(id)
  42. );
  43.  
  44. insert into host2business(host_id,business_id) values
  45. (1,1),
  46. (1,2),
  47. (1,3),
  48. (2,2),
  49. (2,3),
  50. (3,4);
  1. -- 建立userhost的关系
  2. create table user2host(
  3. id int not null unique auto_increment,
  4. user_id int not null,
  5. host_id int not null,
  6. primary key(user_id,host_id),
  7. foreign key(user_id) references user(id),
  8. foreign key(host_id) references host(id)
  9. );
  10.  
  11. insert into user2host(user_id,host_id) values(1,1),
  12. (1,2),
  13. (1,3),
  14. (1,4),
  15. (1,5),
  16. (1,6),
  17. (1,7),
  18. (1,8),
  19. (1,9),
  20. (1,10),
  21. (1,11),
  22. (1,12),
  23. (1,13),
  24. (1,14),
  25. (1,15),
  26. (1,16),
  27. (2,2),
  28. (2,3),
  29. (2,4),
  30. (2,5),
  31. (3,10),
  32. (3,11),
  33. (3,12);

练习

MySQL数据库----表与表之间的关系的更多相关文章

  1. 点评阿里JAVA手册之MySQL数据库 (建表规约、索引规约、SQL语句、ORM映射)

    下载原版阿里JAVA开发手册  [阿里巴巴Java开发手册v1.2.0] 本文主要是对照阿里开发手册,注释自己在工作中运用情况. 本文内容:MySQL数据库 (建表规约.索引规约.SQL语句.ORM映 ...

  2. php面试专题---mysql数据库分库分表

    php面试专题---mysql数据库分库分表 一.总结 一句话总结: 通过数据切分技术将一个大的MySQLServer切分成多个小的MySQLServer,既攻克了写入性能瓶颈问题,同一时候也再一次提 ...

  3. 利用navcat为mysql数据库单独的表赋权限及表结构同步

    为mysql数据库单独的表赋权限 场景:考勤系统需要拿OA数据库td_oa中的flow_run和flow_run_data表中的数据做考勤计算 考勤系统只需要读取这两张表的数据,所以只需要开通一个单独 ...

  4. MySQL数据库之单表查询中关键字的执行顺序

    目录 MySQL数据库之单表查询中关键字的执行顺序 1 语法顺序 2 执行顺序 3 关键字使用语法 MySQL数据库之单表查询中关键字的执行顺序 1 语法顺序 select distinct from ...

  5. 4.mysql数据库创建,表中创建模具模板脚本,mysql_SQL99标准连接查询(恩,外部连接,全外连接,交叉连接)

     mysql数据库创建,表创建模等模板脚本 -- 用root用户登录系统,运行脚本 -- 创建数据库 create database mydb61 character set utf8 ; -- ...

  6. mysql管理 ------查看 MySQL 数据库中每个表占用的空间大小

    如果想知道MySQL数据库中每个表占用的空间.表记录的行数的话,可以打开MySQL的 information_schema 数据库.在该库中有一个 TABLES 表,这个表主要字段分别是: TABLE ...

  7. freeswitch用户整合(使用mysql数据库的用户表)

    转:freeswitch用户整合(使用mysql数据库的用户表) freeswitch是一款强大的voip服务器,可以语音和视频.但是它默认是采用/directory文件夹下的xml来配置用户的,对于 ...

  8. MySQL数据库语法-多表查询练习一

    MySQL数据库语法-多表查询练习一 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客主要介绍的多表查询的外键约束,以及如何使用外链接和内连接查询数据信息. 一.数据表和测试 ...

  9. MySQL数据库查看数据表占用空间大小和记录数

    MySQL数据库中每个表占用的空间.表记录的行数的话,可以打开MySQL的 information_schema 数据库.在该库中有一个 TABLES 表,这个表主要字段分别是: TABLE_SCHE ...

  10. 先排序然后union all失效,mysql数据库多个表union all查询并排序的结果为什么错误

    mysql数据库多个表union all查询并排序的结果为什么错误? 群主,我想进行一个表的查询,先把表中某个字段的内容查出,然后其他的再排序,我用union all连接两个表的查询结果排序是错的 比 ...

随机推荐

  1. Shell脚本导入外部脚本内容

    vim subscript.sh #!/bin/bash tool="ApacheSpark" vim main.sh #!/bin/bash source /home/wx/su ...

  2. C 语言实现增量式PID

    一直以来,pid都是控制领域的经典算法,之前尝试理解了很久,但还是一知半解,总是不得要领,昨天模仿着别人的代码写了一个增量式pid的代码. 我的理解就是pid其实就是对你设置的预定参数进行跟踪.在控制 ...

  3. 微信小程序中target与currentTarget

    target在事件流的目标阶段:currentTarget在事件流的捕获,目标及冒泡阶段.但事件流处于目标阶段,target与currentTarget指向一样, 而当处于捕获和冒泡阶段的时候,tar ...

  4. ubuntu16.04下安装pycharm

    下面开始教程 先在PyCharm官网下载安装包 链接:https://www.jetbrains.com/pycharm/download/#section=linux 选择平台为Linux,可以看到 ...

  5. (sklearn)机器学习模型的保存与加载

    需求: 一直写的代码都是从加载数据,模型训练,模型预测,模型评估走出来的,但是实际业务线上咱们肯定不能每次都来训练模型,而是应该将训练好的模型保存下来 ,如果有新数据直接套用模型就行了吧?现在问题就是 ...

  6. PXE配置手记(Linux)

    服务器端:RHEL5(静态IP 192.168.1.101) 源Gentoo系统:服务器上的Gentoo系统就来自于它,编译内核也是在这台机器上执行的 无盘客户端:网卡是 AMD PCnet32 支持 ...

  7. PL/SQL常用语法及举例

    PLSQL语句 DECLARE 声明部分 BEGIN 程序编写,SQL语句 EXECPTION 处理异常 END; / 声明部分(DECLARE) SQL> set serveroutput o ...

  8. Find The Multiple--POJ1426

    Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...

  9. GDB常用命令使用说明(一)

    本文由霸气的菠萝原创,转载请注明出处:http://www.cnblogs.com/xsln/p/gdb_instructions1.html 全部关于gdb的文章索引请点这里 GDB(GNU Deb ...

  10. 【spring mvc】springmvc在tomcat中的执行过程

    一.WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象(每个web应用程序唯一),它代表当前web应用web容器提供其一个全局的上下文环境,其为后面的spri ...