当前位置:网站首页>多表操作-一对一,一对多与多对多
多表操作-一对一,一对多与多对多
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);
边栏推荐
- excel如何打开100万行以上的csv文件
- 门级建模—课后习题
- vs2015 AdminDeployment.xml
- TS initial use, TS type
- Algolia's search needs are almost closed
- Chapter 6 data flow modeling
- How excel opens CSV files with more than one million lines
- Postgresql源码(58)元组拼接heap_form_tuple剖析
- Redis master-slave synchronization
- Using uni simple router, dynamically pass parameters typeerror: cannot convert undefined or null to object
猜你喜欢
随机推荐
The essence of software architecture
Depth first search and breadth first search of graph traversal
Zero foundation tutorial of Internet of things development
PyTorch学习记录
Redis AOF log
Similarities and differences between the defined identity execution function authid determiner and PostgreSQL in Oracle
2022年最佳智能家居开源系统:Alexa、Home Assistant、HomeKit生态系统介绍
Openwrt enable kV roaming
Future trend and development of neural network Internet of things
Linux foundation - centos7 offline installation of MySQL
SecurityUtils. getSubject(). How to solve the problem that getprincipal() is null
在代码中使用SqlCommand对象
algolia 搜索需求,做的快自闭了...
第六章 数据流建模
小程序表单校验封装
为什么PHP叫超文本预处理器
Reproduction process and problems of analog transformer (ICLR 2022 Spotlight)
字典、哈希表、数组的概念
Daily three questions 6.30 (2)
vs2015 AdminDeployment.xml


















