博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MySQL外键的三种关系
阅读量:7169 次
发布时间:2019-06-29

本文共 3235 字,大约阅读时间需要 10 分钟。

一对多

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);# 先往被关联表中插入记录insert into press(name) values('北京工业地雷出版社'),('人民音乐不好听出版社'),('知识产权没有用出版社');# 再往关联表中插入记录insert into book(name,press_id) values('九阳神功',1),('九阴真经',2),('九阴白骨爪',2),('独孤九剑',3),('降龙十巴掌',2),('葵花宝典',3);查询结果: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('qwe'),('asd'),('zxc'),('wer');# 每个作者的代表作qwe: 九阳神功、九阴真经、九阴白骨爪、独孤九剑、降龙十巴掌、葵花宝典asd: 九阳神功、葵花宝典zxc:独孤九剑、降龙十巴掌、葵花宝典wer:九阳神功# 在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);# 现在就可以查author2book对应的作者和书的关系了mysql> select * from author2book;+----+-----------+---------+| id | author_id | book_id |+----+-----------+---------+|  1 |         1 |       1 ||  2 |         1 |       2 ||  3 |         1 |       3 ||  4 |         1 |       4 ||  5 |         1 |       5 ||  6 |         1 |       6 ||  7 |         2 |       1 ||  8 |         2 |       6 ||  9 |         3 |       4 || 10 |         3 |       5 || 11 |         3 |       6 || 12 |         4 |       1 |+----+-----------+---------+rows in set (0.00 sec)

 

一对一

#例如: 一个用户只能注册一个博客#两张表: 用户表 (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('qwe'),('asd'),('zxc'),('ert');# 插入博客表的记录insert into blog(url,user_id) values('http://www.cnblog/qwe',1),('http://www.cnblog/asd',2),('http://www.cnblog/zxc',3),('http://www.cnblog/ert',4);# 查询wusir的博客地址select url from blog where user_id=2;

 

转载于:https://www.cnblogs.com/NachoLau/p/10398519.html

你可能感兴趣的文章
设计模式基础(二):目标与原则
查看>>
程序员的自我修养(转)
查看>>
小组项目简介(更改后)
查看>>
oo第一次博客
查看>>
maven3简单配置和使用
查看>>
广工大专用教学质量评价脚本
查看>>
不同的 SQL JOIN
查看>>
SQL Date函数
查看>>
牛客小白月赛4 B 博弈论 思维 字符串
查看>>
一个用bootstrap写的小页面
查看>>
钉钉服务端api接口使用
查看>>
POJ 1045:Bode Plot
查看>>
java基础总结之Hashtable
查看>>
c++11 记录
查看>>
自定义简单的模板引擎-JS模板引擎
查看>>
c#事件机制
查看>>
JS 得细心的坑位
查看>>
【转】基于laravel制作APP接口(API)
查看>>
C#GridViewExport帮助类,美化导出
查看>>
今日工作情况3
查看>>