当前位置:网站首页>多表操作-一对一,一对多与多对多
多表操作-一对一,一对多与多对多
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);
边栏推荐
- 问题随记 —— /usr/bin/perl is needed by MySQL-server-5.1.73-1.glibc23.x86_64
- Windows 7 安装MYSQL 错误:1067
- 图的遍历之深度优先搜索和广度优先搜索
- SecurityUtils. getSubject(). How to solve the problem that getprincipal() is null
- 距离度量 —— 汉明距离(Hamming Distance)
- Leetcode (34) -- find the first and last positions of elements in a sorted array
- algolia 搜索需求,做的快自闭了...
- Postgresql源码(57)HOT更新为什么性能差距那么大?
- 物联网现状及未来发展趋势
- 写给当前及未来博士研究生一些建议整理分享
猜你喜欢
随机推荐
Redis AOF log
Applet form verification encapsulation
在代码中使用SqlCommand对象
Leetcode (34) -- find the first and last positions of elements in a sorted array
Y53. Chapter III kubernetes from introduction to mastery -- ingress (26)
Paramètres communs de matplotlib
Which securities company is the best to open a stock account? Is there a security guarantee
from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
物联网现状及未来发展趋势
RPA教程01:EXCEL自动化从入门到实操
PyCharm调用matplotlib绘图时图像弹出问题怎么解决
algolia 搜索需求,做的快自闭了...
Switch to software testing, knowing these four points is enough!
学成在线案例实战
Oracle中已定义者身份执行函数AUTHID DEFINER与Postgresql行为的异同
Commemorate becoming the first dayus200 tripartite demo contributor
BlocProvider为什么感觉和Provider很相似?
- Oui. Env. Fichier XXX, avec constante, mais non spécifié
Linux foundation - centos7 offline installation of MySQL
Redis RDB snapshot


















