MySQL表与表之间的关系详解

外键说到表与表之间的关系就不得不说到一个关键词:外键MySQ中的外键是什么,和表与表之间有什么关联?通过示例说明:员工信息表有三个字段:工号姓名部门如何把他们相互联系起来呢??公司有3

大家好,又见面了,我是你们的朋友全栈君。

外键

说到表与表之间的关系就不得不说到一个关键词:外键

MySQ中的外键是什么,和表与表之间有什么关联?

外键(foreign  key)又叫外连接, 在数据库中发挥着重要的作用  尤其是对于表和表之间的关系尤为重要

通过示例说明:

员工信息表有三个字段:工号  姓名  部门      如何把他们相互联系起来呢??

公司有3个部门,但是有1个亿的员工,那意味着部门这个字段需要重复存储,部门名字越长,越浪费

解决方法:

  我们完全可以定义一个部门表,然后让员工信息表关联该表,如何关联,即foreign key

<span role="heading" aria-level="2">MySQL表与表之间的关系详解
<span role="heading" aria-level="2">MySQL表与表之间的关系详解

#表类型必须是innodb存储引擎,且被关联的字段,即references指定的另外一个表的字段,必须保证唯一
create table department(
id int primary key,
name varchar(20) not null
)engine=innodb;

#dpt_id外键,关联父表(department主键id),同步更新,同步删除
create table employee(
id int primary key,
name varchar(20) not null,
dpt_id int,
constraint fk_name foreign key(dpt_id)
references department(id)
on delete cascade
on update cascade 
)engine=innodb;


#先往父表department中插入记录
insert into department values
(1,'欧德博爱技术有限事业部'),
(2,'艾利克斯人力资源部'),
(3,'销售部');


#再往子表employee中插入记录
insert into employee values
(1,'egon',1),
(2,'alex1',2),
(3,'alex2',2),
(4,'alex3',2),
(5,'李坦克',3),
(6,'刘飞机',3),
(7,'张火箭',3),
(8,'林子弹',3),
(9,'加特林',3)
;


#删父表department,子表employee中对应的记录跟着删
mysql> delete from department where id=3;
mysql> select * from employee;
+----+-------+--------+
| id | name  | dpt_id |
+----+-------+--------+
|  1 | egon  |      1 |
|  2 | alex1 |      2 |
|  3 | alex2 |      2 |
|  4 | alex3 |      2 |
+----+-------+--------+


#更新父表department,子表employee中对应的记录跟着改
mysql> update department set id=22222 where id=2;
mysql> select * from employee;
+----+-------+--------+
| id | name  | dpt_id |
+----+-------+--------+
|  1 | egon  |      1 |
|  3 | alex2 |  22222 |
|  4 | alex3 |  22222 |
|  5 | alex1 |  22222 |
+----+-------+--------+

示例详解

通过上面的示例  我们可以发现:其实表和表之间是存在一定的关系的  那么  我们怎么找出表和表之间的关系呢??

<span role="heading" aria-level="2">MySQL表与表之间的关系详解
<span role="heading" aria-level="2">MySQL表与表之间的关系详解

分析步骤:
#1、先站在左表的角度去找
是否左表的多条记录可以对应右表的一条记录,如果是,则证明左表的一个字段foreign key 右表一个字段(通常是id)

#2、再站在右表的角度去找
是否右表的多条记录可以对应左表的一条记录,如果是,则证明右表的一个字段foreign key 左表一个字段(通常是id)

#3、总结:
#多对一:
如果只有步骤1成立,则是左表多对一右表
如果只有步骤2成立,则是右表多对一左表

#多对多
如果步骤1和2同时成立,则证明这两张表时一个双向的多对一,即多对多,需要定义一个这两张表的关系表来专门存放二者的关系

#一对一:
如果1和2都不成立,而是左表的一条记录唯一对应右表的一条记录,反之亦然。这种情况很简单,就是在左表foreign key右表的基础上,将左表的外键字段设置成unique即可

找出表和表之间的关系

 通过以上的方法可以找到表和表之间的 关系,既然找到了这种关系或者叫关联  我们就可以用表把他们之间的关联表现出来(即表与表之间的关系):

