MySQL 创建多对多和一对一关系方法
作者:江下下啊 时间:2024-01-29 07:52:37
一、创建多对多
1.学生表
create table students (
id int not null primary key auto_increment,
name varchar(45) not null
)engine=innodb default charset=utf8;
2.课程表
create table courses (
id int not null primary key auto_increment,
name varchar(45) not null
)engine=innodb default charset=utf8;
3.中间表
create table stu_cour (
id int not null primary key auto_increment
course_id int not null,
stu_id int not null,
constraint cour foreign key(course_id) references courses(id),
constraint stu foreign key(stu_id) references students(id)
)engine=innodb default charset=utf8;
4.插入数据
insert into students values (0,"小王");
insert into students values (0,"小宋");
insert into students values (0,"小李");
insert into courses values (0,"语文");
insert into courses values (0,"数学");
insert into courses values (0,"英语");
insert into stu_cour values (0,1,1);
insert into stu_cour values (0,1,2);
insert into stu_cour values (0,1,3);
insert into stu_cour values (0,2,1);
insert into stu_cour values (0,2,3);
insert into stu_cour values (0,3,2);
insert into stu_cour values (0,3,3);
5.查询学生1选了哪些科目
SELECT courses.id,courses.name FROM courses
INNER JOIN stu_cour ON stu_cour.course_id=courses.id
INNER JOIN students ON students.id= 1
and students.id = stu_cour.stu_id;
6.查询id=2数学被谁选了
SELECT students.name FROM students
INNER JOIN stu_cour ON stu_cour.stu_id =students.id
INNER JOIN courses ON courses.id= 2
and stu_cour.course_id = courses.id;
二、MySQL 创建一对一关系
1.一对一
创建用户表:
CREATE TABLE users (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
用户信息表:
CREATE TABLE users_info (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
age int NOT NULL,
phone varchar(11) NOT NULL,
user_id int not null,
constraint user_info foreign key(user_id) references users(id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
2.插入数据
insert into users values (0,"小王");
insert into users values (0,"小宋");
insert into users_info values (0,12,'13812345678',1);
insert into users_info values (0,14,'13812345679',2);
查询人的全部信息:
select * from users inner join users_info on
users_info.user_id =users.id;
来源:https://blog.51cto.com/u_15173612/5102580
标签:MySQL,创建,多对多,一对一
0
投稿
猜你喜欢
Python实现国外赌场热门游戏Craps(双骰子)
2023-12-13 23:22:21
MySQL中distinct与group by之间的性能进行比较
2024-01-15 05:01:56
请谨慎对待程序的图标和名称
2011-06-16 20:35:22
JavaScript前端实用的工具函数封装
2024-04-25 13:15:02
python中open函数的基本用法示例
2021-08-03 20:42:31
对python中数组的del,remove,pop区别详解
2021-01-23 09:22:31
Python中的Selenium异常处理
2021-08-28 04:15:23
Python decorator拦截器代码实例解析
2022-04-07 11:03:47
用代码帮你了解Python基础(2)
2022-01-04 23:42:40
MS IIS server Frontpage Ext Server漏洞
2008-05-04 09:54:00
Python对文件操作知识汇总
2023-02-26 11:10:59
Python实现迪杰斯特拉算法并生成最短路径的示例代码
2023-06-05 21:50:10
MySQL学习笔记4:完整性约束限制字段
2024-01-27 15:54:29
Python time模块之时间戳与结构化时间的使用
2024-01-02 09:07:51
MySQL中把varchar类型转为date类型方法详解
2024-01-27 03:15:01
Golang汇编之控制流深入分析讲解
2024-05-08 10:15:11
python Opencv实现停车位识别思路详解
2023-10-20 21:54:25
PyTorch 多GPU下模型的保存与加载(踩坑笔记)
2023-07-20 15:39:18
基于python实现的百度新歌榜、热歌榜下载器(附代码)
2022-10-29 10:16:12
Python调用Prometheus监控数据并计算
2023-12-01 02:18:39