mysql外键的关系有哪些

40次阅读
没有评论

共计 2567 个字符,预计需要花费 7 分钟才能阅读完成。

mysql 外键的关系有哪些,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面丸趣 TV 小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

多对一

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(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)

;

# 现在就可以查 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

(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;

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注丸趣 TV 行业资讯频道,感谢您对丸趣 TV 的支持。

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-03发表,共计2567字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)