mysql数据库和表的关系_MySQL数据库学习【第六篇】表与表之间的关系
表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数据库和表的关系_MySQL数据库学习【第六篇】表与表之间的关系相关推荐
- JavaScript学习(六十八)—表单校验案例
JavaScript学习(六十八)-表单校验案例 学习内容 (一).如何获取页面的元素-利用id获取 格式:var 变量名称 =document.getElementById('要获取的元素的id的值 ...
- 学习Scala:伴生对象和伴生类之间的关系(二)
孤立对象是只有一个object关键字修饰的对象,该对象会编译成两个class文件,一个是以孤立对象的名字命名的class, 一个是以孤立对象的名字后面加上一个$ 字符命名的class, 这个class ...
- navicat 8 mysql生成关系_MySQL数据库通过navicat建立多对多关系
** 构建两张表的多对多关系:清洗数据表(clean_data表)与用户表(user表)建立多对多的关系.如下图图0所示 图0 多对多之间关系 1 创建表 .打开navicat,创建三张表,clean ...
- mysql多字段分库分表基因码_mysql数据库优化方案之分库分表,轻松解决亿级数据问题...
今天介绍下sharding-sphere,主要介绍他的特性,分库分表的技术的详解. (一)下载源码官网地址获取源码https://shardingsphere.apache.org/index_zh. ...
- mysql多表操作语句_MYSQL数据库语句之多表操作(三)
多表操作 join查询(join按照功能分可以分为3类): cross join(交叉连接):获取多个表中的交叉关系. inner join(内连接):获取2个表中字段匹配关系的记录. 外连接 lef ...
- mysql映射关系_mysql数据库对象关系映射_MySQL
bitsCN.com mysql数据库对象关系映射 1.对"对象关系映射"的理解 a.对象:可以理解为java中的类 b.关系:可以理解为数据库的表 c.映射:这是因为java中的 ...
- mysql数据库表类型设置_mysql数据库表的类型介绍
目录 前言 之前我们讲了下载安装数据库,还有如何卸载(虽然直接重装系统就好) 那么现在让我们来讲讲 """ 1.数据库与表的剩余操作 编码配置.引擎介绍 2.数据库字段的 ...
- mysql数据意外删了怎么办_MySQL数据库意外崩溃导致表数据文件损坏无法启动怎么办...
MySQL数据库意外崩溃导致表数据文件损坏无法启动怎么办 发布时间:2020-07-20 13:45:46 来源:亿速云 阅读:57 作者:小猪 这篇文章主要为大家展示了MySQL数据库意外崩溃导致表 ...
- mysql数据库表复制备份_mysql数据库的备份以及表格数据之间的复制
#####-------------mysql数据备份以及表间数据的复制-------------------##### ##----------------我的mysql学习(二)--------- ...
- mysql授予权限和撤销权限的关系_MySQL数据库常用的授予权限和撤销权限的命令讲解...
MySQL 赋予用户权限命令的简单格式可概括为: 一.grant 普通数据用户,查询.插入.更新.删除 数据库中所有表数据的权利 1 2 3 4grant select on testdb.* to ...
最新文章
- php基础+jquery基础
- gitlab安装_Gitlab安装和配置教程(包括邮箱配置)
- make的使用和Makefile规则和编程及其基本命令(简单)
- 得到的概率值_论文推荐|屋面钢梁在超强意外雪荷载作用下的失效概率
- php能做的事,PHP也能干大事 随机函数
- QT 之 TCP/IP 服务器和客户端(一)
- linux let s证书续期,BT(宝塔面板)Let’s Encrypt证书续签方法
- matlab中if可以判断或语句吗,matlab中if 语句后面的判别式不能是算术表达式?或者说变量?...
- index.php上传到服务器找不到站点,ThinkPHP上传到服务器出现404,未找到index.php模板等问题...
- python查函数用法语句_Python-17 (函数的基本使用)
- tomcat体系结构
- 人大经济论坛SAS入门到高级教程
- oracle 通过同义词创建视图
- 计算机网络简单理解总结
- 基于QT的推箱子小游戏设计
- Flutter 功能最全的JsonToDart工具
- 利用计算思维解决问题人和计算机都能完成,对计算思维能力养成与大学计算机基础课程改革的思考...
- 万向节死锁的理解与CS摄像机减少死锁的简单实现
- 阿里云大数据分析师ACP认证 视频教程
- 东北师范大学计算机学院晓辉,东北师范大学计算机科学与信息技术学院研究生导师简介-韩文峰...