系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 数据库 > Mysql > 详细页面

mysql外键的三种关系实例详解

时间:2020-01-03来源:系统城作者:电脑系统城

本文实例讲述了mysql外键的三种关系。分享给大家供大家参考,具体如下:

因为有foreign key的约束,使得两张表形成了三种了关系:

  • 多对一
  • 多对多
  • 一对一

一对多或多对一

多对一


 
  1. create table press(
  2. id int primary key auto_increment,
  3. name varchar(20)
  4. );
  5. create table book(
  6. id int primary key auto_increment,
  7. name varchar(20),
  8. press_id int not null,
  9. constraint fk_book_press foreign key(press_id) references press(id)
  10. on delete cascade
  11. on update cascade
  12. );
  13.  

 
  1. # 先往被关联表中插入记录
  2. insert into press(name) values
  3. ('北京工业地雷出版社'),
  4. ('人民音乐不好听出版社'),
  5. ('知识产权没有用出版社')
  6. ;
  7. # 再往关联表中插入记录
  8. insert into book(name,press_id) values
  9. ('九阳神功',1),
  10. ('九阴真经',2),
  11. ('九阴白骨爪',2),
  12. ('独孤九剑',3),
  13. ('降龙十巴掌',2),
  14. ('葵花宝典',3)
  15. ;
  16.  

查询结果:


 
  1. mysql> select * from book;
  2. +----+-----------------+----------+
  3. | id | name | press_id |
  4. +----+-----------------+----------+
  5. | 1 | 九阳神功 | 1 |
  6. | 2 | 九阴真经 | 2 |
  7. | 3 | 九阴白骨爪 | 2 |
  8. | 4 | 独孤九剑 | 3 |
  9. | 5 | 降龙十巴掌 | 2 |
  10. | 6 | 葵花宝典 | 3 |
  11. +----+-----------------+----------+
  12. rows in set (0.00 sec)
  13. mysql> select * from press;
  14. +----+--------------------------------+
  15. | id | name |
  16. +----+--------------------------------+
  17. | 1 | 北京工业地雷出版社 |
  18. | 2 | 人民音乐不好听出版社 |
  19. | 3 | 知识产权没有用出版社 |
  20. +----+--------------------------------+
  21. rows in set (0.00 sec)
  22.  

多对多,引入第三张表

多对多


 
  1. # 创建被关联表author表,之前的book表在讲多对一的关系已创建
  2. create table author(
  3. id int primary key auto_increment,
  4. name varchar(20)
  5. );
  6. #这张表就存放了author表和book表的关系,即查询二者的关系查这表就可以了
  7. create table author2book(
  8. id int not null unique auto_increment,
  9. author_id int not null,
  10. book_id int not null,
  11. constraint fk_author foreign key(author_id) references author(id)
  12. on delete cascade
  13. on update cascade,
  14. constraint fk_book foreign key(book_id) references book(id)
  15. on delete cascade
  16. on update cascade,
  17. primary key(author_id,book_id)
  18. );
  19. #插入四个作者,id依次排开
  20. insert into author(name) values('egon'),('alex'),('wusir'),('yuanhao');
  21. # 每个作者的代表作
  22. egon: 九阳神功、九阴真经、九阴白骨爪、独孤九剑、降龙十巴掌、葵花宝典
  23. alex: 九阳神功、葵花宝典
  24. wusir:独孤九剑、降龙十巴掌、葵花宝典
  25. yuanhao:九阳神功
  26. # 在author2book表中插入相应的数据
  27. insert into author2book(author_id,book_id) values
  28. (1,1),
  29. (1,2),
  30. (1,3),
  31. (1,4),
  32. (1,5),
  33. (1,6),
  34. (2,1),
  35. (2,6),
  36. (3,4),
  37. (3,5),
  38. (3,6),
  39. (4,1)
  40. ;
  41.  

 
  1. # 现在就可以查author2book对应的作者和书的关系了
  2. mysql> select * from author2book;
  3. +----+-----------+---------+
  4. | id | author_id | book_id |
  5. +----+-----------+---------+
  6. | 1 | 1 | 1 |
  7. | 2 | 1 | 2 |
  8. | 3 | 1 | 3 |
  9. | 4 | 1 | 4 |
  10. | 5 | 1 | 5 |
  11. | 6 | 1 | 6 |
  12. | 7 | 2 | 1 |
  13. | 8 | 2 | 6 |
  14. | 9 | 3 | 4 |
  15. | 10 | 3 | 5 |
  16. | 11 | 3 | 6 |
  17. | 12 | 4 | 1 |
  18. +----+-----------+---------+
  19. rows in set (0.00 sec)
  20.  

一对一的情况

一对一


 
  1. #例如: 一个用户只能注册一个博客
  2. #两张表: 用户表 (user)和 博客表(blog)
  3. # 创建用户表
  4. create table user(
  5. id int primary key auto_increment,
  6. name varchar(20)
  7. );
  8. # 创建博客表
  9. create table blog(
  10. id int primary key auto_increment,
  11. url varchar(100),
  12. user_id int unique,
  13. constraint fk_user foreign key(user_id) references user(id)
  14. on delete cascade
  15. on update cascade
  16. );
  17. #插入用户表中的记录
  18. insert into user(name) values
  19. ('alex'),
  20. ('wusir'),
  21. ('egon'),
  22. ('xiaoma')
  23. ;
  24. # 插入博客表的记录
  25. insert into blog(url,user_id) values
  26. ('http://www.cnblog/alex',1),
  27. ('http://www.cnblog/wusir',2),
  28. ('http://www.cnblog/egon',3),
  29. ('http://www.cnblog/xiaoma',4)
  30. ;
  31. # 查询wusir的博客地址
  32. select url from blog where user_id=2;
  33.  

更多关于MySQL相关内容感兴趣的读者可查看本站专题:《MySQL查询技巧大全》、《MySQL常用函数大汇总》、《MySQL日志操作技巧大全》、《MySQL事务操作技巧汇总》、《MySQL存储过程技巧大全》及《MySQL数据库锁相关技巧汇总》

希望本文所述对大家MySQL数据库计有所帮助。

分享到:

相关信息

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载