当前位置:网站首页>多表操作-一对一,一对多与多对多
多表操作-一对一,一对多与多对多
2022-07-01 23:41:00 【汤键.TJ】
目录
一对一
多表介绍
- 多表概念
- 说白了就是多张数据表,而表与表之间是可以有一定的关联关系,这种关联关系通过外键约束实现
- 多表的分类
- 一对一
- 一对多
- 多对多
一对一
- 适用场景
- 人和身份证;
- 一个人只有一个身份证,一个身份证只能对应一个人
- 建表原则
- 在任意一个表建立外键,去关联另外一个表的主键
实例操作
-- 创建person表 CREATE TABLE person( id INT PRIMARY KEY auto_increment, -- 主键id name VARCHAR(20) -- 姓名 ); -- 添加数据 INSERT INTO person VALUES (NULL,'张三'),(NULL,'李四'); -- 创建card表 CREATE TABLE card( id INT PRIMARY KEY auto_increment, -- 主键id number VARCHAR(20) UNIQUE NOT NULL, -- 身份证号 pid INT UNIQUE, -- 外键列 CONSTRAINT ou FOREIGN KEY (pid) REFERENCES person(id) ); -- 添加数据 INSERT INTO card VALUES (NULL,'12345',1),(NULL,'56789',2);
一对多
介绍
- 适用场景
- 如:
- 用户和订单;
- 一个用户可以有多个订单
- 商品分类和商品;
- 一个分类下可以有多个商品
- 建表原则
- 在多的一方,建立外键约束
- 来关联一的一方主键
实例操作
- 用户对订单
-- 创建user表 CREATE TABLE user( id INT PRIMARY KEY auto_increment, -- 主键id name VARCHAR(20) -- 姓名 ); -- 添加数据 INSERT INTO user VALUES (NULL,'张三'),(NULL,'李四'); -- 创建orderlist表 CREATE TABLE orderlist( id INT PRIMARY KEY auto_increment, -- 主键id number VARCHAR(20), -- 订单编号 uid INT, -- 外键列 CONSTRAINT ou FOREIGN KEY (uid) REFERENCES user(id) ); -- 添加数据 INSERT INTO orderlist VALUES (NULL,'hm001',1),(NULL,'hm002',1),(NULL,'hm003',2),(NULL,'hm004',2);
- 商品分类对商品
-- 创建category表 CREATE TABLE category( id INT PRIMARY KEY auto_increment, -- 主键id name VARCHAR(20) -- 分类名称 ); -- 添加数据 INSERT INTO category VALUES (NULL,'手机数码'),(NULL,'电脑办公'); -- 创建product表 CREATE TABLE product( id INT PRIMARY KEY auto_increment, -- 主键id number VARCHAR(30), -- 商品名称 uid INT, -- 外键列 CONSTRAINT ou FOREIGN KEY (uid) REFERENCES category(id) ); -- 添加数据 INSERT INTO product VALUES (NULL,'Sony xperia 1',1),(NULL,'Google pixel 6 pro',1),(NULL,'ROG 魔霸',2),(NULL,'MacBook pro',2);
多对多
介绍
- 适用场景
- 如:
- 学生和课程
- 一个学生可以选择多个课程
- 一个课程也可以被多个学生选择
- 建表原则
- 需要借助第三张中间表,中间表至少包含两个列
- 这两个列作为中间表的外键,分别关联另外两张表的主键
实操演示
-- 创建student类 CREATE TABLE student( id INT PRIMARY KEY auto_increment, -- 主键id name VARCHAR(20) -- 学生姓名 ); -- 添加数据 INSERT INTO student VALUES (NULL,'张三'),(NULL,'李四'); -- 创建course表 CREATE TABLE course( id INT PRIMARY KEY auto_increment, -- 主键id name VARCHAR(20) -- 课程名称 ); -- 添加数据 INSERT INTO course VALUES (NULL,'语文'),(NULL,'数学'); -- 创建中间表 CREATE TABLE stu_course( id INT PRIMARY KEY auto_increment, -- 主键id sid INT, -- 用于和student表中的id进行外键约束 cid INT, -- 用于和course表中的id进行外键约束 CONSTRAINT sc_1 FOREIGN KEY (sid) REFERENCES student(id), -- 添加外键约束 CONSTRAINT sc_2 FOREIGN KEY (cid) REFERENCES course(id) -- 添加外键约束 ); -- 添加数据 INSERT INTO stu_course VALUES (NULL,1,1),(NULL,1,2),(NULL,2,1),(NULL,2,2);
边栏推荐
- TS initial use, TS type
- 股票开户哪个证券公司最好,有安全保障吗
- Concepts of dictionary, hash table and array
- PyCharm调用matplotlib绘图时图像弹出问题怎么解决
- How excel opens CSV files with more than one million lines
- 2021 robocom world robot developer competition - preliminary competition of undergraduate group
- 2022-07-01: at the annual meeting of a company, everyone is going to play a game of giving bonuses. There are a total of N employees. Each employee has construction points and trouble points. They nee
- 物联网技术应用属于什么专业分类
- The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem
- 华为HMS Core携手超图为三维GIS注入新动能
猜你喜欢
Redis master-slave synchronization
2021 robocom world robot developer competition - preliminary competition of higher vocational group
ARP报文头部格式和请求流程
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
Use vb Net to convert PNG pictures into icon type icon files
Kubernetes resource object introduction and common commands (III)
神经网络物联网的发展趋势和未来方向
门级建模—课后习题
[es practice] safe operation mode on ES
BlocProvider为什么感觉和Provider很相似?
随机推荐
Key points and difficulties of the course "information content security" at Harbin Institute of Technology
PyCharm调用matplotlib绘图时图像弹出问题怎么解决
Matplotlib common settings
What category does the Internet of things application technology major belong to
2021 RoboCom 世界机器人开发者大赛-高职组初赛
Door level modeling - after class exercises
.env.xxx 文件,加了常量,却undefined
[untitled]
股票开户哪个证券公司最好,有安全保障吗
Kubernetes resource object introduction and common commands (III)
notBlank 和 notEmpty
const // It is a const object...class nullptr_t
Linux基础 —— CentOS7 离线安装 MySQL
What is the difference between memory leak and memory overflow?
Redis 主从同步
Notblank and notempty
图的遍历之深度优先搜索和广度优先搜索
from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
Postgresql源码(57)HOT更新为什么性能差距那么大?
认识--Matplotlib