表和表之间的关系

一对多或者叫多对一

<span role="heading" aria-level="2">MySQL表与表之间的关系详解
<span role="heading" aria-level="2">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,
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)

示例素材及详解

 多对多

<span role="heading" aria-level="2">MySQL表与表之间的关系详解
<span role="heading" aria-level="2">MySQL表与表之间的关系详解

三张表:出版社,作者信息,书   实现相互关联

多对多:一个作者可以写多本书,一本书也可以有多个作者,双向的一对多,即多对多
  
关联方式:foreign key+一张新的表
=====================多对多=====================
create table author(
id int primary key auto_increment,
name varchar(20)
);


#这张表就存放作者表与书表的关系,即查询二者的关系查这表就可以了
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'),('yuanhao'),('wpq');

#每个作者与自己的代表作如下
1 egon: 
      1 九阳神功
      2 九阴真经
      3 九阴白骨爪
      4 独孤九剑
      5 降龙十巴掌
      6 葵花宝典


2 alex: 
      1 九阳神功
      6 葵花宝典

3 yuanhao:
      4 独孤九剑
      5 降龙十巴掌
      6 葵花宝典

4 wpq:
      1 九阳神功


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

示例

一对一

<span role="heading" aria-level="2">MySQL表与表之间的关系详解
<span role="heading" aria-level="2">MySQL表与表之间的关系详解

#一对一
两张表:学生表和客户表  实现相互关联

一对一:一个学生是一个客户,一个客户有可能变成一个学校,即一对一的关系

关联方式:foreign key+unique
#一定是student来foreign key表customer,这样就保证了:
#1 学生一定是一个客户,
#2 客户不一定是学生,但有可能成为一个学生


create table customer(
id int primary key auto_increment,
name varchar(20) not null
);


create table student(
id int primary key auto_increment,
name varchar(20) not null,
class_name varchar(20) not null default 'python自动化',
level int default 1,
customer_id int unique, #该字段一定要是唯一的
foreign key(customer_id) references customer(id) #外键的字段一定要保证unique
on delete cascade
on update cascade
);


#增加客户
insert into customer(name) values
('李飞机'),
('王大炮'),
('守榴弹'),
('吴坦克'),
('赢火箭'),
('战地雷')
;


#增加学生
insert into student(name,customer_id) values
('李飞机',1),
('王大炮',2)
;

示例素材及详解

相关练习题:

  账号信息表,用户组,主机表,主机组

<span role="heading" aria-level="2">MySQL表与表之间的关系详解
<span role="heading" aria-level="2">MySQL表与表之间的关系详解

#用户表
create table user(
id int not null unique auto_increment,
username varchar(20) not null,
password varchar(50) not null,
primary key(username,password)
);

insert into user(username,password) values
('root','123'),
('egon','456'),
('alex','alex3714')
;


#用户组表
create table usergroup(
id int primary key auto_increment,
groupname varchar(20) not null unique
);

insert into usergroup(groupname) values
('IT'),
('Sale'),
('Finance'),
('boss')
;


#主机表
create table host(
id int primary key auto_increment,
ip char(15) not null unique default '127.0.0.1'
);

insert into host(ip) values
('172.16.45.2'),
('172.16.31.10'),
('172.16.45.3'),
('172.16.31.11'),
('172.10.45.3'),
('172.10.45.4'),
('172.10.45.5'),
('192.168.1.20'),
('192.168.1.21'),
('192.168.1.22'),
('192.168.2.23'),
('192.168.2.223'),
('192.168.2.24'),
('192.168.3.22'),
('192.168.3.23'),
('192.168.3.24')
;


#业务线表
create table business(
id int primary key auto_increment,
business varchar(20) not null unique
);
insert into business(business) values
('轻松贷'),
('随便花'),
('大富翁'),
('穷一生')
;


#建关系:user与usergroup

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),
foreign key(group_id) references usergroup(id)
);

