表1 foreign key 表2
则表1的多条记录对应表2的一条记录,即多对一 利用foreign key的原理我们可以制作两张表的多对多,一对一关系
多对多:
表1的多条记录可以对应表2的一条记录
表2的多条记录也可以对应表1的一条记录 一对一:
表1的一条记录唯一对应表2的一条记录,反之亦然 分析时,我们先从按照上面的基本原理去套,然后再翻译成真实的意义,就很好理解了

1、先确立关系

2、找到多的一方,吧关联字段写在多的一方

一、多对一或者一对多(左边表的多条记录对应右边表的唯一一条记录)

需要注意的:1.先建被关联的表,保证被关联表的字段必须唯一。

      2.在创建关联表,关联字段一定保证是要有重复的。

其实上一篇博客已经举了一个多对一关系的小例子了,那我们在用另一个小例子来回顾一下。

这是一个书和出版社的一个例子,书要关联出版社(多个书可以是一个出版社,一个出版社也可以有好多书)。

谁关联谁就是谁要按照谁的标准。

书要关联出版社
被关联的表
create table press(
id int primary key auto_increment,
name char(20)
);
关联的表
create table book(
book_id int primary key auto_increment,
book_name varchar(20),
book_price int,
press_id int,
constraint Fk_pressid_id foreign key(press_id) references press(id)
on delete cascade
on update cascade
); 插记录
insert into press(name) values('新华出版社'),
('海燕出版社'),
('摆渡出版社'),
('大众出版社');
insert into book(book_name,book_price,press_id) values('Python爬虫',100,1),
('Linux',80,1),
('操作系统',70,2),
('数学',50,2),
('英语',103,3),
('网页设计',22,3);

运行结果截图:

二、一对一

例子一:用户和管理员(只有管理员才可以登录,一个管理员对应一个用户)

管理员关联用户

===========例子一:用户表和管理员表=========
先建被关联的表
create table user(
id int primary key auto_increment, #主键自增
name char(10)
);
在建关联表
create table admin(
id int primary key auto_increment,
user_id int unique,
password varchar(16),
foreign key(user_id) references user(id)
on delete cascade
on update cascade
);
insert into user(name) values('susan1'),
('susan2'),
('susan3'),
('susan4'),
('susan5'),
('susan6');
insert into admin(user_id,password) values(4,'sds156'),
(2,'531561'),
(6,'f3swe');

运行结果截图:

例子二:学生表和客户表

========例子二:学生表和客户表=========
create table customer(
id int primary key auto_increment,
name varchar(10),
qq int unique,
phone int unique
);
create table student1(
sid int primary key auto_increment,
course char(20),
class_time time,
cid int unique,
foreign key(cid) references customer(id)
on delete cascade
on update cascade
);
insert into customer(name,qq,phone) values('小小',13564521,11111111),
('嘻哈',14758254,22222222),
('王维',44545522,33333333),
('胡军',545875212,4444444),
('李希',145578543,5555555),
('李迪',754254653,8888888),
('艾哈',74545145,8712547),
('啧啧',11147752,7777777);
insert into student1(course,class_time,cid) values('python','08:30:00',3),
('python','08:30:00',4),
('linux','08:30:00',1),
('linux','08:30:00',7);

运行结果截图:

三、多对多(多条记录对应多条记录)

书和作者(我们可以再创建一张表,用来存book和author两张表的关系)

要把book_id和author_id设置成联合唯一

联合唯一:unique(book_id,author_id)

联合主键:alter table t1 add primary  key(id,avg)

多对多:一个作者可以写多本书,一本书也可以有多个作者,双向的一对多,即多对多
  
关联方式:foreign key+一张新的表

========书和作者,另外在建一张表来存书和作者的关系
#被关联的
create table book1(
id int primary key auto_increment,
name varchar(10),
price float(3,2)
);
#========被关联的
create table author(
id int primary key auto_increment,
name char(5)
);
#========关联的
create table author2book(
id int primary key auto_increment,
book_id int not null,
author_id int not null,
unique(book_id,author_id),
foreign key(book_id) references book1(id)
on delete cascade
on update cascade,
foreign key(author_id) references author(id)
on delete cascade
on update cascade
);
#========插入记录
insert into book1(name,price) values('九阳神功',9.9),
('葵花宝典',9.5),
('辟邪剑谱',5),
('降龙十巴掌',7.3);
insert into author(name) values('egon'),('e1'),('e2'),('e3'),('e4');
insert into author2book(book_id,author_id) values(1,1),
(1,4),
(2,1),
(2,5),
(3,2),
(3,3),
(3,4),
(4,5);

多对多关系举例

用户表,用户组,主机表

-- 用户组
create table user (
id int primary key auto_increment,
username varchar(20) not null,
password varchar(50) not null
);
insert into user(username,password) values('egon','123'),
('root',147),
('alex',123),
('haiyan',123),
('yan',123); -- 用户组表
create table usergroup(
id int primary key auto_increment,
groupname varchar(20) not null unique
);
insert into usergroup(groupname) values('IT'),
('Sale'),
('Finance'),
('boss'); -- 建立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)
ON DELETE CASCADE
on UPDATE CASCADE ,
foreign key(group_id) references usergroup(id)
ON DELETE CASCADE
on UPDATE CASCADE
);

insert into user2usergroup(user_id,group_id) values(1,1),
(1,2),
(1,3),
(1,4),
(2,3),
(2,4),
(3,4);
-- 主机表
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
('轻松贷'),
('随便花'),
('大富翁'),
('穷一生'); -- 建立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);

练习

