学生表和课程表可以多对多

一个学生可以学多门课程

一门课程可以有多个学生: 多对多
***
一个学生对应一个班级

一个班级对应多个学生: 一对多
***
一个老师对应多个学生

多个学生对应一个老师:一对多
***
一个老师教一门课

一门课对应一个老师: 一对一

1. 一对多(foreign key):
学生表要关联班级表,多个学生属于一个班级. 班级是被关联的表.
create table class(
id int primary key auto_increment,
caption  char(20)
);

insert into class(caption) values('三年二班'),('一年三班'),('三年一班');

create table student (
id int primary key into_increment,
name varchar(20),
sex enum('male','famale') default 'male',
class_id int,
foreign key(class_id) references class(id)
);

insert into sudent(name,class_id) values ('钢弹',1),('铁锤',1),('山炮',2);

#对这个班级来说,一门课程只有一个老师,一个老师只会教一门课程. 所以是一对一的关系

create table teacher(
id int primary key auto_increment,
name varchar(20)
on delete cascade
on update cascade
);

insert into teacher(name) value('ff'),('ff1'),('ff2');

create table course (
id int primary key auto_increment,
name varchar(20),
teacher_id int unique,
foreign key(teacher_id) references teacher(id)
);

#一个学生可以学多门课程,一门课程可有有多个学生, 他们就是多对多的关系.
create table student2course (
id int primary key auto_increment,
student_id int,
course_id int,
foreign key(student_id) references student(id)
on delete cascade
on update cascade,
foreign key(course_id) references course(id)
on delete cascade
on update cascade,
score int
);

insert into student2course (student_id,course_id,score) values(1,1,60),(1,2,59),(2,2,100);

user 和 admin的一对一关系

create teable user(

id int premary key auto_increment,

name varchar(10)

on delete cascade

on update cascade# 只管理被关联的表

);

create table admin(

id int priment key auto_increment,

password varchar(10),

user_id int unique,

foreign key(user_id) references user(id)

);

..

..

..
..
..
..
..
..
..

学生表关联客户表, 学生一定是客户,客户不一定是学生.
create table customer(
id int primary key auto_increment,
name char(10),
qq int
);

create table  stu(
id int primary key auto_increment,
name varchar(10),
cid int unique,
foreign key(cid) references customer(id)
);

多对多

create table book(
id int primary key auto_increment,
name varchar(20) not null
);

create table author (
id int primary key auto_increment,
name varchar(20),
price float(5)
);

create table book2author(
id int primary key auto_increment,
book_id int,
author_id int,
foreign key(book_id) references book(id)
on delete cascade
on update cascade,
foreign key(author_id) references author(id)
on delete cascade
on update cascade,
unique(book_id,author_id)
);

用户表可以属于一个组, 用户可以 foreign 组

多个组可以由一个用户管理

这俩就是多对多, 找一张单独的表 存他们关联的字段. 保证关联字段联合唯一.

另外例子

**
unique (1,2) ##意思就是联合唯一

select查询语句

1.简单查询

select * from 表;

select name,salary from employee

2.where 条件

from employee where salary >1000;

3.group by (分组) !!!重要

select * from employee group by sex; #by 后面的字段必须是可以能区分的.

select depart_id,group_concat(name) from employee group by depart_id;

group_concat (合并多个数组)

count(id) # 这个 id 的个数

select depart_id,count(id) from employee group by depart_id;

max(salary)

min(salary)

sum(salary)

avg(salary)

