8-[表操作]--foreign key、表与表的关系
1、 foreign key
(1)快速理解foreign key
员工信息表有三个字段:工号 姓名 部门
公司有3个部门,但是有1个亿的员工,那意味着部门这个字段需要重复存储,部门名字越长,越浪费
解决方法:
我们完全可以定义一个部门表
然后让员工信息表关联该表,如何关联,即foreign key
(2)建立表关系
#表类型必须是innodb存储引擎,且被关联的字段,即references指定的另外一个表的字段,必须保证唯一 # 先建立被关联的表,并且保证被关联的字段唯一
create table dep(
id int primary key,
name char(16),
comment char(50)
); #dpt_id外键,关联父表(department主键id),同步更新,同步删除 # 再建立关联的表
create table emp(
id int primary key,
name char(10),
sex enum('male','female','other'),
dep_id int,
foreign key(dep_id) references dep(id) on delete cascade on update cascade
);
foreign key(dep_id) references dep(id) on delete cascade on update cascade
# 外键 dep_id, 引用 dep 表的id字段 删除,更新 级联
(2)插入数据
#先往被关联表插入记录
#先往父表department中插入记录 insert into dep values
(1,"IT","技术能力有限部门"),
(2,"销售","销售能力不足部门"),
(3,"财务","花钱特别多部门"); #再往关联表插入记录
#再往子表employee中插入记录
insert into emp values
(1,'egon','male',1); insert into emp values
(2,'alex','male',1),
(3,'wupeiqi','female',2),
(4,'yuanhao','male',3),
(5,'jinximn','male',2);
(4)删除更新
#删父表department,子表employee中对应的记录跟着删
delete from dep where id=3; #更新父表department,子表employee中对应的记录跟着改
mysql> update department set id=22222 where id=2;
2、表关系
两张表之间的关系:
# 1、多对一或一对多
一个出版社可以出版多本书
一夫多妻制:妻子表的丈夫id外键到丈夫表的id
关联方式:foreign key # 2、多对多
一个作者可以写多本书,一本书也可以有多个作者,双向的一对多,即多对多
关联方式:foreign key+一张新的表 # 3、一对一
customer表 student表
一对一:一个学生是一个客户,一个客户有可能变成一个学校,即一对一的关系
关联方式:foreign key+unique
(1)多对一: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) # 关联父表的主键id
on delete cascade # 同步更新,删除
on update cascade
);
#插入data
# 先插入父表的
insert into press(name) values
('北京工业地雷出版社'),
('人民音乐不好听出版社'),
('知识产权没有用出版社')
; # 在插入关联表的
insert into book(name,press_id) values
('九阳神功',1),
('九阴真经',2),
('九阴白骨爪',2),
('独孤九剑',3),
('降龙十巴掌',2),
('葵花宝典',3)
;
(2)多对多: 联合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');
#每个作者与自己的代表作如下
egon:
九阳神功
九阴真经
九阴白骨爪
独孤九剑
降龙十巴掌
葵花宝典
alex:
九阳神功
葵花宝典
yuanhao:
独孤九剑
降龙十巴掌
葵花宝典
wpq:
九阳神功
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)
;
(3)一对一:foreign key + unique
#一定是student来foreign key表customer,这样就保证了:
#1 学生一定是一个客户,
#2 客户不一定是学生,但有可能成为一个学生
# 客户表
create table customer(
id int primary key auto_increment,
name varchar(20) not null,
qq varchar(10) not null,
phone char(16) not null
); # 学生表
create table student(
id int primary key auto_increment,
class_name varchar(20) not null,
customer_id int unique, #该字段一定要是唯一的
foreign key(customer_id) references customer(id) #外键的字段一定要保证unique
on delete cascade
on update cascade
);
#增加客户
insert into customer(name,qq,phone) values
('李飞机','',13811341220),
('王大炮','',15213146809),
('守榴弹','',1867141331),
('吴坦克','',1851143312),
('赢火箭','',1861243314),
('战地雷','',18811431230)
; #增加学生
insert into student(class_name,customer_id) values
('脱产3班',3),
('周末19期',4),
('周末19期',5)
;
例一:一个用户只有一个博客 用户表:
id name
egon
alex
wupeiqi 博客表
fk+unique
id url name_id
xxxx 1
yyyy 3
zzz 2 例二:一个管理员唯一对应一个用户
用户表:
id user password
egon xxxx
alex yyyy 管理员表:
fk+unique
id user_id password
1 xxxxx
2 yyyyy 其他例子
8-[表操作]--foreign key、表与表的关系的更多相关文章
- python 存储引擎 mysql(库,表, 行) 单表多表操作 (foreign key) sql_mode pymysql模块讲解
##################总结############### mysql 常用数据类型 整型:tinyint int(42亿条左右) bigint 小数:float double dec ...
- 删除带外键的表【foreign key constraint fails】报错
title: 删除带外键的表[foreign key constraint fails]报错 date: 2018-08-02 21:59:06 tags: 数据库 --- 遥想当时正在学hibern ...
- 更改具有Foreign key约束的表
1.Foreign key 说明: foreign key(外键) 建立起了表与表之间的约束关系,让表与表之间的数据更具有完整性和关联性.设想,有两张表A.B,A表中保存了许多电脑制造商的信息,比如联 ...
- Mybatis高级:Mybatis注解开发单表操作,Mybatis注解开发多表操作,构建sql语句,综合案例学生管理系统使用接口注解方式优化
知识点梳理 课堂讲义 一.Mybatis注解开发单表操作 *** 1.1 MyBatis的常用注解 之前我们在Mapper映射文件中编写的sql语句已经各种配置,其实是比较麻烦的 而这几年来注解开发越 ...
- MySQL数据库:SQL语句基础、库操作、表操作、数据类型、约束条件、表之间的关系
数据库相关概念: 1. 数据库服务器:运行数据库管理软件的计算机 2. 数据库管理软件:MySQL.Oracle.db2.slqserver 3. 库:文件夹,用来组织文件/表 4. 表:文件(类似于 ...
- 报错:无法截断表 '某表',因为该表正由 FOREIGN KEY 约束引用
某表的某个字段作为另一个表的FOREIGN KEY,在truncate另外一个表后,再truncate某表,就报如上的错. 解决方法: → 删除另外一个表的外键 IF OBJECT_ID(N'[dbo ...
- [MySQL数据库之表的约束条件:primary key、auto_increment、not null与default、unique、foreign key:表与表之间建立关联]
[MySQL数据库之表的约束条件:primary key.auto_increment.not null与default.unique.foreign key:表与表之间建立关联] 表的约束条件 约束 ...
- SQL server基础知识(表操作、数据约束、多表链接查询)
SQL server基础知识 一.基础知识 (1).存储结构:数据库->表->数据 (2).管理数据库 增加:create database 数据库名称 删除:drop database ...
- MySQL之表操作
1 创建表 2 查看表结构 3 数据类型 4 表完整性约束 5 修改表 6 复制表 7 删除表 一创建表 语法: create table 表名( 字段名1 类型[(宽度) 约束条件], 字段名2 ...
随机推荐
- 小鸡G4工程款 上手体验
前言:之前只是抱着试一试的态度在小鸡活动贴下报名,说实话之前并没有抱希望能够没选中.所以非常感谢小鸡团队给我的这次机会.这应该是我第一次参与厂家的内测活动.希望能给小鸡团队,给广大玩家带来一片实用的上 ...
- css实现梯形
使用伪元素before和after分别在矩形元素前后加三角形或者直接设置border 使用3d旋转矩形,使之看起来像矩形 <html> <head> <meta char ...
- 解决标题过长的CSS
不知道为什么大家用截取字符串的人很多呢.. <html> <head> <style type="text/css"> .divout { di ...
- September 25th 2017 Week 39th Monday
No man is rich enough to buy back his own past. 没有人富有到可以赎回自己的过去. Those rich are not willing to buy b ...
- casperjs,phantomjs,slimerjs and spooky
1.casperjs http://casperjs.org/ CasperJS is a navigation scripting & testing utility for Phantom ...
- HttpClient的包含注意事项
HttpClient 功能介绍 以下列出的是 HttpClient 提供的主要的功能,要知道更多详细的功能可以参见 HttpClient 的主页. 实现了所有 HTTP 的方法(GET,POST,PU ...
- SOJ 4580 动态规划之01背包 (01背包)
Description Sidney想去Gandtom家玩.但Sidney家和Gandtom家之间是高低不平.坑坑洼洼的土路.所以他需要用他的背包装几袋稀的泥,在路上铺平一些干的土,使路变成平整的泥土 ...
- Sequelize-nodejs-9-Scopes
Scopes作用域 Scoping allows you to define commonly used queries that you can easily use later. Scopes c ...
- 关于jquery的click()方法
昨天,有个同事研究了以下jqury的click()方法,代码如下: <!DOCTYPE html> <html> <head> <meta charset=& ...
- Kafka设计解析(十六)Kafka 0.11消息设计
转载自 huxihx,原文链接 [原创]Kafka 0.11消息设计 目录 一.Kafka消息层次设计 1. v1格式 2. v2格式 二.v1消息格式 三.v2消息格式 四.测试对比 Kafka 0 ...