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

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

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

一对多或多对一

多对一

create table press(  id int primary key auto_increment,  name varchar(20));create table book(  id int primary key auto_increment,  name varchar(20),  press_id int not null,     constraint fk_book_press foreign key(press_id) references press(id)  on delete cascade  on update cascade);
mysql> select * from book;+----+-----------------+----------+| id | name      | press_id |+----+-----------------+----------+| 1 | 九阳神功    |    1 || 2 | 九阴真经    |    2 || 3 | 九阴白骨爪   |    2 || 4 | 独孤九剑    |    3 || 5 | 降龙十巴掌   |    2 || 6 | 葵花宝典    |    3 |+----+-----------------+----------+rows in set (0.00 sec)mysql> select * from press;+----+--------------------------------+| id | name              |+----+--------------------------------+| 1 | 北京工业地雷出版社       || 2 | 人民音乐不好听出版社      || 3 | 知识产权没有用出版社      |+----+--------------------------------+rows in set (0.00 sec)

多对多

# 创建被关联表author表,之前的book表在讲多对一的关系已创建create table author(  id int primary key auto_increment,  name varchar(20));#这张表就存放了author表和book表的关系,即查询二者的关系查这表就可以了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'),('wusir'),('yuanhao');# 每个作者的代表作egon: 九阳神功、九阴真经、九阴白骨爪、独孤九剑、降龙十巴掌、葵花宝典alex: 九阳神功、葵花宝典wusir:独孤九剑、降龙十巴掌、葵花宝典yuanhao:九阳神功# 在author2book表中插入相应的数据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);

一对一

#例如: 一个用户只能注册一个博客#两张表: 用户表 (user)和 博客表(blog)# 创建用户表create table user(  id int primary key auto_increment,  name varchar(20));# 创建博客表create table blog(  id int primary key auto_increment,  url varchar(100),  user_id int unique,  constraint fk_user foreign key(user_id) references user(id)  on delete cascade  on update cascade);#插入用户表中的记录insert into user(name) values('alex'),('wusir'),('egon'),('xiaoma');# 插入博客表的记录insert into blog(url,user_id) values('http://www.cnblog/alex',1),('http://www.cnblog/wusir',2),('http://www.cnblog/egon',3),('http://www.cnblog/xiaoma',4);# 查询wusir的博客地址select url from blog where user_id=2;

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

更多相关文章

  1. MySQL系列多表连接查询92及99语法示例详解教程
  2. Android(安卓)- Manifest 文件 详解
  3. Android的Handler机制详解3_Looper.looper()不会卡死主线程
  4. Selector、shape详解(一)
  5. 【安卓笔记】android客户端与服务端交互的三种方式
  6. android2.2资源文件详解4--menu文件夹下的菜单定义
  7. Android发送短信方法实例详解
  8. Android(安卓)读取资源文件实例详解
  9. 详解Android中的屏幕方向

随机推荐

  1. 谈谈 View 绘制流程
  2. android MTP 过滤指定的文件格式
  3. Android SDK4.0 离线安装方法
  4. 查看自己Android设备分辨率
  5. android UI小知识点
  6. Android BufferQueue原理分析
  7. 缓存
  8. 分享20个Android游戏源码,希望大家喜欢哈!
  9. android显示PNG图片背景黑色问题 ImageVi
  10. Android(安卓)APP跳转微信小程序和APP跳