MySQL数据库----表与表之间的关系的更多相关文章

  1. 点评阿里JAVA手册之MySQL数据库 (建表规约、索引规约、SQL语句、ORM映射)

    下载原版阿里JAVA开发手册  [阿里巴巴Java开发手册v1.2.0] 本文主要是对照阿里开发手册,注释自己在工作中运用情况. 本文内容:MySQL数据库 (建表规约.索引规约.SQL语句.ORM映 ...

  2. php面试专题---mysql数据库分库分表

    php面试专题---mysql数据库分库分表 一.总结 一句话总结: 通过数据切分技术将一个大的MySQLServer切分成多个小的MySQLServer,既攻克了写入性能瓶颈问题,同一时候也再一次提 ...

  3. 利用navcat为mysql数据库单独的表赋权限及表结构同步

    为mysql数据库单独的表赋权限 场景:考勤系统需要拿OA数据库td_oa中的flow_run和flow_run_data表中的数据做考勤计算 考勤系统只需要读取这两张表的数据,所以只需要开通一个单独 ...

  4. MySQL数据库之单表查询中关键字的执行顺序

    目录 MySQL数据库之单表查询中关键字的执行顺序 1 语法顺序 2 执行顺序 3 关键字使用语法 MySQL数据库之单表查询中关键字的执行顺序 1 语法顺序 select distinct from ...

  5. 4.mysql数据库创建,表中创建模具模板脚本,mysql_SQL99标准连接查询(恩,外部连接,全外连接,交叉连接)

     mysql数据库创建,表创建模等模板脚本 -- 用root用户登录系统,运行脚本 -- 创建数据库 create database mydb61 character set utf8 ; -- ...

  6. mysql管理 ------查看 MySQL 数据库中每个表占用的空间大小

    如果想知道MySQL数据库中每个表占用的空间.表记录的行数的话,可以打开MySQL的 information_schema 数据库.在该库中有一个 TABLES 表,这个表主要字段分别是: TABLE ...

  7. freeswitch用户整合(使用mysql数据库的用户表)

    转:freeswitch用户整合(使用mysql数据库的用户表) freeswitch是一款强大的voip服务器,可以语音和视频.但是它默认是采用/directory文件夹下的xml来配置用户的,对于 ...

  8. MySQL数据库语法-多表查询练习一

    MySQL数据库语法-多表查询练习一 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客主要介绍的多表查询的外键约束,以及如何使用外链接和内连接查询数据信息. 一.数据表和测试 ...

  9. MySQL数据库查看数据表占用空间大小和记录数

    MySQL数据库中每个表占用的空间.表记录的行数的话,可以打开MySQL的 information_schema 数据库.在该库中有一个 TABLES 表,这个表主要字段分别是: TABLE_SCHE ...

  10. 先排序然后union all失效,mysql数据库多个表union all查询并排序的结果为什么错误

    mysql数据库多个表union all查询并排序的结果为什么错误? 群主,我想进行一个表的查询,先把表中某个字段的内容查出,然后其他的再排序,我用union all连接两个表的查询结果排序是错的 比 ...

随机推荐

  1. vs code 搭建flutter运行环境(mac)

    之前开发过hybrid app,用的是webview渲染,由于webview的体验会没有原生的体验好,所以对跨端原生开发燃起了学习的兴趣,在react-native和flutter之间纠结, 看了网上 ...

  2. iOS-相关集合类

    第一:NSArrary 1.1:集合的基本方法 1.创建集合   NSArray 是不可变数组,一旦创建完成就不能够对数组进行,添加,删除等操作 NSArray * array = [[NSArray ...

  3. cdn,wsgi框架

    CDN:分布式服务器 wsgi:http请求----wsgi----web框架

  4. <!DOCTYPE html>有什么用?

    <!DOCTYPE html> 是文档声明,必须放在文档第一行,它的作用就是告诉浏览器以哪个html版本来解析你的html代码.当你不写声明的时候,浏览器就会以自己的怪异模式来解析你的ht ...

  5. Hibernate错误

    1.Field 'id' doesn't have a default value 原来是我的数据设计的时候,把主键的类型定义为int的,原本想是用自增的方式来的,可是由于自己的粗心,写sql语句的时 ...

  6. 用SCSS需要小心IE对css的几个限制

    IE对CSS的限制主要有两个: 一个页面中引用的CSS只读前32个 一个CSS文件中只读前4095个选择器 关于这个问题的文章有很多,我就不细讲了. 我想讲的是在用SCSS写CSS的时候非常容易超过这 ...

  7. CentOS工作内容(四)主机禁ping

    CentOS工作内容(四)主机禁ping 用到的快捷键 tab 自动补齐(有不知道的吗) ctrl+a 移动到当前行的开头(a ahead) ctrl+u 删除(剪切)此处至开始所有内容 vim 末行 ...

  8. SeaJS 与 RequireJS 的差异对比

    这篇文章主要介绍了SeaJS 与 RequireJS 的差异对比,本文主要对CMD规范和AMD规范的弊端做了对比,并做出了一个总结,需要的朋友可以参考下 “历史不是过去,历史正在上演.随着 W3C 等 ...

  9. Chrome插件汇总

    Chrome浏览器可以增加很多插件,帮助我们更方便地使用. 1   重新定位新标签页 名字:Infinity.crx 官网:http://www.infinitynewtab.com/ 效果图如下: ...

  10. FPKM\RPKM\TPM学习[转载]

    转自:http://www.360doc.com/content/18/0112/02/50153987_721216719.shtml 1.问题提出 在RNA-Seq的分析中,对基因或转录本的rea ...