MySQL一对一:一对多:多对多的更多相关文章

  1. MySQL一对一:一对多:多对多: 实例!!!!

    学生表和课程表可以多对多 一个学生可以学多门课程 一门课程可以有多个学生: 多对多 *** 一个学生对应一个班级 一个班级对应多个学生: 一对多 *** 一个老师对应多个学生 多个学生对应一个老师:一 ...

  2. Python进阶----表与表之间的关系(一对一,一对多,多对多),增删改查操作

    Python进阶----表与表之间的关系(一对一,一对多,多对多),增删改查操作,单表查询,多表查询 一丶表与表之间的关系 背景: ​ ​ ​  ​ ​ 由于如果只使用一张表存储所有的数据,就会操作数 ...

  3. JPA级联(一对一 一对多 多对多)注解【实际项目中摘取的】并非自己实际应用

    下面把项目中的用户类中有个:一对一  一对多  多对多的注解对应关系列取出来用于学习      说明:项目运行正常 问题类:一对多.一对一.多对多 ============一对多 一方的设置 @One ...

  4. mybatis 一对一 一对多 多对多

    一对一 一对多 多对多

  5. JPA 一对一 一对多 多对一 多对多配置

    1 JPA概述 1.1 JPA是什么 JPA (Java Persistence API) Java持久化API.是一套Sun公司 Java官方制定的ORM 方案,是规范,是标准 ,sun公司自己并没 ...

  6. 使用NHibernate(7)-- 一对一 && 一对多 && 多对多

    1, 一对一. 对于数据量比较大的时候,考虑查询的性能,肯能会把一个对象的属性分到两个表中存放:比如用户和用户资料,经常使用的一般是Id和用户名,用户资料(学校,籍贯等)是不经常被查询的,所以就会分成 ...

  7. day 69-70 一对一 一对多 多对一联表查询

    day 69 orm操作之表关系,多对多,多对一 多对一/一对多, 多对多{类中的定义方法} day69 1. 昨日内容回顾 1. 单表增删改查 2. 单表查询API 返回QuerySet对象的: 1 ...

  8. 初学者易上手的SSH-hibernate04 一对一 一对多 多对多

    这章我们就来学习下hibernate的关系关联,即一对一(one-to-one),一对多(one-to-many),多对多(many-to-many).这章也将是hibernate的最后一章了,用于初 ...

  9. SQLAlchemy_定义(一对一/一对多/多对多)关系

    目录 Basic Relationship Patterns One To Many One To One Many To Many Basic Relationship Patterns 基本关系模 ...

随机推荐

  1. Bugzilla-5.0.3 (OpenLogic CentOS 7.2)

    平台: CentOS 类型: 虚拟机镜像 软件包: apache2.4.6 bugzilla5.0.3 mariadb5.5.47 perl5.16.3 apache bug tracking bug ...

  2. 利用photoshop制作gif图片

    首先准备你需要的几张素材图片 1.将素材图片根据发生的顺序放置在不同的图层 2.打开窗口下的时间轴 选择帧动画 3.创建第一张帧动画 选项卡右边这个按钮,点击这个选择新建帧 第一张图片显示其他的隐藏 ...

  3. linux命令 ——目录

    开始详细系统的学习linux常用命令,坚持每天一个命令,所以这个系列为每天一个linux命令.学习的主要参考资料为: 1.<鸟哥的linux私房菜> 2.http://codingstan ...

  4. IOS Block动画

    ● + (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)dur ...

  5. 理顺react,flux,redux这些概念的关系

    作者:北溟小鱼hk链接:https://www.zhihu.com/question/47686258/answer/107209140来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转 ...

  6. Linux运维工程师是什么鬼?

    第一部分:定义 运维工程师,字面理解运行维护. linux运维即linux运维工程师,集合网络.系统.数据库.开发.安全工作于一身的“复合性人才”.   除了传统IT运维部分,运维人员还是管理制度.规 ...

  7. js 封装父页面子页面交互接口

    定义标准接口 Interface= {}; Interface.ParentWin = {}; Interface.ChildWin = {}; /** * 父页面提供的标准接口函数名称 */ Int ...

  8. pyqt4 python2.7 中文乱码的解决方法

    import sysimport localefrom PyQt4.QtGui import *from PyQt4.QtCore import *from untitled import Ui_Di ...

  9. 在 Java 8 中避免 Null 检查

    如何预防 Java 中著名的 NullPointerException 异常?这是每个 Java 初学者迟早会问到的关键问题之一.而且中级和高级程序员也在时时刻刻规避这个错误.其是迄今为止 Java ...

  10. 【luogu题解】P1546 最短网络 Agri-Net

    题目 约翰已经给他的农场安排了一条高速的网络线路,他想把这条线路共享给其他农场.为了用最小的消费,他想铺设最短的光纤去连接所有的农场. 你将得到一份各农场之间连接费用的列表,你必须找出能连接所有农场并 ...