当前位置:网站首页>多表操作-一对一,一对多与多对多
多表操作-一对一,一对多与多对多
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);
边栏推荐
- from pip._ internal. cli. main import main ModuleNotFoundError: No module named ‘pip‘
- Future trend and development of neural network Internet of things
- [swoole Series 1] what will you learn in the world of swoole?
- 使用VB.net将PNG图片转成icon类型图标文件
- 小程序表单校验封装
- Commemorate becoming the first dayus200 tripartite demo contributor
- Use vb Net to convert PNG pictures into icon type icon files
- Write some suggestions to current and future doctoral students to sort out and share
- Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
- openwrt 开启KV漫游
猜你喜欢
随机推荐
Postgresql随手记(10)动态执行EXECUTING语法解析过程
PostgreSQL source code (58) tuple splicing heap_ form_ Tuple analysis
What category does the Internet of things application technology major belong to
ARP message header format and request flow
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
ARP报文头部格式和请求流程
BlocProvider为什么感觉和Provider很相似?
Openwrt enable kV roaming
Daily three questions 6.30 (2)
Matplotlib常用設置
图的遍历之深度优先搜索和广度优先搜索
What professional classification does the application of Internet of things technology belong to
2021 robocom world robot developer competition - semi finals of higher vocational group
golang中的iota
SQL optimization
【必会】BM41 输出二叉树的右视图【中等+】
安全协议重点
Behind sharing e-commerce: the spirit of CO creation, symbiosis, sharing, CO prosperity and win-win
Redis RDB快照
The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem


















