码迷,mamicode.com
首页 > 数据库 > 详细

数据库

时间:2019-03-16 16:50:06      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:sed   left join   har   val   and   table   pen   笛卡尔积   uniq   

多表之间的关联最好是用逻辑上的关联,而不是物理上的关联,导致后期的扩展性差!!!

原生sql

--

建表

一对多:

技术图片
create table dept(id int primary key auto_increment,name char(20),job char(20));
create table emp(id int primary key auto_increment,name char(20),d_id int,foreign key(d_id) references dept(id));
一对多建表

多对多:

技术图片
 1 create table teacher(id int primary key auto_increment,name char(15));
 2 create table student(id int primary key auto_increment,name char(15));
 3 #中间表
 4 create table tsr(
 5 id int primary key auto_increment,
 6 t_id int,s_id int,
 7 foreign key(t_id) references teacher(id),
 8 foreign key(s_id) references student(id)
 9 );
10 现在老师和学生 都是主表  关系表是从表
11 先插入老师和学生数据
12 insert into teacher values
13 (1,"高跟"),
14 (2,"矮跟");
15 
16 insert into student values
17 (1,"炜哥"),
18 (2,"仨疯");
19 
20 # 插入对应关系
21 insert into tsr values
22 (null,1,1),
23 (null,1,2),
24 (null,2,2);
25 
26 建表语句,中间表及插数据语句
多对多建表

一对一:

技术图片
客户和学生
一个客户只能产生一个学生
一个学生只能对应一个客户
这样的关系是一对一
使用外键来关联,但是需要给外键加上唯一约束
客户和学生有主从关系,需要先建立客户,再建学生

create table customer(c_id int primary key auto_increment,
name char(20),phonenum char(11),addr char(20));

create table student1(s_id int primary key auto_increment,
name char(20),
class char(11),
number char(20),
housenum char(20),c_id int UNIQUE,
foreign key(c_id) references customer(c_id)
);

示例,建表语句
一对一建表

查询

单表查询:

技术图片
不带关键字的查询
    select  {*|字段名|四则运行|聚合函数} from 表名 [where 条件]
    1.* 表示查询所有字段
    2.可以手动要查询的字段
    3.字段的值可以进行加减乘除运算
    4.聚合函数,用于统计
    where是可选的
View Code

一对多查询五种方式:

技术图片
1.笛卡尔积查询
select *from 表1,表n
查询结果是
将左表中的每条记录,与右表中的每条记录都关联一遍
因为他不知道什么样的对应关系是正确,只能帮你都对一遍
a表有m条记录,b表有n条记录
笛卡尔积结果为m * n 记录

需要自己筛选出正确的关联关系
select *from emp,dept where emp.dept_id = dept.id;

2.内连接查询就是笛卡尔积查询:[inner] join
select *from emp [inner] join dept;
select *from emp inner join dept where emp.dept_id = dept.id;

3.左外链接查询:left join
select *from emp left join dept on emp.dept_id = dept.id;
左表数据全部显示,右表只显示匹配上的

4.右外链接查询:right join
select *from emp right join dept on emp.dept_id = dept.id;
右表数据全部显示,左表只显示匹配上的

内和外的理解:内指的是匹配上的数据,外指的是没匹配上的数据

5.全外连接:union
select *from emp full join dept on emp.dept_id = dept.id;  ##mysql不支持

 union: 合并查询结果,默认会去重
 select *from emp left join dept on emp.dept_id = dept.id
 union
 select *from emp right join dept on emp.dept_id = dept.id;

 union 去除重复数据,只能合并字段数量相同的表
 union all 不会去除重复数据

on,where关键字都是用于条件过滤,没有本质区别
在单表中where的作用是筛选过滤条件
只要是连接多表的条件就使用on,为了区分是单表还是多表,搞个新的名字就是on
在多表中on用于连接多表,满足条件就连接,不满足就不连接
View Code

 多对多查询:

技术图片
create table stu(id int primary key auto_increment,name char(10));
create table tea(id int primary key auto_increment,name char(10));

#中间表
create table tsr(id int primary key auto_increment,t_id int,s_id int,
foreign key(s_id) references stu(id),
foreign key(s_id) references stu(id));
#插数据
insert into stu values(null,"张三"),(null,"李李四");
insert into tea values(null,"chuck"),(null,"wer");
insert into tsr values(null,1,1),(null,1,2),(null,2,2);
#查询语句,多表查询过滤条件用on
select *from stu join tea join tsr
on stu.id = tsr.s_id and tea.id = tsr.t_id
where tea.name = "chuck";
View Code

 子查询:

技术图片
给你部门的的名称,查部门有哪些人?
    第一步查到部门的id
    第二部拿着id去员工表查询
select *from dept join emp on dept.id = emp.dept_id;

select *from emp join
# 使用子查询,得到每个部门的id以及部门的最高工资,形成一个虚拟表,把原始表和虚拟表连接在一起
(select dept_id,max(salary)as m from emp group by dept_id) as t1
# 如果这个人的部门编号等于虚拟表中的部门编号
on emp.dept_id = t1.dept_id
and
# 并且,如果这个人的工资等于虚拟表中的最高工资,就是你要找的人
emp.salary = t1.m;
View Code

orm

 

数据库

标签:sed   left join   har   val   and   table   pen   笛卡尔积   uniq   

原文地址:https://www.cnblogs.com/xuechengeng/p/10542442.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!