insert into user2usergroup(user_id,group_id) values
(1,1),
(1,2),
(1,3),
(1,4),
(2,3),
(2,4),
(3,4)
;



#建关系:host与business

create table host2business(
id int not null unique auto_increment,
host_id int not null,
business_id int not null,
primary key(host_id,business_id),
foreign key(host_id) references host(id),
foreign key(business_id) references business(id)
);

insert into host2business(host_id,business_id) values
(1,1),
(1,2),
(1,3),
(2,2),
(2,3),
(3,4)
;

#建关系:user与host

create table user2host(
id int not null unique auto_increment,
user_id int not null,
host_id int not null,
primary key(user_id,host_id),
foreign key(user_id) references user(id),
foreign key(host_id) references host(id)
);

insert into user2host(user_id,host_id) values
(1,1),
(1,2),
(1,3),
(1,4),
(1,5),
(1,6),
(1,7),
(1,8),
(1,9),
(1,10),
(1,11),
(1,12),
(1,13),
(1,14),
(1,15),
(1,16),
(2,2),
(2,3),
(2,4),
(2,5),
(3,10),
(3,11),
(3,12)
;

View Code

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/124515.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • latex字母斜体加粗_latex加粗字体

    latex字母斜体加粗_latex加粗字体显示直立文本: \textup{文本}意大利斜体: \textit{文本}slanted斜体: \textsl{文本}显示小体大写文本:\textsc{文本}中等权重:

    2022年8月3日
    13
  • 1s看懂555定时器,以及应用?

    1s看懂555定时器,以及应用?555定时器是美国Signetics公司1972年研制的用于取代机械式定时器的中规模集成电路,因输入端设计有三个5kΩ的电阻而得名。此电路后来竟风靡世界。目前,流行的产品主要有4个:BJT两个:555,556(含有两个555);CMOS两个:7555,7556(含有两个7555)。555定时器是一种模拟和数字功能相结合的中规模集成器件。一般用双极型(TTL)工艺…

    2022年6月23日
    37
  • linux查看ftp用户列表_linux ftp下载命令

    linux查看ftp用户列表_linux ftp下载命令ftp服务器在网上较为常见,Linuxftp命令的功能是用命令的方式来控制在本地机和远程机之间传送文。下面由学习啦小编为大家整理了linux下查询ftp命令的相关知识,希望对大家有所帮助。Linuxftp命令的格式$ftp主机名/IP其中“主机名/IP”是所要连接的远程机的主机名或IP地址。在命令行中,主机名属于选项,如果指定主机名,ftp将试图与远程机的ftp服务程序进行连接;如果没有指…

    2022年9月21日
    3
  • 谈谈CompoundButton的OnCheckedChangeListener

    谈谈CompoundButton的OnCheckedChangeListenerCompoundButton相信大家都很熟悉了。OnCheckedChangeListener相信大家也很熟悉。不过不知道大家有没有碰到类似的问题:在某个CompoundButton.OnCheckedChangeListener中请求网络接口。在其他一些地方,例如onCreate/onCreateView/setUserVisableHint等等方法中,从另一个接口取得数据,

    2022年5月2日
    69
  • javacollection_java freemarker

    javacollection_java freemarkerlabelmeImagePolygonalAnnotationwithPythonDescriptionLabelmeisagraphicalimageannotationtoolinspiredbyhttp://labelme.csail.mit.edu.ItiswritteninPythonandusesQtforitsgraphica…

    2025年10月30日
    2
  • Windows 10 多出多个虚拟显示器的解决方法「建议收藏」

    Windows 10 多出多个虚拟显示器的解决方法「建议收藏」昨天更新Windows101809之后,系统中多出了4个“通用非即插即用监视器”,由于本人长期是笔记本外接显示器使用,出现这个问题后,鼠标会经常性的跑到另外的屏幕上去,甚至一些窗口会跑到其他页面上去。到NVIDIA控制面板查看之后发现多出的显示器是在核心显卡上,所以怀疑是核显的问题。其实之前也出现过类似问题,Google之后发现有人也有类似问题。尝试在任务管理器中卸载多出…

    2022年8月21日
    16

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号